2011-11-22 61 views
1

通知FTP_PUT_RETVAL。有兩個echo命令,第一個顯示值爲'0'。第二個顯示沒有價值。我每晚在裏面跑cronBASHGNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)擊變失去了它的價值 - 奇怪

我的腳本告訴我,ftp上傳不成功,因爲FTP_PUT_RETVAL不包含任何值!

這是爲什麼出現?我該如何改變這一點?我做錯了什麼?

#!/bin/sh 
    # 
    # Filename: rh5-manager-cron.sh 
    # 
    # Purpose: Runs backup.cron, and FTP uploads backup to Linux4 
    # 
    # Usage: the program is run with no arguments. This program is 
    # intended to run from manager cron, and redirection is handled inside the cron 
    #  e.g. 00 1 * * * /u2/app/lbin/rh5-manager-cron.sh > /tmp/log/rh5-manager-cron.log 2>&1 
    trap signalCaught ABRT EXIT HUP INT PIPE TERM QUIT 
    signalCaught() { 
     for file in $FTP_LOG_FILE $TMPF; do 
      $BIN_HOME/rm_file $TMPF 
     done 
     exit 
    } 

    if [[ ! -r /u2/conf/ctrl/lettus.sh || ! -r /u2/app/lbin/fsh_sh_lib ]]; then 
     printf "Cannot source environment.\n" >&2 
     exit 1 
    fi 
    . /u2/conf/ctrl/lettus.sh 
    . /u2/app/lbin/fsh_sh_lib 

    #------------------------------------------------------- 
    # Variables 
    #------------------------------------------------------- 
    PRG=${0##*/} 
    FTPUSER=dummy 
    FTPPASS=dummy 
    FTPHOST=192.168.0.3 
    BACKUPDIR=/backup 
    FTPBACKUPNAME=Redhat5Backup.tgz 
    FTPBACKUPFILE=$BACKUPDIR/$FTPBACKUPNAME 
    LOGDIR=/tmp/log 
    FTP_LOG_FILE=$LOGDIR/Redhat5FTP.log 
    SENDLOGS=$C/sendlogs.dat 
    ZOOT_XML=$C/zoot.xml 
    TMPF=`mktemp` || exit 1 

    #------------------------------------------------------- 
    # Sanity 
    #------------------------------------------------------- 
    if ! isValidUser root manager; then 
     printf "$PRG: Must be run as user 'root' or user 'manager'.\n" >&2 
     exit 1 
    fi 

    if [[ ! -d $BACKUPDIR ]]; then 
     logger "$PRG: $BACKUPDIR: Not a valid directory" >&2 
     exit 1 
    fi 

    if [[ ! -d $LOGDIR ]]; then 
     logger "$PRG: $LOGDIR: Not a valid directory" >&2 
     exit 1 
    fi 

    if [[ ! -r $SENDLOGS || ! -r $ZOOT_XML ]]; then 
     logger "$PRG: E-mail: Files are not readable: $SENDLOGS, $ZOOT_XML" >&2 
     exit 1 
    fi 

    if ! which lftp >/dev/null 2>&1; then 
     logger "$PRG: lftp: command not found" >&2 
     exit 1 
    fi 

    # e-mail 
    EMAIL_SUBJECT="Redhat5 FTP PUT" 
    EMAIL_TO=$(email_to $SENDLOGS) 
    EMAIL_FROM=$(email_from $ZOOT_XML) 

    # ftp binary 
    LFTP_BIN=$(which lftp 2>/dev/null) 

    #------------------------------------------------------- 
    # Functions 
    #------------------------------------------------------- 

    # calls lftp to upload a file to server non-interactively 
    ftp_put() { 
     $LFTP_BIN $LFTP_DEBUG -u ${FTPUSER},${FTPPASS} $FTPHOST <<-EOF 
    put $FTPBACKUPFILE 
    pwd 
    ls $FTPBACKUPNAME 
    quit 
    EOF 
    #^^^^^^^^^^^ leave the above indentation alone 
    } 

    main() { 
     # Backup, and send manager logs 
     logger "Running backup.cron..." 
     echo 
     backup.cron 
     BACKUP_CRON_RETVAL=$? 

     logger "Running fsh_sendlogs..." 
     echo 
     $SH_HOME/fsh_sendlogs 1 

     # show ls listing 
     logger "Here comes a directory listing..." 
     echo 
     /bin/ls -l /backup/Redhat5Backup.tgz 
     echo 

     # ftp upload 
     logger "Running ftp upload..." 
     echo 
     ftp_put 
     FTP_PUT_RETVAL=$? 
     echo " -- debug: Line ${LINENO:-}: FTP_PUT_RETVAL=${FTP_PUT_RETVAL:-}" 
    } 

    checkSuccess() { 
     if [[ "$BACKUP_CRON_RETVAL" -eq 0 ]]; then 
      echo "Backup was successful." 
      echo 
      echo "*********************************" 
      echo " OK: Backup was successful." 
      echo "*********************************" 
      echo 
     else 
      echo "ERROR: Backup FAILED! " 
      echo 
      echo "*********************************" 
      echo "  ERROR: Backup FAILED! " 
      echo "*********************************" 
      echo 
     fi 

     echo " -- debug: Line ${LINENO:-}: FTP_PUT_RETVAL=${FTP_PUT_RETVAL:-}" 
     if [ "$FTP_PUT_RETVAL" -eq 0 ]; then 
      echo "lftp: ftp file upload complete." 
      echo 
      echo "*********************************" 
      echo " OK: ftp file upload complete." 
      echo "*********************************" 
      echo 
     else 
      echo "lftp: error ftp file upload not successful." 
      echo 
      echo "*********************************" 
      echo "  ERROR: file upload" 
      echo "  NOT successful." 
      echo "*********************************" 
      echo 
     fi 
    } 

    email_logs() { 
     if [[ -z "$EMAIL_FROM" || -z $EMAIL_TO || -z $EMAIL_SUBJECT || ! -r $FTP_LOG_FILE ]]; then 
      logger "$PRG: One of the variables is not correctly configured" >&2 
      exit 1 
     fi 
     fsh_mail -r "${EMAIL_TO}" -F "${EMAIL_FROM}" -s "${EMAIL_SUBJECT}" -t "${FTP_LOG_FILE}" 
     return 
    } 

    #---------------------------------------------------------------------- 
    # Main Program 
    #---------------------------------------------------------------------- 
    main    | tee $FTP_LOG_FILE 
    checkSuccess  | tee -a $FTP_LOG_FILE $TMPF 
    cat $FTP_LOG_FILE >> $TMPF 

    # E-mail ftp log file 
    logger "Emailing ftp logfile" 
    echo 
    email_logs 

    #eof 
+1

可能是你需要在你的腳本的全局範圍定義一個變量並存儲'FTP_PUT_RETVAL'的價值,它同時裏面'ftp_put',並查看是否在'main'使用時,它保留? –

回答

3

的問題是,你正在使用:

main    | tee $FTP_LOG_FILE 
checkSuccess  | tee -a $FTP_LOG_FILE $TMPF 

由於管道的,在main所做的工作是在一個子shell完成的,一個子shell不能設置變量父殼。所以,當checkSuccess(這也是在一個子shell中運行)去看看結果,看到的是父shell(什麼),而不是什麼是在一個子shell的main功能設置。

如果你放棄了管道,它將開始工作。

更可靠,就把電話給checkSuccessmain功能。或者使用一個或另一個這兩個符號的:

{ main; checkSuccess; } | tee $FTP_LOG_FILE $TMPF 
(main; checkSuccess) | tee $FTP_LOG_FILE $TMPF 

注意,{ ... }符號要求其(...)符號並不需要第二個功能後分號。然而,在這個機制中涉及的過程較少,並不是說它會對整體性能產生任何可衡量的差異(FTP時間將佔主導地位)。

+0

很好的結果,這實際上是問題,而不是像我所建議的那樣單個'[]'。 – chown

+1

這可以通過切換到bash(即腳本開頭的'#!/ bin/bash',因此它不會嘗試與plain-sh兼容)並使用流程替換代替管道來修復:'main >(tee $ FTP_LOG_FILE)' –

+0

哇。我學到了東西。我也看了一下http://stackoverflow.com/questions/5760640/left-side-of-pipe-is-the-subshel​​l –