?
Solved

Checking if a file is empty, Bash

Posted on 2009-04-17
9
Medium Priority
?
2,425 Views
Last Modified: 2013-12-26
I want an if statement in my bash script to check if a file is empty. I've tried

if [ -s <stuff here> ] ;
and
if [ -z <stuff here> ] ;

but they don't seem to work. Is there something I can do with grep that will make it work? THe code below ALWAYS seems to return the "There are no URLS.." even if there are

if [ -s $DLBASKET];
then
cat $DLBASKET | (
        while read url ; do
                # First, check the cache if the file has been already downloaded
		if grep -i $(basename $url) "$CACHEFILE" >/dev/null 2>&1; then
			echo "$DATETIME File exists in cache. Already downloaded - Skipping: $url"
			echo $url >> $LOGFILE
		else
        	        echo "$DATETIME Starting: $url"
                		    if [ $(expr match "$url" '[rapidshare.com]') = 1 ] ; then
                               	    # If it is a Rapidshare.com link, load the RS cookie
                            	    	echo "RAPIDSHARE LINK"
                                    	wget -c -t 1  --directory-prefix=$DLDIR --load-cookies $RSCOOKIE $url
                                    else
                                    	wget -c -t 1  --directory-prefix=$DLDIR $url
                                    fi
                        echo "$DATETIME Finished: $url"
           		echo $DATETIME $url  >> $LOGFILE
                        echo $url >> $CACHEFILE
			echo "All Files have been downloaded, clearing Download Basket"
			echo "Finished!"
		fi
			
 
			done )		
else
echo "There are no URLS in $DLBASKET"
fi
exit 0

Open in new window

0
Comment
Question by:jaxstorm
9 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 24169095
can you elaborate more on the problem? what is meant by "file is empty"?
0
 
LVL 8

Author Comment

by:jaxstorm
ID: 24169104
The script looks for URLS in the file DLBASKET.

If there are URLS in there, it downloads them with wget

I want an if statement to check if there are URLS in there, and if there aren't to return a statement
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24169125
do you expect the file to contain empty lines? or if the file size is greater than 0 bytes then it contains urls?
0
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
LVL 8

Author Comment

by:jaxstorm
ID: 24169216
Either method would probably work. I'll be adding the lines myself. If the file size is greater than 0 would probably be more useful
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24169486
try this logic

if [ -s filename ]
then
       s=`wc -w < filename`
       if [ $s -gt 0 ]
       then
             cat filename | while read url
             do
                s=`echo $url | wc -w`
                if [ $s -gt 0 ]
                then
                       echo "do something"
                fi
              done
       fi
fi
0
 
LVL 23

Accepted Solution

by:
Maciej S earned 2000 total points
ID: 24169523
if [ -s "{DLBASKET}" ]; then
should work as expected.

You can also use something like below (but -s as in above line should be sufficient):
if [ -n "`cat ${DLBASKET}`" ]; then

Remember to put space between variable name and "]".
0
 
LVL 48

Expert Comment

by:Tintin
ID: 24172178
      -s FILE
              FILE exists and has a size greater than zero

So in your case, if you want to test for an empty or non-existent file, you negate it, eg:

if [ ! -s $var ]


0
 
LVL 40

Expert Comment

by:omarfarid
ID: 24174261
Please see author's comment to my query # 24169125
0
 
LVL 8

Author Closing Comment

by:jaxstorm
ID: 31571528
if [ -n "`cat ${DLBASKET}`" ]; then

This solution worked and was the easiest to implement, thanks
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question