bash script -- if then else elif problems
I am trying to get a script to do the following:
Get input of 2 numbers from user. Compare the two numbers and state which one is greater. Then give the user the option to hit <Enter> to continue and q to quit the program. I can get the input from user and compare the numbers, but how do I get the script to recognize the letter q and then quit the program? Here is a portion of what I have: echo "Please type in 2 single digits you would like to compare." echo "Please hit <Enter> after each entry." read num1 read num2 if [ "$num1" -eq "$num2" ] then echo "Your numbers are equal." echo "Hit <Enter> to continue or q to exit." read choice if [ "$choice" -eq "q" ] then exit 2 else exit 1 fi |
Hi,
your nesting of if statements is wrong for what you are trying to do. Here it is indented correctly: Code:
echo "Please type in 2 single digits you would like to compare." Another problem is that you are using the numerical comparison "-eq" instead of the string comparison "=". So, presumably you want something like: Code:
echo "Please type in 2 single digits you would like to compare." HTH, Evo2. |
sounds like a homework..
have you tried running the script? what's the error? check out this link, it will give you an idea: http://bash.cyberciti.biz/guide/If..else..fi script below from this link: http://tldp.org/LDP/Bash-Beginners-G...ect_08_02.html Quote:
|
Thank you! This helped.
Quote:
|
Yes, this is a homework. But you did help me. Thank you! I'll fix it now so the q will work.
Quote:
|
You may also wish to have a look at round bracket expressions for your arithmetic :- http://tldp.org/LDP/abs/html/arithexp.html
|
All times are GMT -5. The time now is 11:36 PM. |