传递bash数组以期待脚本

时间:2021-04-30 00:11:21

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

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

$SCRIPTS_DIRECTORY/my_expect_script.sh $my_bash_array

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

我似乎可以传递一个变量,然后使用它。对于此示例,变量似乎在[lindex $ argv 0]中。

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

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

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.

我试图找出如何使用expect来获取此变量,将其保存到数组然后循环遍历数组以一次吐出多个命令。

So my output should be something like

所以我的输出应该是这样的

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]

but it looks like I am wrong.

但看起来我错了。

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.

任何人都有任何好的地方,我可以解释可能解释从bash期望更好,或知道如何做到这一点?我认为它相对简单,但在某些方面我期望与我非常相关。

1 个解决方案

#1


2  

Sample.sh

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

Sample.exp

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

Output :

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

#1


2  

Sample.sh

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

Sample.exp

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

Output :

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