From man wget
, you can see that it uses the usual Unix return value convention - 0 means no error, anything else is an error. Provided you don't expect other kinds of errors (e.g. network failure or things like that), i.e. you expect that if it doesn't download anything that means there's no file, you can use something like this:
get_tf_simulated() {
t=$1
if [ $t -lt 3 ]; then
f=$3
s=$((2 * $t))
if [ $f -lt $s ]; then
return 0
fi
fi
return 1
}
get_tf_real() {
tp=$2
fp=$4
inf=$5
ext=$6
# Get http://example.com/test<test number>/<image or file><file number>.<jpg or txt>
wget -Otest$tp_file$fp_$inf.$ext http://example.com/test$tp/$inf$fp.$ext
}
get_tf() {
echo --- Getting $*
get_tf_simulated $*
#get_tf_real $*
}
get_all() {
get_tf $t $tp $f $fp image jpg
ret_val=$?
if [ $ret_val -ne 0 ]; then
return $ret_val
fi
get_tf $t $tp $f $fp file txt
}
for t in {1..999}; do
tp=`printf %3.3d $t`
got_one=no
for f in {1..9999}; do
fp=`printf %4.4d $f`
get_all $t $tp $f $fp
if [ $? -ne 0 ]; then
echo Failed, going next
break
fi
got_one=yes
done
if [ $got_one == 'no' ]; then
echo Nothing more
break
fi
done
Uncomment the right line in get_all
function. Currently, it will simulate it and the output will be like this (provided you saved the above to mkt.sh
):
$ ./mkt.sh
--- Getting 1 001 1 0001 image jpg
--- Getting 1 001 1 0001 file txt
--- Getting 1 001 2 0002 image jpg
Failed, going next
--- Getting 2 002 1 0001 image jpg
--- Getting 2 002 1 0001 file txt
--- Getting 2 002 2 0002 image jpg
--- Getting 2 002 2 0002 file txt
--- Getting 2 002 3 0003 image jpg
--- Getting 2 002 3 0003 file txt
--- Getting 2 002 4 0004 image jpg
Failed, going next
--- Getting 3 003 1 0001 image jpg
Failed, going next
Nothing more
Note I did not test the wget
one, but you can use this to test on a couple of files:
wget -Otest$tp_file$fp_$inf.$ext http://example.com/test$tp/$inf$fp.$ext; echo $?
Just replace $tp
, $fp
, $inf
and $ext
as necessary, e.g. for similar example to what you gave:
wget -Otest052_file0001_file.txt http://www.example.com/sub-somewhere052/file0001.txt; echo $?
This should echo 8
for 404, from the man wget
:
8 Server issued an error response.
If this works, then the script should work, hope there are no typos in that one line. :)