Pass bash array to expect script

烈酒焚心 提交于 2020-01-14 03:42:07

问题


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

$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].

From bash, it will be a bunch of values, e.g. 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.

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.


回答1:


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:~$


来源:https://stackoverflow.com/questions/44555049/pass-bash-array-to-expect-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!