2

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.

2
  • 1
    Are you using dash as /bin/sh? Commented Jan 5, 2022 at 13:17
  • 2
    If you paste your script into shellcheck you will get the warning SC3010 (warning): In POSIX sh, [[ ]] is undefined. Commented Jan 5, 2022 at 13:20

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.