Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    8

    Smile Help me debug this Bash script


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

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    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:
    Code:
    number=$(printf "%.0f" 180.00)
    echo number is $number
    if [ $time -gt $number ]; then
      echo true
    fi
    If you want to retain the precision, then you could do something like:
    Code:
    printf "$time\n180.00\n"|sort -n|head -n1
    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.

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    8
    Thanks atreyu. Your code works flawlessly. I use the first one

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •