将expect加入到shell脚本后 导致设置变量不生效

将expect加入到shell脚本后 set 定义的变量会失效; 解决办法 : 将变量提到expect之外定义 

#!/bin/bash
. /etc/profile
. ~/.bash_profile
ipnum=`ifconfig|grep addr|grep xxxx |wc -l`
if [ $ipnum -eq 0 ];then
exit
fi
time2=`date "+%Y%m%d"`
expect << EOF
cd /xxx
set time1 [exec date +%Y%m%d]   该语句可以执行 但在后面调用的时候 time1为空
spawn sftp -o port=500 user@ipaddr
set timeout 300
expect "assword:"
send "passwd"
expect "sftp>"
send "cd dir\r"
expect "sftp>"
send "get *BJ*.$time1*\r"    $time1 在取值时得不到结果 为空
sleep 5
expect "sftp>"
sleep 5
#set timeout 300
EOF

 

修改之后的脚本:

#!/bin/bash 
. /etc/profile
. ~/.bash_profile
ipnum=`ifconfig|grep addr|grep xxxx |wc -l`
if [ $ipnum -eq 0 ];then
exit
fi
time2=`date "+%Y%m%d"`
expect << EOF
cd /xxx
spawn sftp -o port=500 user@ipaddr
set timeout 300
expect "assword:"
send "passwd\r"
expect "sftp>"
send "cd dir\r"
expect "sftp>"
send "get *BJ*.$time2*\r"    
sleep 5
expect "sftp>"
sleep 5
#set timeout 300
EOF

 

posted @ 2017-03-03 16:04  XavierLee  阅读(4114)  评论(1编辑  收藏  举报