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

  • 相关阅读:
    Linux就该这么学28期——Day03 2.4-2.7
    Linux就该这么学28期——Day02 2.1-2.3
    Linux就该这么学28期——开篇
    层次遍历应用

    二叉树
    Linux 随手记录
    KMP算法详解(转)
    使用MySQL connector/C++链接MySQL数据库
    C++ 代码片段
  • 原文地址:https://www.cnblogs.com/xavierlee/p/6497146.html
Copyright © 2011-2022 走看看