Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash整数比较_Bash_Comparison_Integer - Fatal编程技术网

Bash整数比较

Bash整数比较,bash,comparison,integer,Bash,Comparison,Integer,我想编写一个bash脚本,检查是否至少有一个参数,如果有,该参数是0还是1。 以下是脚本: #/bin/bash if (("$#" < 1)) && ( (("$0" != 1)) || (("$0" -ne 0q)) ) ; then echo this script requires a 1 or 0 as first parameter. fi xinput set-prop 12 "Device Enabled" $0 我做错了什么?shell命令的第0个参数

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

#/bin/bash
if (("$#" < 1)) && ( (("$0" != 1)) ||  (("$0" -ne 0q)) ) ; then
echo this script requires a 1 or 0 as first parameter.
fi
xinput set-prop 12 "Device Enabled" $0

我做错了什么?

shell命令的第0个参数是命令本身(有时是shell本身)。您应该使用
$1

(("$#" < 1)) && ( (("$1" != 1)) ||  (("$1" -ne 0q)) )
这个脚本有效

#/bin/bash
if [[ ( "$#" < 1 ) || ( !( "$1" == 1 ) && !( "$1" == 0 ) ) ]] ; then
    echo this script requires a 1 or 0 as first parameter.
else
    echo "first parameter is $1"
    xinput set-prop 12 "Device Enabled" $0
fi
输出为same1:

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

#/bin/bash
if (( ${1:-2} >= 2 )); then
    echo "First parameter must be 0 or 1"
fi
# rest of script...
输出

$ ./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
解释

  • (())
    -使用整数计算表达式
  • ${1:-2}
    -如果未定义,则使用参数展开设置值
    2
  • =2
    -如果整数大于或等于2
    2
    ,则为True

    • 我知道这个问题已经得到了回答,但这是我的问题,因为我认为案例是一个未被重视的工具。(可能是因为人们认为它很慢,但它至少和if一样快,有时甚至更快。)


      这是一个进步,谢谢你。但是,它仍然给我错误:./setTouchpadEnabled:第2行:(:!=1:语法错误:预期的操作数(错误标记为“!=1”)./setTouchpadEnabled:第2行:(:!=0:语法错误:预期的操作数(错误标记为“!=0”)在
      (())中不需要引号
      ,但它们使StackOverflow的高亮显示更加智能。@Cheiron我想你误解了我的评论。再看看我的回答。哇,我确实做了布尔逻辑错误。尽管如此,它仍然给我错误:./setTouchpadEnabled:line 2:(:0&(=1 | | |==0):语法错误:预期操作数(错误标记为“==1 | |==0”)非常感谢,错误是因为该命令是xinput而不是inpuntial。具体原因是您没有坚持使用问题中也使用的算术表达式,即使用
      @user828193:很明显,这是关于计算的,所以算术表达式才是解决问题的方法。这就是为什么我觉得你在回答中改变了问题的这一方面很恼火。@0xC0000022L:你说得对!这是算术表达式。我想我被
      -ne
      和poss的比较搞糊涂了OP的代码中有一个空字符串…看起来您正在使用
      sh./setTouchpadEnabled
      而不是使用bash运行脚本。@jordanm您指的是shebang行中缺少bang吗?
      #/bin/bash
      if [[ ( "$#" < 1 ) || ( !( "$1" == 1 ) && !( "$1" == 0 ) ) ]] ; then
          echo this script requires a 1 or 0 as first parameter.
      else
          echo "first parameter is $1"
          xinput set-prop 12 "Device Enabled" $0
      fi
      
      #/bin/bash
      if (( $# )) && (( $1 == 0 || $1 == 1 )); then
          echo "first parameter is $1"
          xinput set-prop 12 "Device Enabled" $0
      else
          echo this script requires a 1 or 0 as first parameter.
      fi
      
      $ ./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.
      
      #/bin/bash
      if (( ${1:-2} >= 2 )); then
          echo "First parameter must be 0 or 1"
      fi
      # rest of script...
      
      $ ./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
      
      case "$1" in
          0|1) xinput set-prop 12 "Device Enabled" $1 ;;
            *) echo "This script requires a 1 or 0 as first parameter." ;;
      esac