2013-03-15 176 views
5

如下面我shell脚本:如何将变量从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脚本是指望下面scriptshown:

#!/usr/bin/expect 

set timeout 3 
spawn ssh "[email protected]$gateway" 
expect "password:" 
send "TSfdsHhtfs\r"; 
interact 

但我无法从shell脚本传递网关变量以期望脚本。任何人都可以告诉我如何实现这一目标?请注意,我需要使用shell脚本只是由于传统的原因(不能使用TCL脚本或期望脚本本身不能做的一切)

回答

12

从你的shell脚本:

/mypath/abc $gateway 

从你期望的脚本:

#!/usr/bin/expect 

set gateway [lindex $argv 0]; # Grab the first command line parameter 

set timeout 3 
spawn ssh "[email protected]$gateway" 
expect "password:" 
send "TSfdsHhtfs\r"; 
interact 
相关问题