The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If Statement Problem.. LinuxRacr Shell Programming and Scripting 2 02-26-2008 09:47 PM
problem with if statement equality cleansing_flame Shell Programming and Scripting 1 02-12-2008 07:57 AM
Case statement problem gzs553 UNIX for Advanced & Expert Users 6 11-14-2006 01:24 PM
problem with an IF statement hcclnoodles Shell Programming and Scripting 2 04-17-2003 07:53 AM
if statement problem coughlin74 UNIX for Dummies Questions & Answers 1 09-27-2001 01:31 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-04-2005
Registered User
 

Join Date: Nov 2005
Posts: 5
if statement problem

hi all. i just have a very small problem. i have a menu of 7 choices. i want an if statement so that if the user chooses anything except inside the 1 to 7 range, i can handle the error for it.

i tried this:

if [ $choice -ne [1-7] ]
then
.......
fi

(but it dont work)

...any suggestions?

thanks all in advance
Reply With Quote
Forum Sponsor
  #2  
Old 12-04-2005
Registered User
 

Join Date: Sep 2005
Posts: 45
use :-

if [[ $choice -lt 1 || $choice -gt 7 ]]
then
.....
fi

cheers
Reply With Quote
  #3  
Old 12-04-2005
Registered User
 

Join Date: Dec 2005
Location: Windsor, Ont Canada
Posts: 13
LHS of "if" must be quoted.

Hi djt !

One of the reasons why your script does not work is that the left side MUST be in quotes, eg:
Code:
if [ "$number" = "1" ]; then
    echo "Number equals 1"
else
    echo "Number does not equal 1"
fi
Refer to this link for excellent explanation: http://linuxcommand.org/wss0100.php

Hope that was helpful
Regards
Graham
Reply With Quote
  #4  
Old 12-04-2005
Registered User
 

Join Date: Dec 2005
Location: Windsor, Ont Canada
Posts: 13
Testing a range of values

Hi again djt !!

Try this one, this idea addresses the RH-side of the equation, namely: ranges of values:
Code:
read character
case $character in
            # Check for letters
    [a-z] | [A-Z] ) echo "Alpha range i[a-z]: You typed the letter $character"
            ;;
            # Check for digits
    [0-9] )     echo "Numeric range: 0-9: You typed the digit $character"
            ;;
            # Check for anything else
    * )         echo "You did not type a letter or a digit"
esac
Try this link for further explanations: http://linuxcommand.org/wss0120.php

Again, I hope this is usefull!
Rergards
GrahamB
Reply With Quote
  #5  
Old 12-04-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,256
"-ne" is an operator to compare integer values (for being not equal). "[-7]" is a string, not an Int, this is why the test failed.

How to solve your task better has already been explained.

bakunin
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0