如何在for循环中的curl上执行grep? [英] How to perform grep on curl within for loop?
本文介绍了如何在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 stringConnected
then thecurl
command shouldtimeout
and thefor loop
should exit out.
b)如果curl
输出没有字符串Connected
,则
curl
命令应timeout
和for 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屋!
查看全文