1838576 Members
4161 Online
110128 Solutions
New Discussion

script

 
Nobody's Hero
Valued Contributor

script

I need to do 2 things.

check to see if a file is empty

check to see if the contents of a file match a certain string.

Is there an easy way to do this?
UNIX IS GOOD
5 REPLIES 5
Hein van den Heuvel
Honored Contributor

Re: script

yes.

Regards,
Hein.
A. Clay Stephenson
Acclaimed Contributor

Re: script

Man test and see if something doesn't click to determine if a file is empty. If nothing clicks, then you have much bigger problems that shell programming.
If it ain't broke, I can fix that.
Nobody's Hero
Valued Contributor

Re: script

Closed.
UNIX IS GOOD
James R. Ferguson
Acclaimed Contributor

Re: script

HI:

# F=/tmp/emptyfile;[ -f "${F}" -a ! -s "${F}" ] && echo "${F} exists but is empty!"

...note that we test for the presence of a regular file in addition to testing its size.

# grep -q pattern file && echo "MATCH" || echo "NO MATCH"

Regards!

...JRF...
Geoff Wild
Honored Contributor

Re: script

Quick and dirty:


if [ -s $MYFILE ]
then
strings $MYFILE |grep $MYSTRING >/dev/null
if [ $? = 0 ]
then
echo "$MYSTRING is in $MYFILE"
else
echo "$MYSTRING is NOT in $MYFILE"
fi
else
echo "$MYFILE is emty!"
fi



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.