LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-15-2012, 11:48 PM   #1
azurepenguin
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Rep: Reputation: Disabled
Elif Trouble


When I run the following script, I only get output based on the first two digits and not the entire number.

For instance, if I input 134, the script will return a result for 13.

I would be grateful for any thoughts, ideas or help.

#!bin/bash
clear
#
echo "Age Check"
echo
echo -n "Enter customer's age: "
read age
echo
echo -n "Customer's age: "
echo $age
echo
if [ ${age} '<' \13 ]
then
echo "Allowed ratings: G '&' PG"
elif [ ${age} '<' \17 ]
then
echo "Allowed ratings: G, PG '&' PG-13"
elif [ ${age} '<' \21 ]
then
echo "Allowed ratings: G, PG, PG-13 '&' R"
elif [ ${age} '>' \20 ]
then
echo "Allowed ratings: G, PG, PG-13, R '&' X"
elif [ ${age} '>' \121 ]
then
echo "DEAD"
fi
echo
echo "End Script"
 
Old 11-16-2012, 12:44 AM   #2
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
The less-than and greater-than characters are for string comparisons, not numeric comparisons. Try -gt and -lt:

Code:
if [ ${age} -lt 13 ]
 
Old 11-16-2012, 02:34 AM   #3
azurepenguin
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Unhappy

That didn't correct the error
 
Old 11-16-2012, 09:56 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Using this corrected version of the script and entering 134 gives the expected result of the greater than 20 case. The greater than 121 case is unreachable because any age greater than 121 will be caught by the greater than 20 case. To fix, swap the order of the 121 and 20 tests.
Code:
#!/bin/bash
#
echo "Age Check"
echo
echo -n "Enter customer's age: "
read age
echo
echo -n "Customer's age: "
echo $age
echo
if [ ${age} -lt 13 ]
then
echo "Allowed ratings: G '&' PG"
elif [ ${age} -lt 17 ]
then
echo "Allowed ratings: G, PG '&' PG-13"
elif [ ${age} -lt 21 ]
then
echo "Allowed ratings: G, PG, PG-13 '&' R"
elif [ ${age} -gt 20 ]
then
echo "Allowed ratings: G, PG, PG-13, R '&' X"
elif [ ${age} -gt 121 ]
then
echo "DEAD"
fi
echo
echo "End Script"
BTW the { and } in ${age} are not wrong but are not necessary.
 
1 members found this post helpful.
Old 11-17-2012, 02:12 AM   #5
azurepenguin
LQ Newbie
 
Registered: Nov 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Switching the arguments to put the -gt 121 first corrected the issue. Thank you.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if elif issue shellscript_03 Programming 7 11-14-2012 08:54 AM
scipt help elif bilbonix Linux - Newbie 6 11-03-2012 03:47 PM
[SOLVED] Elif Statement help UnixNewbie91 Linux - Newbie 6 04-28-2012 10:35 AM
Python shell help (using elif) danred Linux - General 8 10-20-2009 03:55 AM
elif??????????? embsupafly Programming 1 11-27-2002 03:48 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration