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

Fre*_*wal 6 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
Run Code Online (Sandbox Code Playgroud)

在上面的脚本中,我从用户输入选择中获取网关并尝试传递给我的abc.sh脚本,该脚本期望如下脚本:

#!/usr/bin/expect

set timeout 3
spawn ssh "james@$gateway"
expect "password:"
send "TSfdsHhtfs\r";
interact
Run Code Online (Sandbox Code Playgroud)

但是我无法将shell脚本中的网关变量传递给期望脚本.谁能告诉我如何实现这一目标?请注意,由于遗留原因,我只需要使用shell脚本(不能使用tcl脚本或者不能在期望脚本本身中执行所有操作)

Hai*_* Vu 12

从你的shell脚本:

/mypath/abc $gateway
Run Code Online (Sandbox Code Playgroud)

从您的期望脚本:

#!/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
Run Code Online (Sandbox Code Playgroud)