0

I would like to have this script in a way that I can use it somewhat like this:

cat websites.txt | latency | sort -n

Right now I'm doing:

test.sh

# Sort websites by their response time

# get the list of websites
websites=$(cat websites.txt)

# loop through the websites
for website in $websites
do
    # get the response time of the website
    response_time=$(curl -o /dev/null -s -w %{time_total} $website)

    # print the response time
    echo "Response Time for $website is $response_time seconds"
done

I want to use it to find the fediverse instances with the lowest latencies.

1 Answer 1

1
#!/bin/bash


fun1 () {
# get the list of websites
websites=$(cat websites.txt)

# loop through the websites
for website in $websites
do
    # get the response time of the website
    response_time=`curl -o /dev/null -s -w %{time_total} $website`
    # print the response time
    echo "Response Time for $website is $response_time seconds"
done
}

fun1 | sort -k6 -n

So what this does it make your whole current script into a function and then pipe it through sort with the 6-th key as an element. The key (-k) option takes a field/line number.

You must log in to answer this question.