如何在for循环中的curl上执行grep? [英] How to perform grep on curl within for loop?

查看:607
本文介绍了如何在for循环中的curl上执行grep?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用命令IFS=', ' read -r -a array <<< "$(command)"

该数组具有以下值:

abc001
abc002
abc003

我想遍历数组并在每个元素上运行curl命令.

I want to loop through the array and run a curl command on each element.

a)如果curl输出具有字符串Connected,则curl命令 应该timeout并且for loop应该退出.

a) If curl output has string Connected then the curl command should timeout and the for loop should exit out.

b)如果curl输出没有字符串Connected,则 curl命令应timeoutfor loop应该移至下一个 元素.

b) If the curl output is not having string Connected then the curl command should timeout and for loop should move to next element.

我写了下面的代码.

for element in "${array[@]}"
do
 resp=$(curl -v http://"$element":8888)
 echo resp
done

我得到以下输出:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to abc001 port 8888 (#0)
*   Trying 10.10.10.10...
* Connected to abc001 port 8888 (#0)

推荐答案

请问您可以尝试以下方法,虽然应该可以使用,但未经过测试.

Could you please try following, not tested though should work but.

for element in "${array[@]}"
do
 resp=$(curl -v http://"$element":8888)
 if grep -q "Connected" "$resp"
 then
      echo resp
 fi
done

OR

for element in "${array[@]}"
do
 curl http://"$element":8888 -s -f -o /dev/null
 if [[ $? -eq 0 ]]
 then
      echo "url $element is working."
 fi
done

这篇关于如何在for循环中的curl上执行grep?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆