If else


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If else
# 1  
Old 03-30-2013
If else

Dear All,
Hope you doing well!
Code:
#!/bin/sh
 
echo " enter 1st integer:"
read int1
echo "enter 2nd integer:"
read int2
 
if($int1 -eq $int2)
 then
  echo " numbers are equal"
elif ($int1 -gt $int2)
 then 
echo "integer 1 is greater"
 
else
echo " integer 2 is greater"
 
fi

------
output:
for every input last else part is coming up.
----
please help me understand this.

Last edited by Scrutinizer; 03-31-2013 at 03:07 PM.. Reason: code tags
# 2  
Old 03-30-2013
Please use code tags as required by forum rules!

Parentheses usually group commands to a list which is executed in a subshell. For testing purposes, use the test command, to which [ is a synonym.
man [:
. . .
Quote:
NOTE: your shell may have its own version of test and/or [, which usually supersedes the version described here. Please refer to
your shell's documentation for details about the options it supports.
# 3  
Old 03-30-2013
Try something like this:
Code:
echo -n "enter 1st integer: "
read int1
echo -n "enter 2nd integer: "
read int2

if [ $int1 -eq $int2 ]; then
  echo "numbers are equal"
elif [ $int1 -gt $int2 ]; then
  echo "integer 1 is greater"
else
  echo "integer 2 is greater"
fi

# 4  
Old 03-31-2013
Hi shads...

Apologies for any typos...

After extracting, changing the permissions, and running your code this is the result:-

Code:
Last login: Sun Mar 31 11:55:16 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ ./shad.sh
 enter 1st integer:
12
enter 2nd integer:
34
./shad.sh: line 8: 12: command not found
./shad.sh: line 11: 12: command not found
 integer 2 is greater
Barrys-MacBook-Pro:~ barrywalker$

So before trying anything research what error report pertains to...

Line 8 reveals:-
Code:
if($int1 -eq $int2)

Therefore somewhere in that line is your first error...

"if" is the command wanted and is adjoined to a parenthesis, (read RudiC's comment). Next try a space between the if and the parentesis. The same error occurs like this "elif" command at Line 11:-
Code:
elif ($int1 -gt $int2)

So as the same error occurs in both the either your _brackets_ are wrong or the parts inside the _brackets_ are wrong.

Next modification after adding spaces between the commands and _brackets_ is to add spaces inside the _brackets_ like this...
Code:
elif ( $int1 -gt $int2 )

If this does not work then make an assumption that the part inside the parentheses is OK and change your parentheses first to, say, these:-

Code:
elif [ $int1 -gt $int2 ]

Retry and VOILA, it works, (your code cleaned up):-
Code:
#!/bin/sh

echo "enter 1st integer:"
read int1
echo "enter 2nd integer:"
read int2
if [ $int1 -eq $int2 ]
then
echo "numbers are equal"
elif [ $int1 -gt $int2 ]
then 
echo "integer 1 is greater"
else
echo "integer 2 is greater"
fi

Code:
Last login: Sun Mar 31 11:56:33 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ ./if_else.sh
enter 1st integer:
23
enter 2nd integer:
-1
integer 1 is greater
Barrys-MacBook-Pro:~ barrywalker$

By doing a little process of elimination you woild have gotten the result by yourself. It just needs someone to show that process of elimination.

I hope this has helped...
# 5  
Old 03-31-2013
Thank you All!

thank you wisecracker for the detailed explaination. you rock.

god bless!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question