bash 为什么我的期望脚本在第 1 行失败?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4505700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my expect script failing on line 1?
提问by Dennis Day
The very first line of my expect script fails. Here are the entire contents of my script and it fails:
我的期望脚本的第一行失败了。这是我的脚本的全部内容,但它失败了:
#!/usr/bin/expect -f
And it fails right off the bat with
它立即失败了
": no such file or directory
as my response. Expect is in fact installed and is located in /usr/bin/ and I am running this from root. I have no extra spaces or lines before the # sign either. Of course there was more to the script originally but it fails way before it gets to the good stuff.
作为我的回应。Expect 实际上已安装并位于 /usr/bin/ 中,我正在从 root 运行它。我在 # 符号之前也没有多余的空格或行。当然,最初的脚本还有更多内容,但在获得好东西之前就失败了。
回答by Bert F
Tried it and here is the result: /usr/bin/expect^M: bad interpreter
试过了,结果如下:/usr/bin/expect^M: bad interpreter
Is it possible that there's a Windows newline (the "^M") in there that's confusing the script? You can try "od" to see what newline character(s) is after the the "expect" and "tofromdos" or an editor (e.g. emacs in hexl-mode) to remove it. See the man pages for more info.
是否有可能在那里有一个 Windows 换行符(“^M”)混淆了脚本?您可以尝试使用“od”来查看“expect”和“tofromdos”之后的换行符是什么,或者使用编辑器(例如十六进制模式下的 emacs)将其删除。有关更多信息,请参阅手册页。
回答by G_Style
I had this issue and found I didn't have the expect interpreter installed! Oddly enough, if you ran the command in the shell it worked. However, through a shell script I got the error: /usr/bin/expect: bad interpreter: No such file or directory.
我遇到了这个问题,发现我没有安装 expect 解释器!奇怪的是,如果您在 shell 中运行该命令,它会起作用。但是,通过 shell 脚本我得到了错误:/usr/bin/expect: bad interpreter: No such file or directory。
I fixed it by simply installing the Expect interpreter. The package name that was chosen was: expect libtcl8.6
我通过简单地安装 Expect 解释器来修复它。选择的包名是:expect libtcl8.6
Just run:
赶紧跑:
sudo apt-get install expect
回答by Ignacio Vazquez-Abrams
Your line endings are wrong. Shove it through dos2unix
or tr -d '\r'
.
你的行尾是错误的。把它推过去dos2unix
或tr -d '\r'
。
回答by Glenn McAllister
I don't really know expect, to be honest, but when I run that on my system it "works" fine. Nothing happens, but that's what I'd expect. I don't get any error message. According to the man page,
老实说,我真的不知道期望,但是当我在我的系统上运行它时,它“工作”得很好。什么都没有发生,但这正是我所期望的。我没有收到任何错误消息。根据手册页,
#!/usr/bin/expect -f
is the correct way to start your script. Expect then slurps up the script you are executing as the cmdfile.
是启动脚本的正确方法。然后,Expect 将您作为 cmdfile 执行的脚本包含在内。
The way I got it to reproduce the problem was to actually put a ^M at the end of the line instead of a normal newline (saw Bert F's response and that prompted me to try it). I'm sure vim's :set list command will show any odd characters.
我让它重现问题的方法是在行尾实际放置一个 ^M 而不是普通的换行符(看到 Bert F 的响应并促使我尝试它)。我确信 vim 的 :set list 命令会显示任何奇数字符。