0

I am a bash newbie (a PHP dev posing as a Linux sysadmin).
I have the following if block in a bash script:

 if [ "$DAY"=="Sunday" ];
 then
 cp /Drive_D/videos_bu/daily/$TODAY /Drive_D/videos_bu/weekly/$WEEK
 printf "\n==========\nWeekly Backup - $TODAY SUCCESS!\n==========\n\n" >> $LOG
 fi

The printf line executes even when the test command is false. Can anyone tell me why?

TIA

Roger

7

Due to the fact that you don't have the operator, and operands delimited by spaces, the interpreter assumes the whole item as a string, which is different than the empty string, and it goes to the then branch. Modify your condition to:

if [ "$DAY" == "Sunday" ];

in order to get the expected result. Here's a useful post.

2
  • ahhh! Thanks. I'll give that a go. Thanks much. – Roger Creasy Jul 6 '16 at 14:43
  • BTW, I recommend using a single = in [ ], since it's more portable. bash allows ==, but if you're writing for bash (not just any old POSIX shell), you might as well use the cleaner [[ ]] form of conditional instead (see BashFAQ #31). – Gordon Davisson Jul 6 '16 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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