如何在Shell脚本中执行逻辑OR运算以进行整数比较? [英] How to do a logical OR operation for integer comparison in shell scripting?

查看:61
本文介绍了如何在Shell脚本中执行逻辑OR运算以进行整数比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如果 $#等于 0 或大于 1 ,则打个招呼.

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

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

I have tried the following syntax with no success:

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

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

推荐答案

这应该有效:

#!/bin/bash

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

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

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:

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

这篇关于如何在Shell脚本中执行逻辑OR运算以进行整数比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆