LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-09-2007, 03:10 AM   #1
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Rep: Reputation: 15
Scripting troubles


Hi guys,

I seem to have hit a wall. What I am trying to do is a listing of all process, grep for java processes and then return only the command being used and its arguments.

eg:

Code:
# ps afx | grep java
10045 pts/1    S      0:00          \_ grep java
18772 ?        S    195:24 /usr/java/j2sdk1.4.2_05/bin/java org.hsqldb.Server -database.0 file:/home/jboss/Qflex/bin/hsqldb/data/qflex -dbname.0 qflex
In the extract above, I only want to return this bit :
Code:
/usr/java/j2sdk1.4.2_05/bin/java org.hsqldb.Server -database.0
I've tried these (and many other) commands

Code:
ps afx | grep java |  tr -d ' ' | awk '{ for (i=0;i<length($0);i++) { if  ($i ~ "/user/") {print substr($i, index("user", $i), length($i)) }  } }'

ps -ef | grep java | awk '{ for (i=0;i<length($0);i++) { if  ($i ~ "/java/") {print substr($0, i, length) }  } }'

 ps -ef | grep java | awk BEGIN { for ( i=0;i>length;i++) { if ( $i == "\/") {print substr($i,i,length) }  } exit }
Any help would be greatly appreciated.

Thanks
 
Old 02-09-2007, 04:55 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
What I am trying to do is a listing of all process, grep for java processes and then return only the command being used and its arguments.
In your example you want only *part* of the arguments shown. The problem is I don't know *why*, so showing only the first three args is in no way a generic "solution". If your platform has 'pgrep' try to use it. It's a combo of 'ps' and 'grep' as the name suggests. With pgrep you can "anchor" your simple globbing on each part of the full argument line (except on Solaris). You can choose a "generic" anchor like the "java" line below to catch all processes using "/usr/java/j2sdk1.4.2_05/bin/java" or a more unique anchor like the "qflex" one:
Code:
pgrep -lf "/usr/java/j2sdk1.4.2_05/bin/java" | awk '{print $2, $3, $4}'
pgrep -lf '/*/qflex' | awk '{print $2, $3, $4}'
unless you mean select *and* the first three arguments conforming to be say one of "/java", "org." or "-datab":
Code:
pgrep -lf "/usr/*/java" | while read args; do
 args=(${args}); for ((i=0;i<${#args[@]};i++)); do
  case "${args[$i]}" in */java|org.*|-datab*) arglist="${arglist} "${args[$i]}"";;
  esac; done; echo $arglist; 
done
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting troubles Reegz Linux - Newbie 12 07-01-2006 09:50 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
BASH Scripting Troubles with IF and FUNCTION semaja2 Linux - Software 10 01-26-2006 02:04 AM
Shell Scripting Troubles!!! sc_3007 Linux - General 3 11-30-2005 01:21 PM
troubles with SATA, troubles with NVIDIA aevangelica Linux - Hardware 6 10-17-2005 02:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration