我已经使用命令IFS=', ' read -r -a array <<< "$(command)"
创建了一个数组
数组具有值:
abc001
abc002
abc003
我想遍历数组并在每个元素上运行curl
命令。
a)如果
curl
输出具有字符串Connected
,则curl
命令应该timeout
,并且应该退出for loop
。b)如果
curl
输出没有字符串Connected
,则curl
命令应将timeout
和for 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)
您能不能尝试,虽然没有经过测试,但是应该可以。
for element in "${array[@]}"
do
resp=$(curl -v http://"$element":8888)
if grep -q "Connected" "$resp"
then
echo resp
fi
done