LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash statement (http://www.linuxquestions.org/questions/programming-9/bash-statement-304102/)

os2 03-20-2005 10:29 PM

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

homey 03-20-2005 10:58 PM

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


Dark_Helmet 03-20-2005 11:13 PM

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



All times are GMT -5. The time now is 08:12 PM.