将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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM