通过bash数组以期望脚本 [英] Pass bash array to expect script

查看:96
本文介绍了通过bash数组以期望脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,可以像这样调用期望脚本

I have a bash script that calls an expect script like so

$SCRIPTS_DIRECTORY/my_expect_script.sh $my_bash_array

看来,我可以将变量传递过来并使用它. 对于此示例,变量似乎在[lindex $argv 0]中.

I can pass a variable over, it seems, and use it. For this example the variable seems to be in [lindex $argv 0].

从bash开始,它将是一堆值,例如1 2 3 4 5.

From bash, it will be a bunch of values, e.g. 1 2 3 4 5.

我试图弄清楚如何使用Expect来获取这个变量,将其保存到一个数组中,然后遍历该数组以一次吐出多个命令.

I am trying to figure out how to use expect to grab this variable, save it to an array then loop through the array to spit out multiple commands one at a time.

所以我的输出应该是

send command 1 \r
send command 2 \r

等,直到到达数组末尾.

etc., until it reaches the end of the array.

我以为我希望将其分配为

I thought in expect I would assign it like

array set myArray [lindex $argv 0]

但是看起来我错了.

有人能在我看来有什么好地方可以解释从bash到更好的期望,或者知道如何做到这一点吗?我认为它相对简单,但是在某些方面对我的看法很渺茫.

Would anyone have any good places I can look that might explain going from bash to expect better, or know how to do this? I assume its relatively simple, but expect is very iffy with me in some aspects.

推荐答案

Sample.sh

my_array=(1 2 3 4 5)
expect sample.exp "${my_array[@]}"

Sample.exp

foreach arg $argv { 
    puts "arg : $arg"   
}

输出:

dinesh@mypc:~$ ./sample.sh 
arg : 1
arg : 2
arg : 3
arg : 4
arg : 5
dinesh@mypc:~$

这篇关于通过bash数组以期望脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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