Unix Shell - comparison expression

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > comparison expression





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author comparison expression
PRadyut

2006-01-09, 6:02 pm

Hi Group,

I'm running Fedora Core 3 with release id 2.6.9-1.667
I have a code that gives a error as
"too many arguments" in the line if [ $per -le 50 -a $ per -lt 60 ]

My code

----------------------Code----------------------------------
echo "enter marks in five subjects"
read m1 m2 m3 m4 m5

per=`expr \( $m1 + $m2 + $m3 + $m4 + $m5 \) / 5`

if [ $per -ge 60 ]
then
echo First division
fi

if [ $per -le 50 -a $ per -lt 60 ]
then
echo Second Division
fi
----------------------------EOF------------------------------

Please explain the output of the code and suggest correcting the code

Thanking You
Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India

Benjamin Schieder

2006-01-09, 6:02 pm

PRadyut wrote:
> Hi Group,
>
> I'm running Fedora Core 3 with release id 2.6.9-1.667
> I have a code that gives a error as
> "too many arguments" in the line if [ $per -le 50 -a $ per -lt 60 ]

^
Space where not allowed.

Greetings,
Benjamin

--
The Nethack IdleRPG! Idle to your favorite Nethack messages!
http://pallas.crash-override.net/nethackidle/
Chris F.A. Johnson

2006-01-09, 6:02 pm

On 2006-01-09, PRadyut wrote:
> Hi Group,
>
> I'm running Fedora Core 3 with release id 2.6.9-1.667
> I have a code that gives a error as
> "too many arguments" in the line if [ $per -le 50 -a $ per -lt 60 ]
>
> My code
>
> ----------------------Code----------------------------------
> echo "enter marks in five subjects"
> read m1 m2 m3 m4 m5
>
> per=`expr \( $m1 + $m2 + $m3 + $m4 + $m5 \) / 5`


You do not need expr to do arithmetic in a POSIX shell such as
bash:

per=$(( ($m1 + $m2 + $m3 + $m4 + $m5) / 5 ))

> if [ $per -ge 60 ]
> then
> echo First division
> fi
>
> if [ $per -le 50 -a $ per -lt 60 ]


You have an extra space; it should be:

if [ $per -le 50 -a $per -lt 60 ]

> then
> echo Second Division
> fi
> ----------------------------EOF------------------------------



--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com