Reputation: 603
I use expect in bash shell.But it seems that their syntax conflicts with each other's.Codes is shown below:
1 #! /bin/bash
2
3 SLJ_SERVERS=/data/qq_update/web/servers.conf
4
5 >wrong_server.log
6 while read line
7 do
8 if [[ "$line" == "s"* ]];then
9 export IP DIR
10 IP=`echo $line | awk -F: '{print $3}'`
11 DIR=`echo $line | awk -F: '{print $4}'`
12 echo "ip:$IP, dir:$DIR"
13
14 expect <<'END_OF_EXPECT'
exp_internal 1
15 spawn scp2 -o Port=36000 app27805@$env(IP):$env( DIR)/socket/conf/GameServerConfig.xml ./
16 set password "app27805"
17 expect {
18 "(yes/no)?" {
19 send "yes\r"
20 expect "password" {
21 send "$password\r"
22 }
23 }
24 "Password:" {
25 send "$password\r"
26 }
27 }
28 interact
29 END_OF_EXPECT
30 CONFIGURED_IP=`grep "ip" GameServerConfig.xml | awk -F\" '{print $4}'`
31 echo "configured ip:$CONFIGURED_IP" >> configured_ip.log
32 if [[ "$IP" != "$CONFIGURED_IP" ]];then
33 echo "server:$DIR, correct ip:$IP, wrong ip:$CON FIGURED_IP" >> wrong_server.log
34 fi
35 fi
36
37 done < $SLJ_SERVERS
Now I got an error said that:
How can I fix this problem.Thanks for any help.
Upvotes: 1
Views: 191
Reputation: 247210
That's because expect and bash are different languages. You wouldn't put java or assembly code into a bash script and magically get it to work. You have to invoke expect code with the expect
interpreter. The contents of line 2 is just a bash comment: #!
is only special as the first 2 characters of the file (see the execve man page)
Some other errors
[[ $line != #* ]]
-F
option and never close them: I assume you want double quote as the field separator. awk can do grep's work very capably so you don't need both.-eq/-ne
for numeric comparison and =/!=
for strings -- did you not see any errors like "integer expression expected"?while read line
do
if [[ "$line" != #* ]];then
export IP DIR
read _ _ IP DIR <<< "$line"
expect -c <<'END_OF_EXPECT'
spawn scp2 -o Port=36000 app27805@$env(IP):$env(DIR)/socket/conf/GameServerConfig.xml ./
set password "app27805"
expect {
"(yes/no)?" {
send "yes\r"
expect "password" {
send "$password\r"
}
}
"Password:" {
send "$password\r"
}
}
interact
END_OF_EXPECT
CONFIGURED_IP=$(awk -F \" '/ip/ {print $4; exit}' GameServerConfig.xml)
if [ "$IP" != "$CONFIGURED_IP" ]; then
echo "server:$DIR, correct ip:$IP, wrong ip:$CONFIGURED_IP"
fi
fi
done < $SLJ_SERVERS
Note that I single quoted the first END_OF_EXPECT token -- that puts the whole script in single quotes so the expect variables will be handled by expect, not bash. Also the terminating END_OF_EXPECT token must not have any additional whitespace before or after it on the line.
Upvotes: 1