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?
# 1  
Old 04-23-2013
Better to Use Return Code or wc -l Output?

Hey All,

Quick question...

I'm writing a short script to check if a continuous port is running on a server.

I'm using "ps -ef | grep -v grep | grep processName" and I was wondering if it was better/more reliable to just check the
return code from the command or if its better to pipe to 'wc -l' and check if it is '1' or not?

I was just curious if one method was more reliable then the other?

The server is "AIX 6"...

Thanks in Advance,
# 2  
Old 04-23-2013
They both do the exact same thing, but the return code method is simpler and costs one less process. The simpler way tends to be the best.

However: You're putting yourself through a lot of needless pain here:

if pgrep processName >/dev/null

You should find pgrep in most places.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-23-2013
Hey Corona, thanks for the reply!

Ok cool... I've never used "pgrep" before, thanks I'll give that a try.

Thanks Again,
# 4  
Old 04-23-2013
also, pgrep directly prints pid's, so if you need them as output it can be used unfiltered.
# 5  
Old 04-23-2013
Ugghhh, nevermind....

Looks like the UNIX server (which is probably older then myself, or pretty darn close) doesn't have the pgrep command.

It only has: grep, egrep, fgrep and zgrep...
ls -l /usr/bin | grep "grep"
-r-xr-xr-x    3 bin      bin           34052 Jan  7 2011  egrep
-r-xr-xr-x    3 bin      bin           34052 Jan  7 2011  fgrep
-r-xr-xr-x    3 bin      bin           34052 Jan  7 2011  grep
lrwxrwxrwx    1 root     system           27 May 12 2012  zgrep -> /usr/opt/freeware/bin/zgrep

Oh well, thanks anyways... I'll probably be able to get some use out of that on a slightly newer server. I guess I'll just go with the original idea.

Thanks Again,
# 6  
Old 04-23-2013
Originally Posted by mrm5102
Ugghhh, nevermind....

Looks like the UNIX server (which is probably older then myself, or pretty darn close) doesn't have the pgrep command.
What is it? uname -a It'd be useful to know where pgrep is absent... I thought it was somewhat universal.
# 7  
Old 04-23-2013
Yea I thought the same thing...

It's a pretty ancient system that I "think" we plan on replacing soon... FINGERS CROSSED!
Running mostly dreaded UniData stuff.

#uname -a
AIX hostName 1 6 00C595524C00

No worries though I should be fine with the 'ps | grep' commands...

Thanks Again,
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 #!/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

Featured Tech Videos