Results 1 to 3 of 3
Here's the code
Code:
#!/bin/sh
time=10
if [ $time \> 180.00 ]; then echo true; fi
when time=10, it evaluates to false
when time=20, it evaluates to true
The condition ...
- 10-11-2011 #1Just Joined!
- Join Date
- Oct 2011
- Posts
- 7
Help me debug this Bash script
Here's the code
when time=10, it evaluates to falseCode:#!/bin/sh time=10 if [ $time \> 180.00 ]; then echo true; fi
when time=20, it evaluates to true
The condition takes the first characters from both arguments and compare them which is not how it should be.
Note that I want both values to be compared as Strings not Integers because I'm going to implement it where $time takes value from an output formatted as String.
Thanks a lot
Last edited by aizul357; 10-11-2011 at 04:10 AM. Reason: corrections
- 10-11-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
For bash, you want to use -gt, not the > symbol. However, bash (at least my version of it) does not handle floating point numbers, so you'll have to either convert it first, or do it differently.
If you can convert it (i.e., round it to the nearest whole number), you could do:
If you want to retain the precision, then you could do something like:Code:number=$(printf "%.0f" 180.00) echo number is $number if [ $time -gt $number ]; then echo true fi
That would sort the two numbers in a column numerically, and return the first line, that is, the lower number. Of course, you'd need to check first that the two numbers aren't equal first.Code:printf "$time\n180.00\n"|sort -n|head -n1
- 10-11-2011 #3Just Joined!
- Join Date
- Oct 2011
- Posts
- 7
Thanks atreyu. Your code works flawlessly. I use the first one


