Home > front end >  Shell script if statement always executes else block
Shell script if statement always executes else block

Time:01-05

I have written a simple if-else statement and am calling it from cronjob.

cronscript.sh

#!/bin/sh

# Author : TEST USER
# Script follows here:
VAR1="-h"
VAR2="-h"
if [[ "$VAR1" == "$VAR2" ]]; then
    echo "Strings are equal."
else
    echo "Strings are not equal."
fi

I'm always getting output as Strings are not equal. Even though both strings are same why it is executing ELSE block?

cronjob command

34 18 05 * * /var/www/html/cronscript.sh > /var/www/html/testcron.txt

cronjob is executing properly and output is stored in testcron.txt file.

CodePudding user response:

Your script has the shebang #!/bin/sh and tries to use [[, which is bash-specific syntax. You should either change the shebang:

#!/bin/bash

Or use plain POSIX sh conditional syntax:

if [ "$VAR1" = "$VAR2" ]; then
  •  Tags:  
  • Related