2

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.

4
  • If I understand correctly, you explain that you can access the variable using [lindex $argv 0]. Why, then, are you trying to use [lindex $argv 5] instead? Commented Jun 14, 2017 at 21:56
  • $my_bash_array only expands to the first element of an array. Maybe your "array" is really a string? Commented Jun 15, 2017 at 1:16
  • The 5 was a typo, I shortened the code from what i was using because i was passing 5 variables to the expect script and the 5th one was the one i was originally trying to access in the live code. Commented Jun 15, 2017 at 13:13
  • Take care with your expect commands: lindex returns a single element from a list; array set expects as its last argument an even-numbered list of name-value pairs. In Tcl/expect, an "array" is actually an associative array, and a "list" is a numerically-indexed array. Commented Jun 19, 2017 at 18:30

2 Answers 2

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:~$
Sign up to request clarification or add additional context in comments.

4 Comments

Quotes -- "${my_array[@]}" -- are needed to ensure that my_array=( "first argument" "second argument" ) is treated distinctly from my_array=( "first" "argument" "second" "argument" ).
Question, If i am passing multiple values to the expect script, Would this be the correct way to call the variable? foreach variable [lindex $argv 5]{ puts "variable equals : $variable" } I,m guessing there is a different way to do it since i am getting this error code wrong # args: should be "foreach varList list ?varList list ...? command" while executing "foreach variable [lindex $argv 5]{"
As an update, i was able to get it to pass the variable correctly, but it still gives the error code wrong # args: should be "foreach varList list ?varList list ...? command" code while trying to pass a variable "1 2 3".
in the sample.sh , second line can replace as tclsh sample.tcl "${my_array[@]}" "${my_arrayTWO[@]}" also works, thanks for this
0

Expanding on @glenn jackman's comment...

Expect treats an Array as a set of key-pair values, while Expect treats a List simply as a list.

Therefore to be able to pass an array/list as a single arg...

Array Example (myExpectArray.tcl)

array set myArray [lindex $argv 0];
foreach item [array name myArray] {
  puts $myArray($item)
}

Call the expect script from command line...

myExpectArray.tcl "0 dog 1 cat 2 rabbit"

To call this from bash, you'd need to loop through the array and build the string to pass to the expect script. Using a List is easier...

List Example (myExpectList.tcl)

set myList [lindex $argv 0];
for {set i 0} {$i < [llength $myList]} {incr i} {
  puts [lindex $myList $i]
}

Call the expect script from bash as follows...

myAnimals+=( "dog" "cat" "rabbit" )
myArgs=${myAnimals[@]}
myExpectList.tcl "$myArgs"

Note: You must convert the list ($myAnimals) to a string ($myArgs) otherwise Expect treats each element of the array as a unique arg.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.