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

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...

#!/bin/ksh

DD_INDD=/tmp/yourfile

count1=`wc -l < $DD_INDD`

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

exit 0

Change

set $count1 = `wc -l < $DD_INDD`

to

$count1=`wc -l < $DD_INDD`

To check for empty file, you can use the -f construct i.e.

if [ -s $DD_INDD ] ; then

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

thnx for all prompt replies ...all of them worked for me

i am using ! -s for my script :slight_smile: