Bash if block syntax

一曲冷凌霜 提交于 2021-01-29 17:53:53

问题


I'm having a syntax error with this if block and can't I haven't been able to correct it

if [[ $X >= 100] || [$Y >= 100 ]]
then
   echo "..."
fi

I've rewrote this, but haven't had any luck on finding the correct syntax. Thanks in advance!


回答1:


This is a syntax error, you should try :

if ((X >= 100 || Y >= 100 ))
then
   echo "..."
fi

NOTE

  • with this syntax, no need to remember -ge and such. This is just like arithmetic
  • ((...)) is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for let, if side effects (assignments) are needed. See http://mywiki.wooledge.org/ArithmeticExpression



回答2:


What you're looking for is this :

if [[ $X -ge 100 || $Y -ge 100 ]];
then
   echo "..."
fi



回答3:


Your if is fine. Your [[ command is screwed up.

if [[ $X -ge 100 || $Y -ge 100 ]]



回答4:


You seem to be trying to nest [...] expressions, which is not something you can do in bash. The [[ ... ]] expression is an entirely separate construct available in bash. I won't bother posting a correct expression, as the other answers have that well covered.



来源:https://stackoverflow.com/questions/14328606/bash-if-block-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!