Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
A question

Hi,
I'm new to unix and got struck here.Can any one help me out.My question is ..
is the command


Code:
if [ ps -ef |grep "any pid"];
then
  echo "do some stuff"
fi

correct?

Thanks in advance
abhijeet

Last edited by Scott; 02-07-2012 at 06:03 AM.. Reason: Please use code tags, and a more useful subject for your threads. Thanks.
Sponsored Links
    #2  
Old 02-07-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,019
Thanks: 9
Thanked 285 Times in 277 Posts

Code:
ps -ef | grep -q "any pid"
if [ $? -eq 0 ]
then
    echo "do some stuff"
fi

Sponsored Links
    #3  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Balajesuri!!

Can you throw some light on the syntax too.. it would be helpfull.
    #4  
Old 02-07-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,019
Thanks: 9
Thanked 285 Times in 277 Posts

Code:
ps -ef | grep -q "any pid" => Search for "any pid" from the output of ps -ef but don't display any output (-q takes care of this).
if [ $? -eq 0 ]            => $? is the exit status of last command executed. $? is set to 0 if the last command executed is a success.

Sponsored Links
    #5  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
suppose i want to grep only that PIDs which have its parrent pid as 1, then what should i do?
Sponsored Links
    #6  
Old 02-07-2012
ahamed101's Avatar
root is god!!!
 

Join Date: Sep 2008
Location: India
Posts: 1,478
Thanks: 33
Thanked 382 Times in 377 Posts
Something similar...

Code:
if  pgrep "some_pid" >/dev/null
then 
  echo "do some stuff"
fi

pgrep = process id grep

--ahamed
The Following 2 Users Say Thank You to ahamed101 For This Useful Post:
Abhijeet_Atti (02-07-2012), Prasannag87 (02-07-2012)
Sponsored Links
    #7  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Ahamed!!

can you help me little more..
suppose we have a pid= 15641 and it PPID =1
i want to check if this process is there i'll do some stuff..
now how would i check

Last edited by Scott; 02-07-2012 at 05:51 AM.. Reason: Removed "bump"; previous post was in moderation queue. Patience!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
New to Awk question... treeNinja Shell Programming and Scripting 2 09-21-2010 08:11 PM
***question on sed*** mahenderkrao UNIX for Advanced & Expert Users 3 06-19-2008 03:59 AM
Another question??? skyineyes UNIX for Dummies Questions & Answers 1 06-12-2008 01:09 AM
I have a question... Jerry_wang Linux 2 06-25-2005 09:59 AM
sed question Docboyeee Shell Programming and Scripting 2 04-06-2005 08:54 PM



All times are GMT -4. The time now is 04:36 AM.