问题
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" ];
回答1:
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.
回答2:
You should be using the little-appreciated select command here. Replace the : command with the appropriate action.
choices=(
'ALL TEH THINGZ!'
'Find the enemy postions and villages'
'Set the firewalls to my liking'
'Confuse the enemy'
'Deploy the spies!'
'DISABLED Run the TOP SECRET STUFF! (Note password is required)'
'Burn the bridges! (Cautian: Last Resort)'
'Install/Update Software (locally)'
'Install/Update Software (remotely)'
'Update and add new Kali tools'
'I am done with you for now'
)
PS3='Select an operation for me to do: '
echo 'Hello, What would you like me to do?'
select choice in "${choices[@]}"; do
case $choice in
'ALL TEH THINGZ!')
:
;;
'Find the enemy postions and villages')
:
;;
'Set the firewalls to my liking')
:
;;
'Confuse the enemy')
:
;;
'Deploy the spies!')
:
;;
'DISABLED Run the TOP SECRET STUFF! (Note password is required)')
:
;;
'Burn the bridges! (Cautian: Last Resort)')
:
;;
'Install/Update Software (locally)')
:
;;
'Install/Update Software (remotely)')
:
;;
'Update and add new Kali tools')
:
;;
'I am done with you for now')
break
;;
esac
done
You might want to look into using an associative array to construct a dispatch table.
回答3:
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
回答4:
-eq works on integers only.
$input and 0 should not be in quotes.
To convert $input to integer, use $((input)) instead.
You seem to be missing a done somewhere.
来源:https://stackoverflow.com/questions/26111098/integer-expression-expected-error