简体   繁体   中英

Bash: Expect script does not store value of exit code

I'm using a bash expect script and want to record the exit code for the diff command at the end in a variable. However it does not recognise it. How do i make this record and store the value?

/usr/bin/expect << 'EOF'
set timeout -1
spawn ssh root@server
send "wget -r  --spider --user user--password password server/php/site_index.php -P /data/tmp/wget_result_after \r"
expect { 
            "Downloaded" 
       }
send "set exitCodeDb 1 \r"
expect { 
            "*]# " 
       }
send "diff --brief /data/tmp/db1 /data/tmp/db2 && exitCodeDb=0 || exitCodeDb=1 \r"
expect { 
            "*]# " 
       }  
send "echo \"Exit code for DB diff is $exitCodeDb\" \r"  
 expect { 
            "*]# " 
       }

EOF

I get the error

 can't read "exitCodeDb": no such variable

The problem is that the variable $exitCodeDb gets interpreted by expect .

You actually want to pass a string with a bash variable, but for expect this is only a string.

In order to pass the dollar sign, you can use the accolade {...} :

set str {echo -e "Exit code for DB diff is $exitCodeDb"}
send "$str\r"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM