0

The following code:

weekday=$(date +%a)
day=$(date +%d)
month=$(date +%m)    

if [[ ( $month == 03 || $month == 10 ) && $weekday = "Sun" && $day > 24 ]]
    then
      # DO SOMETHING
      exit 1
    else
      # DO SOMETHING
    fi

leads to the error:

 8: Syntax error: word unexpected (expecting ")")

when executing with:

/bin/sh script.sh

What is wrong here and how can this be fixed?

I need to use sh how i need to modificate this code to get working with sh?

2
  • The [[...]] syntax is not available in plain sh. Run your code with /bin/bash – glenn jackman Mar 30 at 2:38
  • Is it not possible to modify the code here so that it still works under SH? – Omexlu Mar 30 at 2:40
1

The correct way is:

weekday=$(date +%a)
day=$(date +%d)
month=$(date +%m)

if { [ $month -eq 03 ] || [ $month -eq 10 ]; } && [ $weekday = "So" ] && [ $day -gt 24 ]
then
  echo "true"
else
  echo "false"
fi

VIEW: https://stackoverflow.com/a/66865406/14997935

0
weekday=$(date +%a)
day=$(date +%d)
month=$(date +%m)

if [ \( \( $month -eq 03 \) -o \( $month -eq 10 \) \) -a \( "$weekday" = "Sun" \) -a \( $day -gt 24 \) ]
then
  # DO SOMETHING
  echo ok
else
  # DO SOMETHING
  echo ko
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.