带有 OR 条件的 Bash 脚本

带有 OR 条件的 Bash 脚本

希望这里有人可以帮助我编写一个仅在特定条件下运行的 bash 脚本。

#!/bin/bash
TODAY=`date +%Y-%m-%d`
MODE=$1
if  [ $(date '+%A') == "Sunday" ] || [ $(date '+%d') == "01" ] || [ $MODE == 'Complete']
then
    echo "Running backup as it is the either a Sunday the 1st of the Month or the script was called with the paramater 'Complete'."
    /usr/bin/7z a -t7z -m0=lzma -mx=9 /external-storage/snapshots/snapshot-complete-"$TODAY"-public_html.7z /local-storage/www/public_html > /dev/null
fi

当我尝试运行上述程序时出现错误。

line 4: [: missing `]'

如能得到任何建议我将非常感激,先行致谢。

答案1

我会尝试这样做:

#!/bin/bash
TODAY=`date +%Y-%m-%d`
MODE=$1
DAY=$(date '+%A')
DATE=$(date '+%d')
if  [[  ($DAY == "Sunday")   ||  ($DATE == "01")  ||  ($MODE == "Complete") ]] ; then
    echo "Running backup as it is the either a Sunday the 1st of the Month or the script
    was called with the parameter 'Complete'."
 ...................
fi

有些括号只是为了清楚起见。

但你要小心,你的测试没有反映您的回声声明所肯定的内容。测试结果对任何星期日都为阳性,而不仅仅是每月的第一天。

答案2

]特殊的,需要单独出现。最后一个之前少了一个空格。

因此第 4 行应该是:

if  [ $(date '+%A') == "Sunday" ] || [ $(date '+%d') == "01" ] || [ $MODE == 'Complete' ]

相关内容