views:

490

answers:

3

The shellscript shown below will show a warning if the page takes more than 6 seconds to load. The problem is that the myduration variable is not an integer. How do I convert it to integer?

myduration=$(curl http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}) > /dev/null ; [[ $myduration -gt 1 ]] && echo "`date +'%y%m%d%H%M%S'` took more than 6 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1 " >> /home/shantanu/speed_report.txt
+1  A: 

It's not entirely clear, but I think you're asking how to convert a floating-point value (myduration) to an integer in bash. Something like this may help you, depending on which way you want to round your number.

#!/bin/bash

floor_val=
ceil_val=

function floor() {
    float_in=$1
    floor_val=${float_in/.*}
}

function ceiling() {
    float_in=$1
    ceil_val=${float_in/.*}
    ceil_val=$((ceil_val+1))
}


float_val=$1
echo Passed in: $float_val
floor $float_val
ceiling $float_val

echo Result of floor: $floor_val
echo Result of ceiling: $ceil_val

Example usage:

$ ./int.sh 12.345
Passed in: 12.345
Result of floor: 12
Result of ceiling: 13
ire_and_curses
+1  A: 

Eliminate page contents from the variable:

When I tried your command, myduration contained the HTML contents of the page at the URL I used in my test plus the time value. By adding -s to suppress the progress bar and adding -o /dev/null to the options for curl, I was able to remove the redirect to /dev/null and have only the time saved in myduration.

Since the value of myduration is likely to be short, you can use the technique ire_and_curses shows which will often yield zero as its result which would be less than the 1 you are testing for (note that your log message says "6 seconds", though).

Finer resolution:

If you'd like to have a finer resolution test, you can multiply myduration by 1000 using a technique like this:

mult1000 () {
    local floor=${1%.*}
    [[ $floor = "0" ]] && floor=''
    local frac='0000'
    [[ $floor != $1 ]] && frac=${1#*.}$frac
    echo ${floor}${frac:0:3}
}

Edit: This version of mult1000 properly handles values such as "0.234", "1", "2.", "3.5" and "6.789". For values with more than three decimal places, the extra digits are truncated without rounding regardless of the value ("1.1119" becomes "1.111").

Your script with the changes I mentioned above and using mult1000 (with my own example time):

myduration=$(curl -s -o /dev/null http://192.168.50.1/mantisbt/view.php?id=1 -w %{time_total}); [[ $(mult1000 $myduration) -gt 3500 ]] && echo "`date +'%y%m%d%H%M%S'` took more than 3.5 seconds to load the page http://192.168.50.1/mantisbt/view.php?id=1 " >> /home/shantanu/speed_report.txt

Here it is broken into multiple lines (and simplified) to make it more readable here in this answer:

myduration=$(curl -s -o /dev/null http://example.com -w %{time_total})
[[ $(mult1000 $myduration) -gt 3500 ]] &&
  echo "It took more than 3.5 seconds to load thttp://example.com" >> report.txt
Dennis Williamson
That mult1000 function is severely misleading: $(mult1000 1.2) != "1200".
glenn jackman
It could probably use a better name, but it does work with `mult1000 1.200` as I indicated in my remarks about trailing zeros. If that is needed, it's easy enough to make it work with your example.
Dennis Williamson
My simplistic function also doesn't properly handle values less than one, such as "0.234"
Dennis Williamson
I've edited my answer to include an improved function.
Dennis Williamson
A: 

Assuming $myduration is a decimal or integer

$ myduration=6.5
$ myduration=$( printf "%.0f" $myduration )
$ echo $myduration
6
Shin