Here is a small bash code which initially creates a temporary file tmpfile using mktemp followed by a wget operation on success or failure removes the temporary file created.
#!/bin/bash -ex
tmpfile="$(mktemp)"
wget -q $1 -O /tmp/temp.txt
if [ $? -eq 0 ] ; then
echo "wget success"
rm "${tmpfile}"
else
echo "wget fail"
rm "${tmpfile}"
fi
When a right url is passed to the script, wget on success checks the return value of last command using $? and deletes the temporary file as expected.
root@box:/# ./temp.sh www.google.com
++ mktemp
+ tmpkeyfile=/tmp/tmp.83uGY1NH5B
+ wget -q www.google.com -O /tmp/temp.txt
+ '[' 0 -eq 0 ']'
+ echo 'wget success'
wget success
+ rm /tmp/tmp.83uGY1NH5B
However if a url which result wget a unsuccessful such as 404-not found etc., I assume last executed wget should fail against if check and delete temporary file in else block. This isn't happening as wget just returns without any last return value as shown below. This indeed doesn't delete the temporary file when ever call to wget fails.
root@box:/# ./temp.sh www.googlegoogle.com
++ mktemp
+ tmpkeyfile=/tmp/tmp.pSL7hKyAlz
+ wget -q www.googlegoogle.com -O /tmp/temp.txt
root@box:/#
May I know how to capture every return failure code of wget by any means.
Question:
It should response every return http status code:
EDIT: I have tried your code, and it works fine
This is the list of exit codes for wget:
Check this: Link