LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-20-2005, 10:29 PM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206

Rep: Reputation: 30
bash statement


hi


i know i can do:


Code:
X=20
Y=30
Z=40
if [ $Z -lt $X ]; then
 echo "$Z est plus petit que $X";
elif [ $Z -lt $Y ]; then
 echo "$Z est plus petit que $Y";
else
 echo "$Z est plus petit que 50";
fi
i tried another way to do that but they seem to have problem after le last else

Code:
X=20
Y=30
Z=40
if [ $Z -lt $X ]; then
 echo "$Z is smaller $X";
else
 if [ $Z -lt $Y ]; then
  echo "$Z is smaller $Y";
 fi
else
 echo "$Z is smaller 50";
fi
any idea?

thanks
 
Old 03-20-2005, 10:58 PM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Try this...
Code:
#!/bin/bash
 
X=20
Y=30
Z=40
if [ $Z -lt $X ]; then
   echo "#1 $Z is smaller $X";
elif [ $Z -lt $Y ]; then
   echo "#2 $Z is smaller $Y";
else
   echo "#3 $Z is smaller 50";
fi
 
Old 03-20-2005, 11:13 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
In your second version, you have a problem because there are two consecutive "else" statements.
Code:
if [ $Z -lt $X ]; then
 ...
else
 ...
else
 ...
fi
You can't do that, because the shell can't figure out what you mean. Which else is the "right" else to take if the original if statement is false?

You can nest if statements (which is what it looks like you were trying to do), but you closed the whole if-structure too early. Something like this:
Code:
if [ $Z -lt $X ]; then
 ...
else
  if [ $Z -lt $Y ]; then
   ...
  else
   ...
  fi
fi
 
  


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
Bash assignment - using a grep inside an if statement lowpro2k3 Programming 4 03-25-2005 06:24 PM
bash if statement question xscousr Programming 3 09-02-2003 12:58 PM
Bash select statement and spaces meshcurrent Linux - General 2 04-09-2003 10:35 AM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 03:03 PM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 01:58 PM


All times are GMT -5. The time now is 01:03 AM.

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