Aamir
Aamir

Reputation: 2283

Shell script if statement always executes else block

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.

Upvotes: 2

Views: 1450

Answers (1)

John Kugelman
John Kugelman

Reputation: 361564

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

Upvotes: 4

Related Questions