Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[: : integer expression expected Error

I am having issues with this script and don't know exactly what is happening.

#Main Menu
clear
while [ "$input" -eq "0" ];
do
clear
#$input == 0
echo "Hello, What would you like me to do?"
echo ""
echo "1 = ALL TEH THINGZ!"
echo "2 = Find the enemy postions and villages"
echo "3 = Set the firewalls to my liking"
echo "4 = Confuse the enemy"
echo "5 = Deploy the spies!"
echo "6 = DISABLED Run the TOP SECRET STUFF! (Note password is required)"
echo "81 = Burn the bridges! (Cautian: Last Resort)"
echo "96 = Install/Update Software (locally)"
echo "97 = Install/Update Software (remotely)"
echo "98 = Update and add new Kali tools"
echo "99 = I am done with you for now"
read -p "Select an operation for me to do: " $input

I know I have the fi and end right but I am getting an error at the while [ "$input" -eq "0" ];

like image 995
Jeremiah Payne Avatar asked Jun 23 '26 13:06

Jeremiah Payne


2 Answers

You'll get that message if $input is not a legal numeric expression; for instance, if it's the empty string. If you want to loop while it's 0, you should start out by setting it to 0.

input=0
while [ "$input" -eq 0 ]; do 
...
done

Although you've tagged this bash; if you're actually using bash, you can also do this:

while (( input == 0 )); do
 ...
done

Note that shell is not Perl; the $ is not part of the variable name. It's better to think of $ as an operator that means "get the value of". The variable is input; you assign it with input=; you read into it with read input; etc. You only use $input when you want to get the value stored in the variable.

So, for instance, it doesn't make sense to do $input= or read $input unless the value stored in the input variable is a string which is the name of some other variable that you want to set the value of.

like image 171
Mark Reed Avatar answered Jun 25 '26 04:06

Mark Reed


The problem is at

while [ "$input" -eq "0" ]

and you have not used the while loop closing statement done, too.

As you are comparing two strings, you should use

while [[ $input == 0 ]]; do <statements>; done

or

while [ $input = 0 ]; do <statements>; done

So your code will be:

#Main Menu
clear
echo -n "Select an operation for me to do: " 
read input

while [ $input = 0 ]
do
        clear
done

#$input == 0
echo "Hello, What would you like me to do?"
echo ""
echo "1 = ALL THE THINGZ!"
echo "2 = Find the enemy postions and villages"
echo "3 = Set the firewalls to my liking"
echo "4 = Confuse the enemy"
echo "5 = Deploy the spies!"
echo "6 = DISABLED Run the TOP SECRET STUFF! (Note password is required)"
echo "81 = Burn the bridges! (Cautian: Last Resort)"
echo "96 = Install/Update Software (locally)"
echo "97 = Install/Update Software (remotely)"
echo "98 = Update and add new Kali tools"
echo "99 = I am done with you for now"

exit 0
like image 26
Catalyst_om Avatar answered Jun 25 '26 04:06

Catalyst_om