• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2448
  • Last Modified:

Checking if a file is empty, Bash

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
jaxstorm
Asked:
jaxstorm
1 Solution
 
omarfaridCommented:
can you elaborate more on the problem? what is meant by "file is empty"?
0
 
jaxstormAuthor Commented:
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
 
omarfaridCommented:
do you expect the file to contain empty lines? or if the file size is greater than 0 bytes then it contains urls?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
jaxstormAuthor Commented:
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
 
omarfaridCommented:
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
 
Maciej SsysadminCommented:
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
 
TintinCommented:
      -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
 
omarfaridCommented:
Please see author's comment to my query # 24169125
0
 
jaxstormAuthor Commented:
if [ -n "`cat ${DLBASKET}`" ]; then

This solution worked and was the easiest to implement, thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now