expect inside bash + wrong # args: should be “foreach varList list ?varList list …? command”

I am using expect script inside bash script. When I use foreach inside expect script, it throws

wrong # args: should be "foreach varList list ?varList list ...? command"

The code:

#!/bin/bash

read -p "Enter username: " username
read -s -p "Enter password: " password

#Expect script
/bin/expect -<<EOD 

set SERVERS {100 101 102}

foreach SERVER $SERVERS {
set timeout -1
spawn scp ${username}@plsa${SERVER}.corp.com:/log.2011-11-24 ${SERVER} 
expect "*password:"; send "$password\r"
expect eof }
EOD

echo "completed"

Thanks

Answers


The heredoc (<<ENDTOK) is subject to shell expansion on the $variables. That means for each of the $variables you want expect to interpret, you'll need to escape the $.


The way to escape something is to prepend a slash to it ($ -> \$). It appears the username and password are supposed to come from the shell, the rest from within expect, so here's how that should go:

#!/bin/bash

read -p "Enter username: " username
read -s -p "Enter password: " password

#Expect script
/bin/expect -<<EOD 

set SERVERS {100 101 102}

foreach SERVER \$SERVERS {
set timeout -1
spawn scp ${username}@plsa\${SERVER}.corp.com:/log.2011-11-24 \${SERVER} 
expect "*password:"; send "$password\r"
expect eof }
EOD

echo "completed"

Note the \ in front of $SERVERS and ${SERVER}.


Need Your Help

How to make such jquery declaration more compact?

javascript jquery refactoring jeditable

I'm using jeditable for around 30-40 fields of different type, that's why I have to write a number of the blocks