bash 未找到生成命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21151042/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:37:15  来源:igfitidea点击:

Spawn command not found

bashshellcommandexpectspawn

提问by Phi

I have an error trying to run a .sh file

我在尝试运行 .sh 文件时出错

line 2: spawn: command not found
": no such file or directory
bash.sh: line 3: expect: command not found
bash.sh: line 4: send: command not found
#!/usr/bin/expect -f
spawn sftp -o IdentityFile=MyFile.ppk [email protected]
expect "XXX.XXX.XXX.XXX.gatewayEnter passphrase for key 'MyFile.ppk.ppk':"
send "myPassword"

Any idea why it happens?

知道为什么会发生吗?

采纳答案by Digital Trauma

It works OK for me (error from sftp: ssh: Could not resolve hostname XXX.XXX.XXX.XXX: Name or service not known), though the .shextension for an expect(tcl) script is a little off-putting ;-)

它的工作原理确定,我(从SFTP错误:ssh: Could not resolve hostname XXX.XXX.XXX.XXX: Name or service not known),虽然.sh扩展的预期TCL)脚本是有点倒胃口;-)

Often when this sort of unexplainable/unpredictable behavior happens, it is because the script was edited under windows(notepad.exe), which uses \r\nto delimit lines. This plays havoc with unix/linuxscripts, as only \nis expected as a line delimiter.

通常,当发生这种无法解释/无法预测的行为时,这是因为脚本是在用于分隔行的windows(notepad.exe)下编辑的\r\n。这会对unix/ linux脚本造成严重破坏,因为它只能\n作为行分隔符。

You can use the dos2unixand unix2dosutilitiesto convert between the two formats. As an experiment, I converted your script to "dos" format, and sure enough got a similar error:

您可以使用dos2unixunix2dos实用工具,以这两种格式之间的转换。作为实验,我将您的脚本转换为“dos”格式,果然得到了类似的错误:

ubuntu@ubuntu:~$ unix2dos bash.sh 
unix2dos: converting file bash.sh to DOS format ...
ubuntu@ubuntu:~$ ./bash.sh 
": no such file or directory
ubuntu@ubuntu:~$ dos2unix bash.sh 
dos2unix: converting file bash.sh to Unix format ...
ubuntu@ubuntu:~$ ./bash.sh 
spawn sftp -o IdentityFile=MyFile.ppk [email protected]
ssh: Could not resolve hostname XXX.XXX.XXX.XXX: Name or service not known
Couldn't read packet: Connection reset by peer
send: spawn id exp6 not open
    while executing
"send "myPassword""
    (file "./bash.sh" line 4)
ubuntu@ubuntu:~$ 

回答by glenn Hymanman

  1. that is an expect script, so ".exp" would be an appropriate file extension: mv bash.sh sftp.exp
  2. do not launch it like bash bash.shor sh bash.sh. Do this:
    1. make the program executable: chmod a+x sftp.exp
    2. launch it with ./sftp.expor /path/to/sftp.expor move it to a directory in your $PATH and launch it just with sftp.exp
  3. after you send "myPassword"you have to "hit enter": send "myPassword\r"
  4. while developing an expect program, add exp_internal 1to the top.
  1. 这是一个期望脚本,所以“.exp”将是一个合适的文件扩展名: mv bash.sh sftp.exp
  2. 不要像bash bash.sh或那样启动它sh bash.sh。做这个:
    1. 使程序可执行: chmod a+x sftp.exp
    2. 使用./sftp.exp/path/to/sftp.exp或将其移动到 $PATH 中的目录中,然后使用sftp.exp
  3. 在你send "myPassword"必须“按回车”之后:send "myPassword\r"
  4. 在开发预期程序时,添加exp_internal 1到顶部。

Good luck, and come back with further questions.

祝你好运,回来提出更多问题。

回答by Dheeraj

I was also getting the same error. it got resolved by using expect in following way:

我也遇到了同样的错误。它通过以下方式使用 expect 得到解决:

DIRNAME=$(date +%F:%T)
expect_sh=$(expect -c "
spawn scp -r ABC [email protected]:/root/$DIRNAME
expect \"password:\"
send \"xxxx\r\"
expect \"#\"
spawn ssh -o StrictHostKeychecking=no [email protected]
expect \"password:\"<
send \"xxxx\r\"
expect \"#\"
send \"rm -f /root/$DIRNAME/abc.txt\r\"
expect \"#\"
send \"scp -r /root/$DIRNAME/* [email protected]:/root/ABC/\r\"
expect \"password:\"
send \"xxxxx\r\"
expect \"#\"
send \"exit \r\"
")<b

echo "$expect_sh"

回答by Brightshine

It seems /usr/bin/expect haven't been installed in your machine. So you will get 'command not found'

您的机器中似乎尚未安装 /usr/bin/expect。所以你会得到'找不到命令'

Use which expectto check, and install it to correct path.

使用which expect检查,并将其安装到正确的路径。

回答by Lattis

It all depends on how you invoke the command. Like ray said, even if you specify the environment with a bang at the top, you still have to run it using expect -f.

这完全取决于您如何调用命令。就像 ray 所说的那样,即使您在顶部用 bang 指定环境,您仍然必须使用 expect -f 来运行它。