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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:17:30  来源:igfitidea点击:

How to use expect inside bash script

bashexpect

提问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 expectcommand requires its arguments on a file or as an argument using the -coption.

您定义的 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 expectcommand.

您有多种选择,但要对脚本添加较少的修改,您只需要使用进程替换来为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"