- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- wc -l if an if statement
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 05:45 AM
тАО02-02-2004 05:45 AM
if [ `wc -l` somefile -ne 30 ]
then
blah...
fi
but I keep getting errors.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 05:47 AM
тАО02-02-2004 05:47 AM
Re: wc -l if an if statement
I would try something like:
NUM=`wc -l`
if [ $NUM -ne 30 ]
then
do something
else
do something_different
Hope this helps!
-Bryan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 05:47 AM
тАО02-02-2004 05:47 AM
Re: wc -l if an if statement
-Bryan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 05:56 AM
тАО02-02-2004 05:56 AM
Re: wc -l if an if statement
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 06:00 AM
тАО02-02-2004 06:00 AM
Re: wc -l if an if statement
`wc -l somefile`
instead of:
`wc -l` somefile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 06:01 AM
тАО02-02-2004 06:01 AM
SolutionYou 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 06:12 AM
тАО02-02-2004 06:12 AM
Re: wc -l if an if statement
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 06:24 AM
тАО02-02-2004 06:24 AM
Re: wc -l if an if statement
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 07:39 AM
тАО02-02-2004 07:39 AM
Re: wc -l if an if statement
NUM=`cat somefile | wc -l`
if [ $NUM -ne 30 ]
then
do something
else
do something_different
Sorry about that.
-Bryan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-02-2004 11:23 PM
тАО02-02-2004 11:23 PM
Re: wc -l if an if statement
another short solution:
if [ $(wc -l
fi
mfG Peter