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
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:
Note the
\in front of$SERVERSand${SERVER}.