1821061 Members
2938 Online
109631 Solutions
New Discussion юеВ

wc -l if an if statement

 
SOLVED
Go to solution
Ratzie
Super Advisor

wc -l if an if statement

I am trying to write a script that would do a wc -l on a file, and if it is not equal to 30, yada yada..

if [ `wc -l` somefile -ne 30 ]
then
blah...
fi

but I keep getting errors.
9 REPLIES 9
Bryan D. Quinn
Respected Contributor

Re: wc -l if an if statement

Hello,

I would try something like:

NUM=`wc -l`

if [ $NUM -ne 30 ]
then
do something
else
do something_different

Hope this helps!
-Bryan
Bryan D. Quinn
Respected Contributor

Re: wc -l if an if statement

Ooops forgot my ending fi...

-Bryan
A. Clay Stephenson
Acclaimed Contributor

Re: wc -l if an if statement

if [[ $(wc -l somefile) -ne 30 ]]
then
blah
fi

Note that a space between [[ and $(.. is required as well as the end. 30 ]].

In Korn or POSIX the '[[ .. ]]' double brackets are preferred because they use the shell's internal test command rather than spawning a new process for test.
If it ain't broke, I can fix that.
Todd McDaniel_1
Honored Contributor

Re: wc -l if an if statement

It seems to me that yours will work if you put your ticks in the right place.

`wc -l somefile`

instead of:

`wc -l` somefile
Unix, the other white meat.
Sridhar Bhaskarla
Honored Contributor
Solution

Re: wc -l if an if statement

Hi,

You would get errors because 'wc -l somefile' will produce two fields and the way in which you are putting the ticks (the ticks should cover the entire command like `wc -l somefile`)

$wc -l myfile
123 myfile

So, you would need to do either of the following

if [ $(wc -l myfile|awk '{print $1}') -ne 30 ]

or

if [ $(cat myfile|wc -l) -ne 30 ]

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Steven E. Protter
Exalted Contributor

Re: wc -l if an if statement

note:

var=`wc -l somefile`

is no longer convention for such scripts. New scripts probably should not be done that way. I suppose support in the shell for the methodology will someday be pulled.

var=$(wc -l somefile)

is the current convention.

A. Clay's method looks simple and straightforward, Shridar's looks very slick.

Nicely done.

SEP

Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: wc -l if an if statement

Oops,

that should be

FNAME=somefile
if [[ $(cat ${FNAME} | wc -l) -ne 30 ]]
then
blah
fi

and a still more robust version would first test for the existence of the file:

FNAME=somefile
if [[ -r "${FNAME} ]]
then
if [[ $(cat ${FNAME} | wc -l) -ne 30 ]]
then
blah
fi
fi

If it ain't broke, I can fix that.
Bryan D. Quinn
Respected Contributor

Re: wc -l if an if statement

Oops again. My first post should have been:

NUM=`cat somefile | wc -l`

if [ $NUM -ne 30 ]
then
do something
else
do something_different

Sorry about that.

-Bryan
Peter Nikitka
Honored Contributor

Re: wc -l if an if statement

Hi,

another short solution:

if [ $(wc -l then bla
fi

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"