將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