The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 03:00 PM
Check for empty string rahman_riyaz Shell Programming and Scripting 12 01-24-2008 03:13 AM
Check for Empty Command Argument Nysif Steve UNIX for Dummies Questions & Answers 6 09-19-2007 03:59 PM
How to check for null or empty string doer Shell Programming and Scripting 5 07-24-2007 01:31 AM
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 03:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-24-2005
stolz stolz is offline
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?
  #2 (permalink)  
Old 09-24-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
  #3 (permalink)  
Old 09-24-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #4 (permalink)  
Old 09-24-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 !!
  #5 (permalink)  
Old 09-24-2005
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
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
  #6 (permalink)  
Old 09-27-2005
stolz stolz is offline
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0