The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to check for empty file in Perl? deepakwins UNIX for Dummies Questions & Answers 1 03-04-2008 12:00 PM
Check for empty string rahman_riyaz Shell Programming and Scripting 12 01-24-2008 12:13 AM
Check for Empty Command Argument Nysif Steve UNIX for Dummies Questions & Answers 6 09-19-2007 12:59 PM
How to check for null or empty string doer Shell Programming and Scripting 5 07-23-2007 10:31 PM
How can I make the for command check to see if a file is empty before executing? chrchcol Shell Programming and Scripting 3 07-29-2006 12:14 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-24-2005
Registered User
 

Join Date: Sep 2005
Posts: 4
check if file is empty

How do I check if a file is empty in a sh script

I want to test in my shell script if the output file is empty and if it is do one thing and if it isnt empty do another?

any ideas?
Reply With Quote
Forum Sponsor
  #2  
Old 09-24-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Read the man pages of test

Code:
       -s FILE
              FILE exists and has a size greater than zero

Within a script it would take this form.

Code:
#! /bin/ksh

if [[ -s $FILE ]] ; then
echo "$FILE has data."
else
echo "$FILE is empty."
fi ;
vino
Reply With Quote
  #3  
Old 09-24-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Quote:
Originally Posted by vino
Code:
#! /bin/ksh

if [[ -s $FILE ]] ; then
echo "$FILE has data."
else
echo "$FILE is empty."
fi ;
vino
an exit status of 1 indicates that the FILE is empty or the file does not exist.....

Code:
$ ls
$ touch file_1
$ dd if=/dev/zero of=$HOME/tmp/file_2 bs=1 count=100
100+0 records in
100+0 records out
$ ls -l file_1 file_2 file_3
ls: file_3: No such file or directory
-rw-rw-r--  1 foo bar   0 Sep 24 20:28 file_1
-rw-rw-r--  1 foo bar 100 Sep 24 20:28 file_2
$ [[ -s file_1 ]]
$ echo $?
1
$ [[ -s file_2 ]]
$ echo $?
0
$ [[ -s file_3 ]]
$ echo $?
1
Cheers
ZB
Reply With Quote
  #4  
Old 09-24-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,699
Quote:
Originally Posted by zazzybob
an exit status of 1 indicates that the FILE is empty or the file does not exist.....

Code:
$ ls -l file_1 file_2 file_3
ls: file_3: No such file or directory
-rw-rw-r--  1 foo bar   0 Sep 24 20:28 file_1
-rw-rw-r--  1 foo bar 100 Sep 24 20:28 file_2
$ [[ -s file_1 ]]
$ echo $?
1
$ [[ -s file_2 ]]
$ echo $?
0
$ [[ -s file_3 ]]
$ echo $?
1
Cheers
ZB
A sleek 'n' smart solution !!
Reply With Quote
  #5  
Old 09-24-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
this is just another way of doing it

a round about one

Code:
if [ `ls -l <file> | awk '{print $5}'` -eq 0 ]
then
//condition for being empty
else
//condition for not being empty
fi
better way of doing is using the -s option of test
Reply With Quote
  #6  
Old 09-27-2005
Registered User
 

Join Date: Sep 2005
Posts: 4
Thanks guys

All worked as I required -s FILE option was the solution I used in my script and now I am a happy man.
Reply With Quote
  #7  
Old 07-24-2007
Registered User
 

Join Date: Apr 2007
Posts: 3
Question What if I want ot check for a file size greater than say 487

I have a requirement where if file size is 487 then ignore it otherwise put into e-mail script? I can do it using -s but it does only if file is empty...
any clue on this please???
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0