The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Splitting file based on number of rows wahi80 Shell Programming and Scripting 2 06-03-2008 09:38 PM
Copy lines from a log file based on timestamp ranjiadmin UNIX for Advanced & Expert Users 1 05-22-2008 03:16 AM
searching and storing unknown number of lines based on the string with a condition swamymns Shell Programming and Scripting 7 05-13-2008 01:02 AM
Grabbing lines out of a file based on a date bsp18974 Shell Programming and Scripting 2 07-17-2006 08:58 AM
case statement based on file availability Loriel Shell Programming and Scripting 6 06-01-2004 08:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-14-2004
LordJezo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Question How can I get an if statement to execute based on number of lines in a file?

I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for:

$ ps -ef | grep wtrs
---
webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui -start
---
webtrend 5030 1 0 May 12 ? 3:29 /webtrends/versions/6.1/wtrs -start
---
webtrend 3550 5030 25 07:03:40 ? 91:09 /webtrends/versions/6.1/wtrs 314 -child
---
webtrend 6473 3999 0 09:10:08 pts/4 0:00 grep wtrs
---
webtrend 4409 5030 11 07:35:54 ?60:29 /webtrends/versions/6.1/wtrs 315 –child


("--" added in as line seperators)


Normally, I would expect to see the first, second, and fourth lines. That means everything is running normally. The third and fifth lines in that example are present because the program (wtrs) is currently executing certain commands.

So, what I want to do is an if statement that says (in English):

If wtrs is present in the above example 3 or fewer times, execute the following commands, otherwise do something else.

So what I was thinking of doing was first sending the output of the ps command into a text file:

$ ps -ef | grep wtrs > test.txt

Then the if statement could say:

If test.txt < 4 lines long, execute.

Anyone know how something like that would be possible, or if it would even be possible?
  #2 (permalink)  
Old 05-14-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Use wc -l to count the lines of the output

i.e.
Code:
if [ $( ps -ef | grep wtrs | wc -l ) -gt "3" ];
then
   # do stuff for more than 3 lines
else
   # do stuff for three or less lines
fi
peace,
ZB
http://www.zazzybob.com
  #3 (permalink)  
Old 05-14-2004
LordJezo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Quote:
Originally posted by zazzybob
Use wc -l to count the lines of the output

i.e.
Code:
if [ $( ps -ef | grep wtrs | wc -l ) -gt "3" ];
then
   # do stuff for more than 3 lines
else
   # do stuff for three or less lines
fi
What's the -gt "3" bit mean?
  #4 (permalink)  
Old 05-14-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
-gt "3" means greater than 3
  #5 (permalink)  
Old 05-14-2004
LordJezo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Script looks like this:

#!/bin/sh
if [ $( ps -ef | grep wtrs | wc -l ) -gt "3" ];
then
echo its running things
else
echo it can be shut down
fi


error it gets when run:

syntax error at line 2: `(' unexpected

  #6 (permalink)  
Old 05-14-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Try replacing
if [ $( ps -ef | grep wtrs | wc -l ) -gt "3" ]; then

with
if [ `ps -ef | grep wtrs | wc -l` -gt "3" ]; then

You're using the original Bourne shell, so try backquotes instead.

Or consider using #!/bin/ksh if it's installed and the original syntax above.

Peace
ZB
http://www.zazzybob.com
  #7 (permalink)  
Old 05-14-2004
LordJezo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Thumbs up

Ah ha!

That was it. Silly me.. just using a different shell.

Thanks much.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0