我想写一个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
这会给出以下错误:
./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")
我究竟做错了什么?
这个脚本有效!
原文链接:https://www.f2er.com/bash/388634.html#/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
但这也是有效的,另外还保留了OP的逻辑,因为问题是关于计算。这里只有arithmetic expressions:
#/bin/bash if (( $# )) && (( $1 == 0 || $1 == 1 )); then echo "first parameter is $1" xinput set-prop 12 "Device Enabled" $0 else echo this script requiers a 1 or 0 as first parameter. fi
输出相同1:
$ ./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.
[1]如果第一个参数是一个字符串,则第二个失败