如何在for循环中对curl执行grep?

问题描述 投票:0回答:1

我已经使用命令IFS=', ' read -r -a array <<< "$(command)"创建了一个数组

数组具有值:

abc001
abc002
abc003

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

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

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

我已经编写了以下代码。

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)
linux bash shell curl rhel
1个回答
0
投票

您能不能尝试,虽然没有经过测试,但是应该可以。

for element in "${array[@]}"
do
 resp=$(curl -v http://"$element":8888)
 if grep -q "Connected" "$resp"
 then
      echo resp
 fi
done
© www.soinside.com 2019 - 2024. All rights reserved.