|
Prev: common history in bash
Next: How to unset $1?
From: Chris on 25 Jan 2006 18:34 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"
From: Wil Cooley on 25 Jan 2006 18:46 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
From: Chris F.A. Johnson on 25 Jan 2006 18:57 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
From: Stephane Chazelas on 26 Jan 2006 03:20 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
|
Pages: 1 Prev: common history in bash Next: How to unset $1? |