The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM
Home Forums Register Rules & FAQ Members List Arcade Search Today's Posts Mark Forums Read


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 !!


Other UNIX.COM Threads You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
If Statement Problem.. LinuxRacr Shell Programming and Scripting 2 02-26-2008 08:47 PM
problem with if statement equality cleansing_flame Shell Programming and Scripting 1 02-12-2008 06:57 AM
Case statement problem gzs553 UNIX for Advanced & Expert Users 6 11-14-2006 12: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 (permalink)  
Old 12-04-2005
Registered User
 

Join Date: Nov 2005
Posts: 5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 12-04-2005
Registered User
 

Join Date: Sep 2005
Posts: 45
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
use :-

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

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

Join Date: Dec 2005
Location: Windsor, Ont Canada
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 12-04-2005
Registered User
 

Join Date: Dec 2005
Location: Windsor, Ont Canada
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 12-04-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 916
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiReddit! Stumble this Post!Spurl this Post!
"-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 UNIX.COM
Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:42 PM.


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

Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102