按响应时间对网站进行排序

按响应时间对网站进行排序

我希望以某种方式使用该脚本,如下所示:

cat websites.txt | latency | sort -n

现在我正在做:

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

我想用它来查找延迟最低的联邦实例。

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

sort那么它的作用是将整个当前脚本变成一个函数,然后将第 6 个键作为元素通过管道传递。 key( )选项-k采用字段/行号。

相关内容