Bash整数比较
发布时间:2020-12-15 19:51:51 所属栏目:安全 来源:网络整理
导读:我想写一个bash脚本,检查是否至少有一个参数,如果有一个参数,如果该参数是0或1。 这是脚本: #/bin/bashif (("$#" 1)) ( (("$0" != 1)) || (("$0" -ne 0q)) ) ; thenecho this script requires a 1 or 0 as first parameter.fixinput set-prop 12 "Device
我想写一个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") 我究竟做错了什么?
这个脚本有效!
#/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]如果第一个参数是一个字符串,则第二个失败 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |