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
C function to test existence of a login xavier054 High Level Programming 2 03-04-2008 09:51 AM
Checking the existence of a file.. igandu Shell Programming and Scripting 7 06-13-2007 12:47 AM
File existence mpang_ Shell Programming and Scripting 2 03-27-2006 08:27 AM
How to test for a specific file size GEBRAUN UNIX for Dummies Questions & Answers 1 02-20-2006 07:50 AM
Loop for file existence bd_joy UNIX for Dummies Questions & Answers 2 02-02-2006 08:51 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-26-2005
Registered User
 

Join Date: Sep 2005
Posts: 74
Stumble this Post!
how to test for file existence using file size?

ok im doing this in a normal shell. i need to check the file in the script. how should i write the if else statment?


if the filesize contains 0kb then it will echo out file contains nothing
else if the script contains more than 0kb then it will echo out file exist.


any one care to help? thanks ! im new to unix scripting..
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-26-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,323
Stumble this Post!
Code:
if [ -s /path/to/file ]; then 
echo file contains something
else echo contains nothing
fi
Try checking the man pages on your system as well. In this case, man test.
Reply With Quote
  #3 (permalink)  
Old 10-26-2005
Registered User
 

Join Date: Sep 2005
Posts: 74
Stumble this Post!
thank you blowtorch!
Reply With Quote
  #4 (permalink)  
Old 10-26-2005
Registered User
 

Join Date: Jul 2005
Location: Oak Park, IL
Posts: 97
Stumble this Post!
Here's something a little more informative, if you need it, but it's in ksh. Dunno if that's normal enough for you. -e is not available in sh:

#!/bin/ksh
if [ -s /path/to/file ] ; then
echo We have a file with stuff in it
elif [ -e /path/to/file ] ; then
echo We have a file with 0-length
else
echo "The file isn't even there!"
fi

do a "man test" on a UNIX box near you.

BTW, if you're on Linux then /sbin/sh is likely really bash, and you can use the -e.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:59 AM.


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

Content Relevant URLs by vBSEO 3.2.0