Bash - checking variable value - bash

I am trying to check if the the variable $userSelection is not having value 1, 2 or 3, then I display error message. Tried the following and other combinations, but no luck.
if [ $userSelection -ne 1 || $userSelection -ne 2 ] || [ $userSelection -ne 2 || $userSelection -ne 3 ]
then
echo "Option selected not valid...please try again."
fi
Am getting error [: missing]'`.

For your actual needs the right code should be the following:
if [ "$userSelection" -ne 1 ] && [ "$userSelection" -ne 2 ] && [ "$userSelection" -ne 3 ]
then
echo "Option selected not valid...please try again."
fi
From what you are saying the only right choices should be 1,2 or 3.

Missing brackets aside, the simplest way to make such a check is with a case statement instead:
case $userSelection in
1|2|3) ;;
*) echo "Option selected not valid...please try again." ;;
esac

Related

If statement returning error in shell script? [duplicate]

I would like to write in one line this:
if [$SERVICESTATEID$ -eq 2]; then echo "CRITICAL"; else echo "OK"; fi
So to do a test in my shell I did:
if [2 -eq 3]; then echo "CRITICAL"; else echo "OK"; fi
The result is
-bash: [2: command not found
OK
So it doesn't work.
Space -- the final frontier. This works:
if [ $SERVICESTATEID -eq 2 ]; then echo "CRITICAL"; else echo "OK"; fi
Note spaces after [ and before ] -- [ is a command name! And I removed an extra $ at the end of $SERVICESTATEID.
An alternative is to spell out test. Then you don't need the final ], which is what I prefer:
if test $SERVICESTATEID -eq 2; then echo "CRITICAL"; else echo "OK"; fi
Write like this, space is required before and after [ and ] in shell
if [ 2 -eq 3 ]; then echo "CRITICAL"; else echo "OK"; fi
Shorter format.
( [ 2 -eq 3 ] && echo "CRITICAL" ) || echo "OK"
Regex pattern type numbers : 10,12.1,+3.33,-1,0004,-48.9
Oneliner attacks again!
( [ `echo $number 2>/dev/null | grep -E "^[ ]*(\+|\-){0,1}[0-9]+(\.[0-9]+)?$"` ] && echo "NUMBER" ) || echo "NOT NUMBER"

While loop in a shell script gives error : [: too many arguments. How to resolve this issue?

function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read upper_limit
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
do
echo "Please enter again."
read_num
done
}
read_num
when I enter the two numbers lower and upper limit it gives the following output.
check.sh: line 6: [: too many arguments
And line number 6 is while loop
while [ [ $lower_limit -lt 1 ] || [ $lower_limit -gt $upper_limit ] ]
Here you go, this works for me:
#!/bin/bash
function read_num(){
echo "Enter a lower limit"
read lower_limit
echo "Enter a upper limit"
read uper_limit
while [[ $lower_limit -lt 1 ]] || [[ $lower_limit -gt $upper_limit ]]
do
echo "Please enter again."
read_num
done
}
read_num
Reference: Bash scripting, multiple conditions in while loop

If statement not evaluated correctly

I have a simple bash function that returns 3 numerical values: 0, 1, 2
When testing for the return value I get the correct value depending on the one returned from the function. # echo $? -> 0, 1, 2
However, when using an if-else statement the return value is not evaluate as I expected. For example when the function returns value = 2 in the if-else statement the elif [ $? -eq 1 ]; then is choosen
if [ "$?" -eq "0" ]; then
echo "0"
elif [ "$?" -eq "1" ]; then
echo "1"
elif [ "$?" -eq "2" ]; then
echo "2"
else
echo "Incorrect"
fi
Result:
output is: 1
I expect:
output is: 2
Any thoughts.
Cheers,
Roland
By running [ "$?" -eq "0" ] you are changing the value of $?. If you want to compare it several times, store its value into a non-magical variable and compare it instead.
Other option is to use case:
case $? in
(0) echo 0 ;;
(1) echo 1 ;;
(2) echo 2 ;;
(*) echo Incorrect
esac

Bash; conditional statement echoing numbers

I'm nearly done writing a script for an assignment, but am having some trouble thinking of how to do this final part.
My problem is within a while loop; it prints out the number based on the IF statements, the number entered will always be an even number.
The IFs aren't connected by else/elif because the number should be able to printed out if it applies to more than 1 of the statements.
I want to print $starting on every loop if it doesn't meet any of the IF conditions, but if it does I don't want to print it. Can anyone see how to do that?
while [[ $starting -lt $ending ]]; do
if [ $((starting %7)) -eq 0 ]
then
echo "$starting red"
fi
if [ $((starting % 11)) -eq 0 ]
then
echo "$starting green"
fi
if [ $((starting % 13)) -eq 0 ]
then
echo "$starting blue"
fi
starting=$((starting + 2))
done
Keep track of whether you've done what you want to do in a variable:
while [[ $starting -lt $ending ]]; do
handled=0
if [ $((starting %7)) -eq 0 ]
then
echo "$starting red"
handled=1
fi
if [ $((starting % 11)) -eq 0 ]
then
echo "$starting green"
handled=1
fi
if [ $((starting % 13)) -eq 0 ]
then
echo "$starting blue"
handled=1
fi
if ! (( handled ))
then
echo "$starting didn't match anything"
fi
starting=$((starting + 2))
done
Add another if at the end that checks if none of the previous if statements are true. if !(starting%7==0 or starting%11==0 or starting%13==0) => echo starting

bash : Illegal number

When I run this bash script :
if [ [$EUID -ne 0] ]; then
echo "This script must be run as root" 1>&2
exit 1
else
printf " whathever "
exit 0
fi
I have this error :
./myScript: 15: [: Illegal number: [
Do you see any problem ?
You have syntax error in your if condition, use this if condition:
if [ "$EUID" -ne 0 ];
OR using [[ and ]]
if [[ "$EUID" -ne 0 ]];
You have syntax error in your if condition, use this if condition:
if [ "$EUID" -ne 0 ];
OR using [[ and ]]
if [[ "$EUID" -ne 0 ]];
If you use the KSH88+/Bash 3+ internal instruction [[, it's not necessary to use doubles quotes around the variables operands :
[ ~/test]$ [[ $var2 = "string with spaces" ]] && echo "OK" || echo "KO"
OK
Instead of the external command test or his fork [ :
[ ~/test]$ [ $var2 = "string with spaces" ] && echo "OK" || echo "KO"
bash: [: too many arguments
KO
[ ~/test]$ [ "$var2" = "string with spaces" ] && echo "OK" || echo "KO"
OK
Of course, you also have to choose the operators according to the type of operands :
[ ~/test]$ var1="01"
[ ~/test]$ [ "$var1" = "1" ] && echo "OK" || echo "KO"
KO
[ ~/test]$ [ "$var1" -eq "1" ] && echo "OK" || echo "KO"
OK
two suggestions apart from what everyone else has pointed out already.
rather than doing else [bunch of code because we are root] fi, just replace the else with fi. once you've tested for the failure condition you are concerned about and taken appropriate action, no need to continue to be within the body of the conditional.
$EUID is a bashism, if you would like to make this portable to shells such as ksh, replacing it with:
if [ $(id -u) -ne 0 ]; then echo "ur not root bro"; exit 1; fi
would be a good way to do it.
using
sudo bash shell_script.sh
instead of
sudo sh shell_script.sh
solved in my case.

Resources