0

I want to check speed of a website with a shell script. I'm using time format to get number result but I have problem with comparator. This is my script:

#!/bin/bash

TIMEFORMAT=%E
TOTAL=`time wget -pq --no-cache --delete-after -H https://www.netflix.com 2>&1`
echo $TOTAL

if [[ "$TOTAL" -gt 0 ]]; then
    echo 'YES'
else
    echo 'NO'
fi

Result was about greather than 2, but result was always no.

EDIT

This is result of script

2,119
NO

EDIT 2

Thanks, resolved this way

#!/bin/bash

start=$(date +%s)
time wget -pq --no-cache --delete-after -H https://www.netflix.com 2>&1
end=$(date +%s)
total=$(($end-$start))

echo "Elapsed Time: $(($total)) seconds"

if [[ $total -gt 1 ]]; then
    echo 'YES'
else
    echo 'NO'
fi
FireFoxII
  • 828
  • 4
  • 18
  • 31
  • 2
    See [BashFAQ #32](https://mywiki.wooledge.org/BashFAQ/032). – Charles Duffy Jul 25 '23 at 16:35
  • 3
    (that said, I wouldn't use `time` for what you're doing here in the first place, particularly if you only need integer granularity; easier to record `SECONDS` before and after the `wget` and compare). – Charles Duffy Jul 25 '23 at 16:36
  • (,,,and btw, note that POSIX advises you to use lower-case names for your own variables so you don't overwrite a reserved/built-in one by mistake; see https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that setting a regular shell variable overwrites any like-named environment variable; also, a lot of shell-built-in variables lose their special meaning and become normal variables if you assign to them directly). – Charles Duffy Jul 25 '23 at 16:37
  • (whereas if you need floating-point evaluation to deal with numbers that _aren't_ integers, bash's built-in math doesn't support it at all, so you need to use the techniques from [BashFAQ #22](https://mywiki.wooledge.org/BashFAQ/022)) – Charles Duffy Jul 25 '23 at 16:41
  • 1
    you should a) update the question to show the complete output from your script and b) review the contents of `TOTAL` (eg, add `typeset -p TOTAL` after the `TOTAL=$(...)` line); my guess is that `TOTAL` does not contain what you think it contains – markp-fuso Jul 25 '23 at 16:44
  • 1
    https://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl `-gt 0` - shell does not support floating point – KamilCuk Jul 25 '23 at 17:13
  • 1
    The value of `$TOTAL` contains a comma as the thousands separator. Arithmetic operations don't understand numbers like this, you need to remove it. – Barmar Jul 25 '23 at 17:14

1 Answers1

1

The time command is not really handy to use (for calculations) in a script because you will have to parse through all the output and convert numbers etc. You second version is a better approach and works.

But when you use variables in bash arithmetic functions $(($end-$start)) you don't have to prefix the variables with a $. Also, why do you use $(($total)) in that echo-statement? Just use $total.

And as mentioned in a comment I would also use the SHELL built-in $SECONDS variable instead of date (which will run as a subprocess). You now don't really need the time command but it gives you a nice overview of used kernel- and user-mode CPU time which you might want to see.

So the result would be something like this:

#!/bin/env bash

start=$SECONDS
time wget -pq --no-cache --delete-after -H https://www.netflix.com 2>&1
end=$SECONDS
total=$((end-start))

echo "Elapsed Time: $total seconds"

if [ $total -gt 1 ]
 then
   echo 'YES'
else
   echo 'NO'
fi
Peter Kuilboer
  • 249
  • 1
  • 5