Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

bash - 如何在shell脚本中执行逻辑OR操作(How to do a logical OR operation in shell scripting)

I am trying to do a simple condition check, but it doesn't seem to work.

(我正在尝试进行简单的条件检查,但它似乎不起作用。)

If $# is equal to 0 or is greater than 1 then say hello.

(如果$#等于0或大于1则说出你好。)

I have tried the following syntax with no success:

(我尝试了以下语法但没有成功:)

if [ "$#" == 0 -o "$#" > 1 ] ; then
 echo "hello"
fi

if [ "$#" == 0 ] || [ "$#" > 1 ] ; then
 echo "hello"
fi
  ask by Strawberry translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should work:

(这应该工作:)

#!/bin/bash

if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then
    echo "hello"
fi

I'm not sure if this is different in other shells but if you wish to use <, >, you need to put them inside double parenthesis like so:

(我不确定这在其他shell中是否有所不同,但是如果你想使用<,>,你需要将它们放在双括号内,如下所示:)

if (("$#" > 1))
 ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...