如何在 bash 脚本中使用 expect

How to use expect inside bash script

谁能告诉我为什么这不起作用?

#!/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"

没有报错但是文件没有传到远程server.I试了很多方法还是没找到解决方法

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