Unix Shell - string comparison problem in bash 3.00

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > January 2006 > string comparison problem in bash 3.00





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 string comparison problem in bash 3.00
Chris

2006-01-29, 9:31 pm

My script takes user input and compare it with some values. It works
fine when it compares against 0.00 or 10 or 100. But if the user input
is something like "-0.009" it gives error - "integer expression
expected in line 7 and line 9" where it ought to print the number like
-

your number is -0.009.

Here's the code -

Code:

if [ "$num" = 0.00 ] || [ "$num" -le 10 ]; then # this is line 7
echo "your number is $num"
elif [ "$num" -ge "100" ]; then # this is line 9
echo "your number is greater than 100"
else
echo "your number is $num"

Wil Cooley

2006-01-29, 9:31 pm

On Wed, 25 Jan 2006 15:34:24 -0800, Chris wrote:

> My script takes user input and compare it with some values. It works fine
> when it compares against 0.00 or 10 or 100. But if the user input is
> something like "-0.009" it gives error - "integer expression expected in
> line 7 and line 9" where it ought to print the number like -
>
> your number is -0.009.


The answer is so close you probably cannot see it:
"integer expression expected"

Is -0.009 an integer?

Wil

Kurt Swanson

2006-01-29, 9:31 pm

"Chris" <atstake@gmail.com> writes:
> My script takes user input and compare it with some values. It works
> fine when it compares against 0.00 or 10 or 100. But if the user input
> is something like "-0.009" it gives error - "integer expression
> expected in line 7 and line 9" where it ought to print the number like
> -


Floating point arithmetic is not supported by the shell.
--
© 2005 Kurt Swanson AB
Chris F.A. Johnson

2006-01-29, 9:31 pm

On 2006-01-25, Chris wrote:
> My script takes user input and compare it with some values. It works
> fine when it compares against 0.00 or 10 or 100. But if the user input
> is something like "-0.009" it gives error - "integer expression
> expected in line 7 and line 9" where it ought to print the number like
> -
>
> your number is -0.009.
>
> Here's the code -
>
> Code:
>
> if [ "$num" = 0.00 ] || [ "$num" -le 10 ]; then # this is line 7
> echo "your number is $num"
> elif [ "$num" -ge "100" ]; then # this is line 9
> echo "your number is greater than 100"
> else
> echo "your number is $num"


Read the error message. It tells you that an integer expression is
expected, not a decimal fraction. Bash does not use decimal
fractions, only integers.

--
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
Stephane Chazelas

2006-01-29, 9:31 pm

On 25 Jan 2006 15:34:24 -0800, Chris wrote:
> My script takes user input and compare it with some values. It works
> fine when it compares against 0.00 or 10 or 100. But if the user input
> is something like "-0.009" it gives error - "integer expression
> expected in line 7 and line 9" where it ought to print the number like
> -
>
> your number is -0.009.
>
> Here's the code -
>
> Code:
>
> if [ "$num" = 0.00 ] || [ "$num" -le 10 ]; then # this is line 7
> echo "your number is $num"
> elif [ "$num" -ge "100" ]; then # this is line 9
> echo "your number is greater than 100"
> else
> echo "your number is $num"


Only zsh and ksh93 support floating point arithmetics. Note that
[ ... = ... ] is string comparison (0 is different from 0.00).

To do floating point arithmetics portably, use awk.

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com