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

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

问题描述

我正在尝试做一个简单的条件检查,但它似乎不起作用.

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脚本中进行整数比较的逻辑或运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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