如何将变量从Shell脚本传递到期望脚本? [英] How to pass variables from a shell script to an expect script?

查看:103
本文介绍了如何将变量从Shell脚本传递到期望脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的shell脚本如下:

I've shell script as below:

#!/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脚本,该脚本应如下所示:

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

#!/usr/bin/expect

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

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

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脚本中获取

From your shell script:

/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

这篇关于如何将变量从Shell脚本传递到期望脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆