【技术分享】解决expect<
问题描述
使用 expect 进行交互式 shell 编程,当以 expect<<EOF 的形式在 shell 脚本中穿插 expect 语句时,interact 在执行中无效。
搜索了一番,找到如下解释:
问题出在 expect << EOF。对于 expect << EOF,expect 的 stdin 是 here-doc,而不是tty。但是interact命令只有在 expect 的 stdin 是 tty 时才起作用。
解决方法
使用整段 expect 代码包裹为一个字符串参数的形式,以 -c 形式调用 expect 执行
更改前
...
...shell code...
...
/usr/bin/expect << EOF
spawn xxxxxxx
expect 'xxx'
send 'xxxxxx'
interact
EOF
...
...shell code...
...
更改后
...
...shell code...
...
/usr/bin/expect -c "spawn xxxxxxx
expect "xxx"
send \"xxxxxx\"
interact "
...
...shell code...
...
🌟-阅读剩余-🌟
THE END