I can seem to see why this doesn’t work:
#!/bin/bash
if [ $# -ne 1 ] || [ $# -ne 2 ]; then
# Should run if there are either 1 or 2 options specified
echo "usage: ${0##*/} <username>"
exit
fi
When testing to see if it works:
root@ubuntu:~# testing.sh optionone optiontwo
...Correct output...
root@ubuntu:~# testing.sh optionone
usage: testing.sh <username>
Note that you are executing 2 commands in:
[ $# -ne 1 ]is a 1st command, and the[ $# -ne 2 ]command is executed only if the previous has a non-zero error code as of the||shell operator.In your case, it is not important, but in the case bellow, it is:
The 2nd command will always be true, as the 2nd
$?is the return code of[ $? -eq 0 ]. You can test it with the lines bellow that will printtruetwice:The correct way to execute a
orin a single command is:This way, those bellow only print
trueonce:And concerning your original question, kev has already point out that there was a logic error in your test. The negative of
[ $# -eq 1 ] || [ $# -eq 2 ]isNOT [ $# -eq 1 ] && NOT [ $# -eq 2 ]and this becomes[ $# -ne 1 ] && [ $# -ne 2 ]or in a single command: