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

1 Answer 1

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. Commented Jul 6, 2016 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). Commented Jul 6, 2016 at 17:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .