LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-12-2010, 09:25 AM   #1
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Rep: Reputation: 8
Question Print all PID folders from /proc line-by-line with this format (( PID: command-line ))


How do you extract all numeric folders from PIDs variable, which is defined as:
Code:
PIDs="$(ls -v /proc/)"  # -v for numeric sort
I would like to print all PID (ie. numeric) folders from /proc folder. This does not work:
Code:
numberOfPIDs="$(ls /proc/ | wc -l)"

for ((x=0; x < $numberOfPIDs; x++)); do
	echo "$PIDsource | cut -d' ' -f$x"
done

Last edited by courteous; 12-12-2010 at 03:30 PM. Reason: made more precise title
 
Old 12-12-2010, 09:36 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
ls -dv [1-9]*
lists all the PID directories under /proc numerically sorted.
 
1 members found this post helpful.
Old 12-12-2010, 02:26 PM   #3
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Original Poster
Rep: Reputation: 8
Excellent.
How can I use your snippet outside /proc folder? Why this doesn't work:
Code:
ls -dv [1-9]* /proc/

Last edited by courteous; 12-12-2010 at 02:57 PM.
 
Old 12-12-2010, 03:01 PM   #4
impert
Member
 
Registered: Feb 2009
Posts: 282

Rep: Reputation: 54
Code:
ls -dv /proc/[1-9]*
 
1 members found this post helpful.
Old 12-12-2010, 03:09 PM   #5
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Original Poster
Rep: Reputation: 8
This for loop is much better, but it still gives plenty of errors:
Code:
PIDlist="$(ls -dv /proc/[1-9]* | cut -d '/' -f3)" # I need only PIDs
PIDnumber="$(ls /proc/ | wc -l)"

for i in "$PIDlist"; do
	printf "%4u | %10s\n" "$i" "$(cat /proc/$i/cmdline)"
done
Error example: cat: 4: No such file or directory. Indeed, in terminal cat /proc/4/cmdline returns nothing.

Last edited by courteous; 12-12-2010 at 03:12 PM.
 
Old 12-12-2010, 04:27 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Keep it simple. You might loop over the /proc/PID directories and use parameter substitution or the basename command to retrieve PID. Example:
Code:
for i in /proc/[1-9]*
do
  command="$(cat $i/cmdline)"
  command=${command:-<not available>}         # if command is not set or empty use default
  printf "%5d | %s\n" ${i##*/} "$command"     # ${i##*/} is the same as $(basename $i)
done | sort -k1n
Hope this helps.
 
1 members found this post helpful.
Old 12-12-2010, 04:36 PM   #7
courteous
LQ Newbie
 
Registered: Dec 2010
Posts: 22

Original Poster
Rep: Reputation: 8
What do hash-hash-star-slash, that is ${i##*/}, in printf do?

EDIT: Oh, I'm sorry, now I see it in comment. Forward to man basename!

Thank you again.

Last edited by courteous; 12-12-2010 at 04:38 PM.
 
Old 12-12-2010, 04:47 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually it is substring removal. Look at the advanced bash scripting guide, here.
Code:
${string##substring}

    Deletes longest match of $substring from front of $string.
In this case it deletes the longest match of any string * followed by a slash, acting as the basename command.
 
1 members found this post helpful.
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SleepAVG in /proc/[pid]/status aloktiagi Linux - Newbie 3 08-14-2009 07:02 AM
pmap or /proc/<pid>smap or /proc/<pid>/status iQoder Linux - Newbie 1 07-16-2009 06:32 PM
/proc/pid/maps Moraxus Linux - General 0 02-11-2009 08:49 AM
/proc/<pid>/stat empty? Brian Moffet Linux - Kernel 1 04-27-2008 02:05 AM
/proc/pid/io seems not to work kornelix Linux - Server 5 06-17-2007 02:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:21 PM.

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