我的 shell 脚本如下:
#!/bin/bash
echo "Select the Gateway Server:"
echo " 1. Gateway 1"
echo " 2. Gateway 2"
echo " 3. Gateway 3"
read gatewayHost
case $gatewayHost in
1) gateway="abc.com" ;;
2) gateway="pqr.com" ;;
3) gateway="xyz.com" ;;
*) echo "Invalid choice" ;;
esac
/mypath/abc
在上面的脚本中,我正在从用户输入选择中获取网关并尝试传递到我的 abc.sh 脚本,这是期望的脚本,如下所示:
#!/usr/bin/expect
set timeout 3
spawn ssh "james@$gateway"
expect "password:"
send "TSfdsHhtfs\r";
interact
但我无法将网关变量从 shell 脚本传递到 expect 脚本。谁能告诉我如何实现这一目标?请注意,我仅出于遗留原因才需要使用 shell 脚本(无法使用 tcl 脚本或无法在 expect 脚本本身中执行所有操作)
最佳答案
从你的 shell 脚本:
/mypath/abc $gateway
来自您的期望脚本:
#!/usr/bin/expect
set gateway [lindex $argv 0]; # Grab the first command line parameter
set timeout 3
spawn ssh "james@$gateway"
expect "password:"
send "TSfdsHhtfs\r";
interact
关于bash - 如何将变量从 shell 脚本传递到 expect 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422757/