Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Hoping someone here can help me with a bash script that runs only on certain conditions.

#!/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

When I try to run the above I get the error.

line 4: [: missing `]'

Any tips would be appreciated, thanks in advance.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I would try like this:

#!/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

Some parentheses are there only for clarity.

But you should be careful, your test does not mirror what your echo statement affirms. The test evaluates to a positive for ANY Sunday, not just the first of the month.

share|improve this answer
    
Thanks, I went for this one and it worked like a charm. –  British Sea Turtle Sep 28 '13 at 20:09

The ] is special and needs to be on its own. You're missing a space before the last one.

So line 4 should be:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.