LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script -- if then else elif problems (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-if-then-else-elif-problems-4175486915/)

Momof5.5 12-04-2013 10:43 PM

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

evo2 12-04-2013 11:35 PM

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."
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

Now it should be pretty obvious that you are missing a fi.
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."
echo "Please hit <Enter> after each entry."
read num1
read num2
if [ "$num1" -eq "$num2" ]
then
  echo "Your numbers are equal."
fi

echo "Hit <Enter> to continue or q to exit."
read choice
if [ "$choice" = "q" ]
then
  exit 2
fi

echo "Continuing..."

However this doesn't tell you which number is greater, only if they are equal.

HTH,

Evo2.

JJJCR 12-04-2013 11:36 PM

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:


8.2.2. Prompting for user input

The following example shows how you can use prompts to explain what the user should enter.


michel ~/test> cat friends.sh
#!/bin/bash

# This is a program that keeps your address book up to date.

friends="/var/tmp/michel/friends"

echo "Hello, "$USER". This script will register you in Michel's friends database."

echo -n "Enter your name and press [ENTER]: "
read name
echo -n "Enter your gender and press [ENTER]: "
read -n 1 gender
echo

grep -i "$name" "$friends"

if [ $? == 0 ]; then
echo "You are already registered, quitting."
exit 1
elif [ "$gender" == "m" ]; then
echo "You are added to Michel's friends list."
exit 1
else
echo -n "How old are you? "
read age
if [ $age -lt 25 ]; then
echo -n "Which colour of hair do you have? "
read colour
echo "$name $age $colour" >> "$friends"
echo "You are added to Michel's friends list. Thank you so much!"
else
echo "You are added to Michel's friends list."
exit 1
fi
fi

michel ~/test> cp friends.sh /var/tmp; cd /var/tmp

michel ~/test> touch friends; chmod a+w friends

michel ~/test> friends.sh
Hello, michel. This script will register you in Michel's friends database.
Enter your name and press [ENTER]: michel
Enter your gender and press [ENTER] :m
You are added to Michel's friends list.

michel ~/test> cat friends

Momof5.5 12-05-2013 01:25 AM

Thank you! This helped.

Quote:

Originally Posted by evo2 (Post 5075721)
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."
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

Now it should be pretty obvious that you are missing a fi.
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."
echo "Please hit <Enter> after each entry."
read num1
read num2
if [ "$num1" -eq "$num2" ]
then
  echo "Your numbers are equal."
fi

echo "Hit <Enter> to continue or q to exit."
read choice
if [ "$choice" = "q" ]
then
  exit 2
fi

echo "Continuing..."

However this doesn't tell you which number is greater, only if they are equal.

HTH,

Evo2.


Momof5.5 12-05-2013 01:28 AM

Yes, this is a homework. But you did help me. Thank you! I'll fix it now so the q will work.

Quote:

Originally Posted by JJJCR (Post 5075723)
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


grail 12-05-2013 09:09 AM

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.