1

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 Commented Mar 30, 2021 at 2:38
  • Is it not possible to modify the code here so that it still works under SH? Commented Mar 30, 2021 at 2:40

2 Answers 2

1
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 
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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.