The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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-27-2008 12:47 AM
problem with if statement equality cleansing_flame Shell Programming and Scripting 1 02-12-2008 10:57 AM
Case statement problem gzs553 UNIX for Advanced & Expert Users 6 11-14-2006 04:24 PM
problem with an IF statement hcclnoodles Shell Programming and Scripting 2 04-17-2003 10:53 AM
if statement problem coughlin74 UNIX for Dummies Questions & Answers 1 09-27-2001 04:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-04-2005
djt0506 djt0506 is offline
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
  #2 (permalink)  
Old 12-04-2005
grasper grasper is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 45
use :-

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

cheers
  #3 (permalink)  
Old 12-04-2005
grahamb grahamb is offline
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
  #4 (permalink)  
Old 12-04-2005
grahamb grahamb is offline
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
  #5 (permalink)  
Old 12-04-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,617
"-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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 07:48 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0