How to echo message when file is empty.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to echo message when file is empty.
# 1  
Old 10-20-2010
How to echo message when file is empty.

If a file is empty then it must print the message "no data found"

eg:

Code:
 if [cat $FILE_NAME = empty ] then
      echo "no data found"

your help is really appreciated.

Last edited by radoulov; 10-20-2010 at 08:42 AM.. Reason: Code tags, please!
# 2  
Old 10-20-2010
There is a test option for this:

Code:
if [ ! -s FILE ]; then
  echo "No data found..."
fi

# 3  
Old 10-20-2010
If you want to test for empty file (without any non printable characters or white spaces):

Code:
[ -s afile ] || echo empty

Otherwise:

Code:
[ -n "$(<afile)" ] || echo empty

# 4  
Old 10-20-2010
Thanks for the help it worked

if [ ! -s FILE ]; then echo "No data found..." fi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Find a string in all files and echo a message

Hi. I m trying to figure out how to do this. I have a directory full of files (100 files) and I want to be able to search for a string called "end" at the end of the files (last line or last 5 lines) and echo each file to say "incomplete" if not found. This is what I have so far. ---... (4 Replies)
Discussion started by: jasonhawaii
4 Replies

3. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

4. UNIX for Dummies Questions & Answers

Mailx empty body message

Hi, Is there a way to suppress this message? Null message body; hope that's ok My email string is: mailx -s "This is my subject" myemail@domain.com < /dev/null It's just an annoyance to me that I would like see go away. (3 Replies)
Discussion started by: bbbngowc
3 Replies

5. UNIX for Dummies Questions & Answers

echo is returning empty

OS - AIX 6.1 I am have below entry in my .profile DS_LIB=/opt/IBM/InformationServer/Server/DSEngine/bin export DS_BIN When I do echo $DS_BIN, echo is returning empty $ echo $DS_BIN $ I never had this kind of issue earlier, please help me. Thanks srimitta (4 Replies)
Discussion started by: srimitta
4 Replies

6. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

7. UNIX for Dummies Questions & Answers

Trying to empty file using > but the file size increasing when next append

AIX 5.3 / KSH I have a Java application which creates a log file a.log. I have a KSH script which does the following action cp a.log /directory2/b.log > a.log After this the file size goes to 0 as per "ls -l" Then next time when the application writes into this file, the file size... (4 Replies)
Discussion started by: firdousamir
4 Replies

8. Shell Programming and Scripting

How to capture status code and echo a message

Im trying to execute application and its return code is below IF Status code=o echo "........" else Staus Code =-2 DJRE then echo "......" Can any one help me how to handle the status code and echo some message. (12 Replies)
Discussion started by: laknar
12 Replies

9. UNIX for Dummies Questions & Answers

What's wrong with this line: if ${TEST:?} ; then echo empty; fi

I want to print "empty" if $TEST is empty. TEST= if ${TEST:?} ; then echo empty; fi But it doesn't work. What's wrong? (2 Replies)
Discussion started by: meili100
2 Replies

10. Shell Programming and Scripting

Suppres error message when moving files from empty source folder

Hi, I would like to write a shell script that moves files from one folder to another without retrieving the error 'can not acces/find file or folder' when the source folder is empty. Any ideas, Thx in advance, Steven. (2 Replies)
Discussion started by: Steven
2 Replies
Login or Register to Ask a Question