if a file is empty throw an error


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers if a file is empty throw an error
# 1  
Old 12-08-2008
if a file is empty throw an error

I want to count the number of lines in a file and store it in a variable if this count is zero i hv to throw an error ...is this syntax correct , but i am not getting the desired result

I am not using -s option here as i am concerned about record count not the size

#!/bin/ksh
set $count1 = `wc -l < $DD_INDD`
echo "$count1" ///just for displaying the count
if [ $count1 -lt 1 ]; then
echo "****************************"
echo "**** ERROR - FILE EMPTY ****"
echo "****************************"
fi;
exit
# 2  
Old 12-08-2008
Please use code tags.

You didn't write what error you got with your script, but there were some things like a dollar sign in front of the variable name when defining it and stuff. Also you used the variable $DD_INDD and it wasn't even defined.... So it could/should look like...

Code:
#!/bin/ksh

DD_INDD=/tmp/yourfile

count1=`wc -l < $DD_INDD`

if [[ $count1 = 0 ]]; then
    echo "****************************"
    echo "**** ERROR - FILE EMPTY ****"
    echo "****************************"
fi

exit 0

# 3  
Old 12-08-2008
Quote:
Originally Posted by mavesum
I want to count the number of lines in a file and store it in a variable if this count is zero i hv to throw an error ...is this syntax correct , but i am not getting the desired result

I am not using -s option here as i am concerned about record count not the size

#!/bin/ksh
set $count1 = `wc -l < $DD_INDD`
echo "$count1" ///just for displaying the count
if [ $count1 -lt 1 ]; then
echo "****************************"
echo "**** ERROR - FILE EMPTY ****"
echo "****************************"
fi;
exit
Change
Code:
set $count1 = `wc -l < $DD_INDD`

to
Code:
$count1=`wc -l < $DD_INDD`

To check for empty file, you can use the -f construct i.e.
Code:
if [ -s $DD_INDD ] ; then


Last edited by vino; 12-09-2008 at 02:48 AM.. Reason: Wrong info
# 4  
Old 12-08-2008
You could also use -s option in the IF construct. "-s" will result true if file is non zero. So i have used negate operator.

if [ ! -s $DD_INDD ]
then
echo "file empty"
else
echo "file has some data"
fi
# 5  
Old 12-08-2008
thnx for all prompt replies ...all of them worked for me

i am using ! -s for my script Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to check and throw error for multiplication result

I need to multiply column1 and column3 data and need to compare it with column5. Need to check multiplication and Throw error if result is greater or less than column5 values, though difference of +/- 2 will be ok Ex - if column1 has 2.4 and column3 has 3.5, it will be ok if column5 have value... (13 Replies)
Discussion started by: as7951
13 Replies

2. HP-UX

Debug3 messages throw in my terminal

OS: HP-UX B.11.31 U ia64 shell : /sbin/sh Messages like "debug3: Wrote 48 bytes for a total of 15837" are thrown in my terminal after each key stroke. If I try to type a command such message appears after each character I type. If I simply press enter messages like below appear. ... (1 Reply)
Discussion started by: black_fender
1 Replies

3. What is on Your Mind?

Throw my Toys out of the Pram!

Hi Folks, Today hasn't been the best one of my career in IT. I've been a contractor for a major utility company for a number of years, on a number of seperate IT contracts mostly Unix. The company had 10 different flavours of unix and multiple different varsions of most of them. At the... (3 Replies)
Discussion started by: gull04
3 Replies

4. Shell Programming and Scripting

search line with more than two dots if so throw error

Hi, I have a requirement like i have to search a script and find names that conatins more than two dots, if so then throw error. For ex: a1.b1.comname here i have to find comname and check two dots. it will not throw error a1.b1.c1.comname here it contains more than 2dots it will throw... (3 Replies)
Discussion started by: swagat123
3 Replies

5. Shell Programming and Scripting

Find Command in the script throw error

Hi I have script that is developed to serch for 30 days old Directory & Files and then remove them ... when i run it successfully removes the Directory & files & but it throw errors on the screen .. .. + find . -type f -mtime +30 -exec rm -f {} ; + exit please help me ?? I... (0 Replies)
Discussion started by: Beginner123
0 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. Shell Programming and Scripting

throw a generic message upon error

hi all, in ksh, is there a way to throw a generic error message. i have lots of commands in my script and i didnt want to put if ; then doStuff(); else print "an error occured, please run script again"; fi around all the commands used. is there a way detect a command has... (1 Reply)
Discussion started by: cesarNZ
1 Replies

8. UNIX for Advanced & Expert Users

Find accessed file in past 1 or 2 minutes, and throw mail.

Can Anyone tell me how to write the Script to "find file(s) in a directory that is being accessed in last 5 minutes, and if there is result of that find, then throw the mail ". Means throw the mail to a person regarding whoever and whenever a file is accessed in a directory. ASAP. Thnks. (2 Replies)
Discussion started by: varungupta
2 Replies

9. Shell Programming and Scripting

loop throw perl regexp selection

hello all i have string that i need to parse something list that : <form name="CustomerStatus" action="<% env.GetURI %>" method="post"<$if(blah)%> name="<% env.get("StatusList") %>" ><% hello %><input type="hidden" name="<% env.get("Operation") %>" value=""> now im selecting the the string... (6 Replies)
Discussion started by: umen
6 Replies
Login or Register to Ask a Question