Better to Use Return Code or wc -l Output?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Better to Use Return Code or wc -l Output?
# 8  
Old 04-23-2013
If the process or utility is coded to return its exit code, I recommend using it to verify whether it succeeded or not.

If you do not have pgrep, use grep:
Code:
if ps -ef | grep [p]rocessName
then
      ...
fi

Here is the reason why I enclosed the first character inside square brackets:
Code:
$ ps -eaf | grep PatrolAgent
    yoda 29701 14851  0 16:12:47 pts/9     0:00 grep PatrolAgent
  patrol  1641     1 80  Mar 14  ?        1961:08 PatrolAgent

This will help you to save a pipeline for grep -v grep
Code:
$ ps -eaf | grep [P]atrolAgent
  patrol  1641     1 76  Mar 14  ?        1961:08 PatrolAgent

This User Gave Thanks to Yoda For This Post:
# 9  
Old 04-23-2013
Code:
I was just curious if one method was more reliable then the other?

AFAIK, the two methods are exactly the same reliability. If grep finds a line, $? will be zero AND there will be output so wc -l result will be at least one. I would use the $? method, because one less step, unless there is some issue about the number of lines that grep might find.

It's fine to use grep -v grep | grep pattern

The other methods suggested (pgrep and [p]attern)are fine too. Smilie But I don't think they are "better". The other methods are (a little) shorter, so that's a small advantage. But one method is subject to failure if pgrep not present, the other admittedly clever method is not obvious to many users why it works, can be confusing to many users. I would normally use the simple way you presented.
This User Gave Thanks to hanson44 For This Post:
# 10  
Old 04-24-2013
Quote:
Originally Posted by hanson44
It's fine to use grep -v grep | grep pattern

The other methods suggested (pgrep and [p]attern)are fine too. Smilie But I don't think they are "better". The other methods are (a little) shorter, so that's a small advantage. But one method is subject to failure if pgrep not present, the other admittedly clever method is not obvious to many users why it works, can be confusing to many users. I would normally use the simple way you presented.
hanson44, I don't have any concerns if you think that using grep -v grep is cleaner, not that much of a waste.

But I would like to remind you that the name grep comes from the ed command: g/re/p (global / regular expression / print)

So if using regular expression: [p]rocessName clearly eliminates the need for using grep -v grep and thus save a process, then I strongly believe we should use it.

By the way it is your privilege to waste processes if you want. So feel free...
# 11  
Old 04-24-2013
Most ps-grep pipelines are underspecified (including all instances in this thread). Consider the following:

Quote:
Originally Posted by Yoda
Code:
ps -ef | grep [p]rocessName

The pattern is evidently intended to match only a command name, but then why is it allowed to match any portion of a full listing? With such a promiscuous approach, usernames, command line arguments, and ps headers can trigger a false positive.

Even if the match is restricted to the command name, if the pattern isn't anchored at both ends, substring matches could cause false positives.

Quote:
Originally Posted by mrm5102
I'm using "ps -ef | grep -v grep | grep processName"
Quote:
Originally Posted by hanson44
It's fine to use grep -v grep | grep pattern
These have the added limitation of not being capable of searching for a grep command.

If I were going to use ps to portably test for the existence of a process whose command name is exactly cmdName, it would look something like this:
Code:
ps -eo comm= | grep -qFx cmdName

If portability isn't a concern, on a Linux system one could also use:
Code:
ps -C cmdName >/dev/null

Quote:
Originally Posted by hanson44
The other methods suggested (pgrep and [p]attern)are fine too. Smilie But I don't think they are "better". The other methods are (a little) shorter, so that's a small advantage. But one method is subject to failure if pgrep not present, the other admittedly clever method is not obvious to many users why it works, can be confusing to many users. I would normally use the simple way you presented.
It's not just about being concise; pgrep makes it simple to do the right thing. Its default behavior is to match against the command name only (args are excluded unless -f is provided). Further, a pgrep instance also excludes itself from the list of results, making it trivial to search for other instances of pgrep.

While it may not be available on the OP's system, pgrep has been widely available for about 5 years now on Solaris, HP-UX, Linux, OpenBSD, NetBSD, FreeBSD, OS X, and others.

Regards,
Alister

Last edited by alister; 04-24-2013 at 03:30 AM..
These 2 Users Gave Thanks to alister For This Post:
# 12  
Old 04-24-2013
Well, I read what you said, and took a look at the man page, and I change my opinion. pgrep is "better". I just didn't realize pgrep had so many useful options. Thanks for correcting my wrong idea.
# 13  
Old 04-24-2013
Wow!

Hey Guys, thanks for the replies!! Didn't think this would spark as much as it did, but I'm glad it did! LOTS of good info here...

Yea, I wish the system I'm running this on had pgrep, I checked out the manpage for it on "MY" own laptop (OpenSuSE 11.4) and it seems like
it would have been a better option if it were available on the AIX box... But oh well I'll just have to stay with the existing command. Thanks!


Yoda,

Yea I found out about using the [..] method a while back and it worked like a charm for MANY MANY other scripts I wrote, so I might try to
include that in my current script and remove the "grep -v grep".


Thanks EVERYBODY for all your replies, much appreciated!!


Thanks Again,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the return value and get the output to $results

cmd() { echo " " echo "$(whoami)@$(hostname):$(pwd)# $*" results=`eval $*` echo $results } I want to get the eval $* 's return value and pass it to a new variable $val, and get "eval $*" 's the ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. Shell Programming and Scripting

Return output from shell script

Hi All, There are 2 scripts A and B. A --> It will invoke script B B --> It will generate below output. 100 - connected 105 - Not Connected 210 - Connected I want to return this value to script A. Please advice. (4 Replies)
Discussion started by: Girish19
4 Replies

3. Shell Programming and Scripting

*ix script to check port of client server and return output

Hello EveryOne, I am new to *ix. some could help to write a script. Problem :- Have to ssh to so many client and check port or filesystem usage, so thinking to automate using script. What i Need:- when i run script on my Launchpad server, it should Should ask and SSH to user provided... (3 Replies)
Discussion started by: MeFirst
3 Replies

4. Shell Programming and Scripting

Problem with call of Java Programm & return code handling & output to several streams.

Hello Everybody, thanks in advance for spending some time in my problem. My problem is this: I want to call a java-Programm out of my shell skript, check if die return code is right, and split the output to the normal output and into a file. The following code doesn't work right, because in... (2 Replies)
Discussion started by: danifunny
2 Replies

5. Shell Programming and Scripting

psql output without return \n

Hi Everyone, When i finish running # echo `psql -t -U root databaseA -c "select a, b from book"`; i get the output 107275 | 14 107301 | 2 107446 | 6 107820 | 77 107929 | 101 Would like to have the result like: 107275 | 14 107301 | 2 107446 | 6 107820 | 77 107929 | 101 (7 Replies)
Discussion started by: jimmy_y
7 Replies

6. Windows & DOS: Issues & Discussions

how to return cygwin output to DOS

Hi, I have installed cygwin on my windows XP system. And I am trying to use the following code snippet: set SERVER="grep JDBCConnectionPool config.xml | gawk -F; '{print $2}'" sed -i 's/%SERVER%/WLMDEVDB1/' config.xml But it doesn't work because at runtime %SERVER% gets replaced by "grep... (2 Replies)
Discussion started by: illcar
2 Replies

7. Shell Programming and Scripting

Howto return the single penultimate line from my output

Hi All I have what seems to be something quite trivial but for the life of me can't work out a solution. Basically, I have the following script that reads a version report that contains certain flags. If a condition is true, I want to print the value of column 2, sort them uniquely and return... (2 Replies)
Discussion started by: kingpin2502
2 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

asking about return code

hi all my system is linux red hat i have a script that runs some object . the object return some code to the system i see the code by writing echo $? i want to ask in the script if $? equals 14 how shell is do that in the script thanks (3 Replies)
Discussion started by: naamas03
3 Replies

10. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies
Login or Register to Ask a Question