![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 ; |
|
#3
|
||||
|
||||
|
Quote:
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 ZB |
|
#4
|
||||
|
||||
|
Quote:
|
|
#5
|
|||
|
|||
|
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
|
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||
|
|||
|
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??? |
|||
| Google The UNIX and Linux Forums |