zoukankan      html  css  js  c++  java
  • 将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 "
    expect "sftp>"
    send "get *BJ*.$time1* "    $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 "
    expect "sftp>"
    send "cd dir "
    expect "sftp>"
    send "get *BJ*.$time2* "    
    sleep 5
    expect "sftp>"
    sleep 5
    #set timeout 300
    EOF

  • 相关阅读:
    NOIP2018 模拟赛(二十二)雅礼NOI
    浅谈左偏树在OI中的应用
    HDU3062&&HDU1814
    2-SAT超入门讲解
    bitset常用用法&&简单题分析
    NOIp2014提高组初赛错题简析
    2018十月刷题列表
    BZOJ 4804: 欧拉心算
    Luogu P2568 GCD
    Luogu P4137 Rmq Problem / mex
  • 原文地址:https://www.cnblogs.com/xavierlee/p/6497146.html
Copyright © 2011-2022 走看看