bash 如何将变量从 shell 脚本传递到期望脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15422757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:27:00  来源:igfitidea点击:

How to pass variables from a shell script to an expect script?

bashshellexpect

提问by Freephone Panwal

I've shell script as below:

我的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

In above script, I'm fetching gateway from user input selection & trying to pass to my abc.sh script which is expect scriptshown below:

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

#!/usr/bin/expect

set timeout 3
spawn ssh "james@$gateway"
expect "password:"
send "TSfdsHhtfs\r";
interact

But I'm not able to pass gateway variable from shell script to expect script. Can any one tell me how to achieve this?? Please note that I need to use shell script only due to legacy reasons (Cannot use tcl script or can't do everything in expect script itself)

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

回答by Hai Vu

From your shell script:

从您的 shell 脚本:

/mypath/abc $gateway

From your expect script:

从您的期望脚本:

#!/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