Bash 整数比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14511295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:16:05  来源:igfitidea点击:

Bash integer comparison

bashcomparisoninteger

提问by Cheiron

I want to write a bash script that checks if there is at least one parameter and if there is one, if that parameter is either a 0 or a 1. this is the script:

我想编写一个 bash 脚本来检查是否至少有一个参数,如果有一个,如果该参数是 0 或 1。这是脚本:

#/bin/bash
if (("$#" < 1)) && ( (("
./setTouchpadEnabled: line 2: ((: ./setTouchpadEnabled != 1: syntax error: operand expected (error token is "./setTouchpadEnabled != 1")
./setTouchpadEnabled: line 2: ((: ./setTouchpadEnabled -ne 0q: syntax error: operand expected (error token is "./setTouchpadEnabled -ne 0q")
" != 1)) || (("
#/bin/bash
if [[ ( "$#" < 1 ) || ( !( "" == 1 ) && !( "" == 0 ) ) ]] ; then
    echo this script requires a 1 or 0 as first parameter.
else
    echo "first parameter is "
    xinput set-prop 12 "Device Enabled" 
#/bin/bash
if (( $# )) && ((  == 0 ||  == 1 )); then
    echo "first parameter is "
    xinput set-prop 12 "Device Enabled" 
$ ./tmp.sh 
this script requires a 1 or 0 as first parameter.

$ ./tmp.sh 0
first parameter is 0

$ ./tmp.sh 1
first parameter is 1

$ ./tmp.sh 2
this script requires a 1 or 0 as first parameter.
else echo this script requires a 1 or 0 as first parameter. fi
fi
" -ne 0q)) ) ; then echo this script requires a 1 or 0 as first parameter. fi xinput set-prop 12 "Device Enabled"
#/bin/bash
if (( ${1:-2} >= 2 )); then
    echo "First parameter must be 0 or 1"
fi
# rest of script...

This gives the following errors:

这会导致以下错误:

$ ./test 
First parameter must be 0 or 1
$ ./test 0
$ ./test 1
$ ./test 4
First parameter must be 0 or 1
$ ./test 2
First parameter must be 0 or 1

What am I doing wrong?

我究竟做错了什么?

采纳答案by user000001

This script works!

这个脚本有效!

(("$#" < 1)) && ( (("" != 1)) ||  (("" -ne 0q)) )

But this also works, and in addition keeps the logic of the OP, since the question is about calculations. Here it is with only arithmetic expressions:

但这也有效,并且还保留了 OP 的逻辑,因为问题是关于计算的。这里只有算术表达式

(( "$#" < 1 && # If the number of arguments is less than one…
  "" != 1 || "" -ne 0)) # …how can the first argument possibly be 1 or 0?

The output is the same1:

输出是相同的1

(( "$#" )) && ((  == 1 ||  == 0 )) # If true, there is at least one argument and its value is 0 or 1

[1] the second fails if the first argument is a string

[1] 如果第一个参数是字符串,则第二个失败

回答by koola

Easier solution;

更简单的解决方案;

case "" in
    0|1) xinput set-prop 12 "Device Enabled"  ;;
      *) echo "This script requires a 1 or 0 as first parameter." ;;
esac

Output

输出

##代码##

Explanation

解释

  • (( ))- Evaluates the expression using integers.
  • ${1:-2}- Uses parameter expansion to set a value of 2if undefined.
  • >= 2- True if the integer is greater than or equal to two 2.
  • (( ))- 使用整数计算表达式。
  • ${1:-2}- 使用参数扩展来设置2如果未定义的值。
  • >= 2- 如果整数大于或等于二,则为真2

回答by kojiro

The zeroth parameter of a shell command is the command itself (or sometimes the shell itself). You should be using $1.

shell 命令的第零个参数是命令本身(或者有时是 shell 本身)。你应该使用$1.

##代码##

Your boolean logic is also a bit confused:

你的布尔逻辑也有点混乱:

##代码##

This is probably what you want:

这可能是你想要的:

##代码##

回答by William

I know this has been answered, but here's mine just because I think case is an under-appreciated tool. (Maybe because people think it is slow, but it's at least as fast as an if, sometimes faster.)

我知道这已经得到了回答,但这是我的,因为我认为 case 是一个被低估的工具。(也许是因为人们认为它很慢,但它至少和 if 一样快,有时更快。)

##代码##