Bash:如果"curl"无法连接,则跳到循环中的下一步



我有一个bash脚本,它遍历IP地址列表,并尝试从中curl信息。然而,有时无法联系到服务器(curl: (28) Failed to connect to XX.XX.XXX.XX port YYYY: Connection timed out(,我的脚本会无限地尝试联系他。我如何更改我的代码,以确保如果我遇到这种失败的连接,我的bash脚本将转到列表上的下一个IP?我厌倦了一些if-else语句和continue语句,但这并没有奏效。

#!/bin/bash
filename='ips.txt'
n=1
while read line;
do
# reading each line
echo "Line No. $n : $line"
curl https://someaddress | jq -r '.[].foo.value' | xargs -I % curl http://$line:YYYY/foo -o $line.json
if [ $? -ne 0 ]
then
continue
fi
n=$((n+1))
echo -en "$i        r"
done < $filename

试试这个:

#!/bin/bash
filename='ips.txt'
n=1
while read line;
do
# reading each line
echo "Line No. $n : $line"

curl https://someaddress | jq -r '.[].foo.value' | xargs -I % curl http://$line:YYYY/foo -o $line.json

if [ $? -ne 0 ]
then
echo "Download failed! Try again with next IP..."
else
n=$((n+1))
echo -en "$i        r"
fi

done < $filename

最新更新