Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shell script if statement always executes else block

I have written a simple ifelse 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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading