LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   wget -O creates a empty file on failure (https://www.linuxquestions.org/questions/linux-newbie-8/wget-o-creates-a-empty-file-on-failure-4175448119/)

cpjain 02-01-2013 03:26 AM

wget -O creates a empty file on failure
 
Hi All,

I am trying to check in the if condition that "some file" exists in the repository or not like below:

if (wget -O /path/ $REPO_URL); then
do operation
fi

my purpose is, if the file is present then do wget and perform some operation on that.

this is working well if the file is present however if it is not present in the repository wget creates an empty file.

how can i prevent this and what are my options?

Regards,
Chetan Jain.

Snark1994 02-01-2013 07:26 AM

You can check the return value of 'wget':

Code:

wget -O /path/ $REPO_URL
if [[ $? -eq 0 ]]; then
  do stuff
fi


unSpawn 02-01-2013 07:37 AM

...next to the return value of wget you can also check the size of the output file (see 'man test' and increase mana for using 'mktemp'):
Code:

goNoGo() { _TMPFILE=`mktemp -p /tmp wget.XXXXXXXXXX` && {
wget -q $1 -O > "${_TMPFILE}"; RETVAL=$?
[ $RETVAL -ne 0 ] && return 1
[ -s "${_TMPFILE}" ] && return 1
 rm -f "${_TMPFILE}"; }
}

Use like:
Code:

goNoGo $REPO_URL && do operation
*Caveats: checking wget exists (subst cURL, elinks -dump, etc, etc) plus the result could still be a malformed repo file...


All times are GMT -5. The time now is 12:48 PM.