8:语法错误:单词意外(预期“)”)

8:语法错误:单词意外(预期“)”)

以下代码:

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

导致错误:

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

执行时:

/bin/sh script.sh

这里出了什么问题?如何修复?

我需要使用 sh,我需要如何修改此代码才能使用 sh?

答案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

答案2

正确的做法是:

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

看法:https://stackoverflow.com/a/66865406/14997935

相关内容