我无法比较Bash中的两个表达式

时间:2013-10-19 09:12:58

标签: bash operator-keyword

我正在尝试写一个像这样的表达式--->如果在bash中[ $condition1 ] && [ $condition2 ],但我一直都会遇到同样的错误

syntax error near unexpected token `elif'
./leer.sh: line 25: `elif [[ "$($(date +%Y) -eq $ano)"  && "$($(date +%m) -lt $mes)" ]]'

代码的那部分是:

elif [[ $($(date +%Y) -eq $year)  && $($(date +%m) -lt $month) ]]
then
echo "Well done";

1 个答案:

答案 0 :(得分:1)

这里有一些问题:

  • 您的情况不是以if开头,而是elif
  • 您错过了最后的fi
  • 您尝试使用第二个$(...)
  • 执行逻辑表达式

你试着这样做吗?

#!/bin/bash

year=2013
month=11

if [[ $(date +%Y) -eq $year && $(date +%m) -lt $month ]]; then
  echo "Well done"
fi