Skip to content

Commit

Permalink
Fixed issue 4: added a -d option for outputting only PIDs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvz committed Jul 11, 2011
1 parent 99eb6ee commit 936fc18
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 9 additions & 4 deletions psgrep
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# © 2006-2010 Matt Sicker
# © 2006-2011 Matt Sicker
# This program is Free Software; you may copy, modify, and distribute it under
# the terms of the GNU General Public License version 3, or (at your option)
# any later version published by the Free Software Foundation.
Expand Down Expand Up @@ -34,6 +34,7 @@ Options:
-l Search using Linux's ps format (-ef)
-s Search using simplified format (PID, user, command)
-o <opts> Search using custom ps output format
-d Search using PID-only format (similar to pgrep)
-i Ignore case in searches
-e Use extended regexps (egrep)
-p Use perl regexps (grep -P)
Expand All @@ -45,7 +46,7 @@ Options:
version_info()
{
echo "psgrep version ${PSGREP_VERSION}
© 2006-2010 Matt Sicker
© 2006-2011 Matt Sicker
This program comes with ABSOLUTELY NO WARRANTY; for details, see the included
COPYING file. This is free software, and you are welcome to redistribute it
under certain conditions; see the included COPYING file for details. In short,
Expand All @@ -56,7 +57,8 @@ Public License version 3, or (at your option) any later version."

[ $# -lt 1 ] && usage

while getopts "nablso:iephv" opt; do
PID_ONLY=0
while getopts "nablso:iephvd" opt; do
case $opt in
h) usage;;
v) version_info;;
Expand All @@ -69,12 +71,15 @@ while getopts "nablso:iephv" opt; do
i) GREP_CMD="${GREP_CMD} -i";;
e) GREP_CMD="${GREP_CMD} -E";;
p) GREP_CMD="${GREP_CMD} -P";;
d) PS_OPTS="-eo pid,comm";
NO_HEADER=1;
PID_ONLY=1;;
esac
done

# now the actual execution
HEADER=$($PS_CMD $PS_OPTS | head -n1)
RESULT=$($PS_CMD $PS_OPTS | $GREP_CMD -- "${*:$OPTIND}" | $GREP_CMD -v "$GREP_CMD" | $GREP_CMD -v "$0")
RESULT=$($PS_CMD $PS_OPTS | $GREP_CMD -- "${*:$OPTIND}" | $GREP_CMD -v "$GREP_CMD" | $GREP_CMD -v "$0" | awk '{print $'$PID_ONLY' }')
if [ "$RESULT" ]; then
[ ! "$NO_HEADER" ] && echo "$HEADER"
echo "$RESULT"
Expand Down
9 changes: 8 additions & 1 deletion psgrep.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" vim:syn=groff:
.\" Man page for psgrep
.TH PSGREP 1 "2010-01-02" "" "psgrep Manual"
.TH PSGREP 1 "2011-07-11" "" "psgrep Manual"
.
.SH NAME
psgrep \- search the process list using the power of grep
Expand Down Expand Up @@ -39,6 +39,13 @@ Search the process list using BSD's \*(lqps ux\*(rq format (\fBdefault\fR).
This option only includes the running user's processes.
.
.TP
.B -d
Search the process list, but only output matching process ID numbers. Similar
in functionality to
.BR pgrep (1)
found on some common POSIX systems.
.
.TP
.B -l
Search the process list using Linux's \*(lqps -ef\*(rq format.
.
Expand Down

0 comments on commit 936fc18

Please sign in to comment.