Visit Our UNIX and Linux User Community


How can I get an if statement to execute based on number of lines in a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I get an if statement to execute based on number of lines in a file?
# 1  
Old 05-14-2004
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?
LordJezo
# 2  
Old 05-14-2004
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  
Old 05-14-2004
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?
LordJezo
# 4  
Old 05-14-2004
-gt "3" means greater than 3
# 5  
Old 05-14-2004
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

Smilie
LordJezo
# 6  
Old 05-14-2004
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  
Old 05-14-2004
MySQL

Ah ha!

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

Thanks much.
LordJezo

Previous Thread | Next Thread
Test Your Knowledge in Computers #591
Difficulty: Medium
Selection Sort and Insertion Sort are simple but very efficient search algorithms.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

2. UNIX for Dummies Questions & Answers

Split file based on number of blank lines

Hello All , I have a file which needs to split based on the blank lines Name ABC Address London Age 32 (4 blank new line) Name DEF Address London Age 30 (4 blank new line) Name DEF Address London (8 Replies)
Discussion started by: Pratik4891
8 Replies

3. Shell Programming and Scripting

Replacing a character with a number based on lines

Hi, I am in need of help for the two things which is to be done. First, I have a file that has around four columns. The first column is filled with letter "A". There are around 400 lines in the files as shown below. A 1 5.2 3.2 A 2 0.2 4.5 A 1 2.2 2.2 A 5 2.1 ... (2 Replies)
Discussion started by: begin_shell
2 Replies

4. Shell Programming and Scripting

Concatinating the lines based on number of delimiters

Hi, I have a problem to concatenate the lines based on number of delimiters (if the delimiter count is 9 then concatenate all the fields & remove the new line char bw delimiters and then write the following data into second line) in a file. my input file content is Title| ID| Owner|... (4 Replies)
Discussion started by: bi.infa
4 Replies

5. Shell Programming and Scripting

combine lines from two files based on an if statement

I'm rather new to programming, and am attempting to combine lines from 2 files in a way that is way beyond my expertise - any help would be appreciated! I need to take a file (file1) and add columns to it from another file (file2). However, a line from file2 should only be added to a given line... (3 Replies)
Discussion started by: Cheri
3 Replies

6. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

7. Shell Programming and Scripting

Execute and log each statement/block SQL file

Hi friends, I would like to get some help on the following requirement. I have a SQL file with following things, select 1 from dual; select user from dual; select sysdate from dual; BEGIN PL/SQL Code END; / This file will be saved as sql file. When I run my expected shell script,... (1 Reply)
Discussion started by: ssnair
1 Replies

8. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

9. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

10. Shell Programming and Scripting

awk if statement matching all lines starting w/ a number

Does awk have a syntax for a range of numbers or is this the best way? if ($1 >= 0 && $1 <= 9 ) (7 Replies)
Discussion started by: Arsenalman
7 Replies

Featured Tech Videos