bash 脚本:继续,直到网站上存在.jpg



我想执行以下操作:bash脚本,从特定的URL开始,一直持续到网站上存在图像。例如:

www.example.com/1.jpg
www.example.com/2.jpg
www.example.com/3.jpg
www.example.com/4.jpg
www.example.com/5.jpg

脚本应继续 1,2,3,4,5,并在达到 6 时停止,因为不再有图像。我想一个人做,但我需要一件事:如何检查图像是否存在?

#!/bin/bash
host='www.example.com/'
i=1
while curl -I  --stderr /dev/null "${host}${i}.jpg" | head -1 | cut -d' ' -f2 | grep 200
do
echo "Do something"
i=$i++
done
你也可以

使用 wget:

#!/bin/bash
i=1
while wget -q "www.example.com/image${i}.jpg"; do
    echo "Got $i"
    (( i++ ))
done

最新更新