如何在bash脚本中使用Expect

Has*_*man 4 bash expect

有人可以告诉我为什么这不起作用吗?

#!/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"
Run Code Online (Sandbox Code Playgroud)

它没有给出任何错误,但是文件没有传输到远程服务器。我尝试了许多方法仍然找不到任何解决方案。

Cri*_*tes 5

您定义的bash脚本在的标准输入上传递了Expect命令expect。但是,该expect命令需要将其参数放在文件上或作为使用-c选项的参数。

您有几种选择,但是要在脚本上添加较少的修改,您只需要使用进程替换为命令创建一个此处文档(临时)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"
Run Code Online (Sandbox Code Playgroud)