bash 如何在bash脚本中使用expect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45145094/
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
How to use expect inside bash script
提问by Hasan Rumman
Could anybody please tell me why this is not working?
谁能告诉我为什么这不起作用?
#!/bin/bash
cd /home
touch somefile
/usr/bin/expect<<FILETRANSFER
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "assword:"
send "MyPassWord\r"
interact
FILETRANSFER
echo "It's done"
It doesn't give any error but file is not transferred to remote server.I have tried many ways still couldn't find any solution.
它没有给出任何错误,但文件没有传输到远程服务器。我尝试了很多方法仍然找不到任何解决方案。
回答by Cristian Ramon-Cortes
The bash script you have defined is passing the expect commands on the standard input of expect
. However, the expect
command requires its arguments on a file or as an argument using the -c
option.
您定义的 bash 脚本在expect
. 但是,该expect
命令需要将其参数放在文件上或作为使用该-c
选项的参数。
You have several options but to add the less modifications on your script you just need to use the process substitution to create a here-document (temporary) for the expect
command.
您有多种选择,但要对脚本添加较少的修改,您只需要使用进程替换来为expect
命令创建此处的文档(临时)。
#!/bin/bash
echo "[DEBUG] INIT BASH"
cd /home
touch somefile
/usr/bin/expect <(cat << EOF
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "Password:"
send "MyPassWord\r"
interact
EOF
)
echo "[DEBUG] END BASH"