LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2013, 03:26 AM   #1
cpjain
LQ Newbie
 
Registered: Jan 2013
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 02-01-2013, 07:26 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
You can check the return value of 'wget':

Code:
wget -O /path/ $REPO_URL
if [[ $? -eq 0 ]]; then
   do stuff
fi
 
Old 02-01-2013, 07:37 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
wget downloads empty pages ted_chou12 Linux - Newbie 1 04-05-2011 01:55 PM
[SOLVED] wget failure on my system Ajit Gunge Linux - Newbie 5 10-07-2010 05:57 AM
[SOLVED] Cron - mysqldump job creates an empty sql file novice32 Linux - Newbie 5 04-17-2010 12:12 PM
Kubuntu creates empty folders in my home dir bhert Ubuntu 3 06-26-2008 12:59 PM
aplay -l > message,txt creates empty file milindlokde Programming 5 06-24-2007 01:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration