Linux 如何通过grep响应计算出有多少呼叫超时?

Linux 如何通过grep响应计算出有多少呼叫超时?,linux,bash,shell,curl,grep,Linux,Bash,Shell,Curl,Grep,我有一个curl命令,它正在调用我们的一个服务,因此如果我的服务超时,它将返回JSON响应,如下所示: [{"results":{"response":null},"error":{"errorCode":1001,"message":"Service Timeout","status":"FAILURE"}}] 下面是我运行curl命令时,如果有任何超时,我将得到上面的响应 curl --header "Authorization: Bearer some token here" "http

我有一个curl命令,它正在调用我们的一个服务,因此如果我的服务超时,它将返回JSON响应,如下所示:

[{"results":{"response":null},"error":{"errorCode":1001,"message":"Service Timeout","status":"FAILURE"}}]
下面是我运行curl命令时,如果有任何超时,我将得到上面的响应

curl --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120";
我在for循环中运行了x次以上的curl命令。现在我想通过检查JSON响应中的
“message”
来查看有多少调用超时?我的意思是,如果我打了100万个电话,那么有多少个电话超时,超时的百分比是多少

所以对于调用curl命令的循环,我得到了下面一行,但每当我运行它时,它总是给出1作为答案,这是错误的。我的意思是,我可以看到很多电话都有时间,但每次都是1作为答案。我做错什么了吗

for ((i=1;i<=1000000;i++)); do
curl --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120"
done \
| grep -wcoE '"errorCode":1001'

只需使用
-s
标志使curl静音即可:

curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120"
或者将其错误输出重定向到
/dev/null
,如下所示:

curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120" 2>/dev/null
此外,您的计数版本也无法按预期工作。以下是您可以修复它的方法:

timedout_count=0
for ((i=1;i<=1000000;i++)); do
    curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120" | 
        grep -q '"errorCode":1001' && timedout_count=$(( timedout_count + 1 ))
done 

echo "Timed out $timedout_count times" 
timedout\u计数=0

对于((i=1;i只需使用
-s
标志使curl保持沉默:

curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120"
或者将其错误输出重定向到
/dev/null
,如下所示:

curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120" 2>/dev/null
此外,您的计数版本也无法按预期工作。以下是您可以修复它的方法:

timedout_count=0
for ((i=1;i<=1000000;i++)); do
    curl -s --header "Authorization: Bearer some token here" "http://localhost:8080/v2/line?&clientid=120" | 
        grep -q '"errorCode":1001' && timedout_count=$(( timedout_count + 1 ))
done 

echo "Timed out $timedout_count times" 
timedout\u计数=0

为了((i=1;我的意思是我的问题是-我看到很多timedout来电,但每次它都给出1作为答案,所以我猜我的命令有问题吗?很抱歉,我已经更新了它以澄清问题。更新了答案我的意思是-我看到很多timedout来电,但每次它给出1作为答案,所以我的命令有问题我想是吧?很抱歉搞混了,我已经更新了,让它更清楚了。更新了答案