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

标签 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 脚本传递到 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/

相关文章:

bash - 为什么使用 sed 命令不起作用?

linux - bash 中的百分号不可输入

java - 如何使用 putty 从 linux 运行 spring boot 应用程序

linux - 期待 bash 脚本中的脚本

linux - ARM 交叉编译 Expect

linux - 如何通过 shell 脚本中的命令行在 Expect 中传递参数

linux - 将日志条目安排到标有日期的文件中

linux - Expect 脚本 - 发送字符串所需的引号与 expect 所需的引号冲突

java - 如何从 java 程序运行无限循环 shell 脚本

Java - 执行 Shell 命令不起作用