为变量分配两个值

时间:2014-05-12 06:01:53

标签: bash shell

使用以下脚本,我成功地使用检查在特定日期和特定目的地为每个应用程序创建的.zip file,以确认我的备份是否成功。

现在我只使用_2230检查备用时间,但现在我还想在此检查中加入_2250

所以我所做的只是改变了:BACKUPTIME="_2230 _2250",但它似乎没有工作,因为我没有在日志中获得SUCCESSFULFAILED状态

  1. 请你指点一下我脚本中的问题在哪里?
  2. 它对脚本的速度有什么影响我的意思是增加更多 TIME(_2230和现在_2250)来测试备份?

    #!/bin/sh
    
    DATE=`date +%Y%m%d -d "1 days ago"`
    DESTINATION="/Network/Storage/Backup"
    LOG="/Network/Storage/Backup"
    BACKUPTIME="_2230"
    
    APPLICATION="DATADEV TESTREV PRETEST"
    
    for C in $APPLICATION
    do
    
    cd $DESTINATION/$C/
    
    test -f $C$DATE$BACKUPTIME.zip
    
    if [ $? -eq 0 ]
    then
    echo "Backup SUCCESSFUL" >>$LOG/output.txt
    
    else
    echo "Backup FAILED" >>$LOG/output.txt
    
    fi
    
    done
    

2 个答案:

答案 0 :(得分:3)

使用数组:

    #!/bin/sh

    DATE=`date +%Y%m%d -d "1 days ago"`
    DESTINATION="/Network/Storage/Backup"
    LOG="/Network/Storage/Backup"
    BACKUPTIMES=(_2230 _2250)                   #Declare an array

    APPLICATION="DATADEV TESTREV PRETEST"

    for C in $APPLICATION
    do
        cd $DESTINATION/$C/
        success=false
        for BACKUPTIME in ${BACKUPTIMES[@]}; do #Iterate over the array
            test -f $C$DATE$BACKUPTIME.zip && success=true
        done
        if $success
        then
            echo "Backup SUCCESSFUL" >>$LOG/output.txt
        else
            echo "Backup FAILED" >>$LOG/output.txt
        fi
    done

注意:为了便于理解,我对您的代码进行了少量修改。仍有优化的余地。

答案 1 :(得分:1)

这可能会这样做

dya=$(date +%y%m%d -d '1 days ago')
for fdr in datadev testrev pretest
do
  cd /network/storage/backup/$fdr
  pas=1
  for chk in ${fdr}${dya}_{2230,2250}
  do
    [ -f "$chk" ] || pas=0
  done
  (
    printf 'backup '
    (( pas )) && echo successful || echo failed
  ) >> /network/storage/backup/output.txt
done