LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-04-2013, 11:43 PM   #1
Momof5.5
LQ Newbie
 
Registered: Dec 2013
Location: Vancouver, WA
Distribution: Red Hat
Posts: 4

Rep: Reputation: Disabled
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

Last edited by Momof5.5; 12-04-2013 at 11:46 PM. Reason: needed to quote script
 
Old 12-05-2013, 12:35 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
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.
 
1 members found this post helpful.
Old 12-05-2013, 12:36 AM   #3
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,102

Rep: Reputation: 181Reputation: 181
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
 
1 members found this post helpful.
Old 12-05-2013, 02:25 AM   #4
Momof5.5
LQ Newbie
 
Registered: Dec 2013
Location: Vancouver, WA
Distribution: Red Hat
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thank you! This helped.

Quote:
Originally Posted by evo2 View Post
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.
 
Old 12-05-2013, 02:28 AM   #5
Momof5.5
LQ Newbie
 
Registered: Dec 2013
Location: Vancouver, WA
Distribution: Red Hat
Posts: 4

Original Poster
Rep: Reputation: Disabled
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 View Post
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
 
Old 12-05-2013, 10:09 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
You may also wish to have a look at round bracket expressions for your arithmetic :- http://tldp.org/LDP/abs/html/arithexp.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
if...elif...else BASH ladder problems bdrockwell Linux - Newbie 3 11-17-2010 12:38 AM
Bash script if elif problem assyrian1 Linux - Software 2 12-09-2008 10:00 AM
BASH if/elif variable problem GNUJoshua Programming 6 07-15-2008 08:01 AM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 11:28 AM
bash scripting question - elif ?? xscousr Programming 2 08-12-2003 10:56 AM


All times are GMT -5. The time now is 08:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration