如何将变量从shell脚本传递到expect脚本?

时间:2013-03-15 00:37:43

标签: bash shell expect

我的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脚本中的网关变量传递给期望脚本。谁能告诉我如何实现这一目标?请注意,由于遗留原因,我只需要使用shell脚本(不能使用tcl脚本或者不能在期望脚本本身中执行所有操作)

1 个答案:

答案 0 :(得分:12)

从你的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