Если вы сейчас используете
curl icanhazip.com > ip-location1.txt
затем, добавив опцию -f
, вы можете заставить curl
возвращать ошибку (и не выводить ), если это делает веб-сервер, поэтому, надеюсь, не будет необходимости выяснять, является ли вывод IP-адрес или сообщение об ошибке в формате HTML.
curl -f icanhazip.com > ip-location1.txt
Немного усложнив, вы можете добавить функцию повтора:
for i in 1 2 3 # if you want more retries, add more numbers here
do
curl -f icanhazip.com > ip-location1.txt
if [ $? -eq 0 ] && [ -s ip-location1.txt ]
then
break
fi
# if we get here, the current attempt failed
sleep 5 # be nice and wait a bit before retrying instead of spamming the service
done
if [ ! -s ip-location1.txt ]
then
echo "i cannot haz ip."
# do whatever you want to do in case of all the retries fail
fi