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:
#/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
This gives the following errors:
./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")
What am I doing wrong?
This script works!
#/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
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:
#/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
The output is the same1:
$ ./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] the second fails if the first argument is a string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With