Help with KSH script

I need to inside one of my scripts be able to view a file
if it's empty have a message that says - nothing in the file
and if it's not view the contents

I was trying to figure out the best and most efficient way to do this but I'm getting stuck.

Any suggestions would be much appreciated.

Thank you.

Hello there,

I think the following code does the job

#!/bin/ksh

WORD_COUNT=$(wc -w $1 | cut -c 1)

if (( WORD_COUNT == 0 ))
then
    print "file is empty"
else
    print "file is not empty"
fi

Regards,
:slight_smile:

Or using test -s

#!/bin/ksh
FILENAME="$1"
if [ -s "${FILENAME}" ]
then
    echo "Not empty"
else
    echo "Empty"
fi

i tried the -s for test and it did not work. I dont think we have that option in our environment or something?

As for print "file is not empty"
can I just do cat filename so it shows the contents?

Also can you explain this line to me:
WORD_COUNT=$(wc -w $1 | cut -c 1)
wut does the cut do here?

thanks!

Here is an example:
Suppose you have a file named "myfile" containing only a single line "Hello World", that is, 11 characters, therefore there will be 11 bytes in the file and two words "Hello" and "World". Here is the result of the wc commnd

$ wc -c myfile
11 myfile

11 is the number of bytes and myfile is just the name of the file. Yet, it is only the number of bytes which is interesting for this problem. As a result by using the command cut we extract this first field (by a pipe)

$ wc -c myfile | cut -c 1
11

:slight_smile:

Hi, just a note,

wc -c < myfile

would be neater, one program less involved.

And, llsmr777, If You're only interested in the content of myfile, and not really the status of it (having text or not), just cat'ing it would do fine, because You will see whether it has anything in it. Like:

echo Content of myfile:
cat myfile
echo End of content

/Lakris