Package: procps
Severity: wishlist

hi,

it might, in certain cases, be handy to have a grep like -c option in
order to be able to sum up the matched processes, like:

 [EMAIL PROTECTED]:~$ ./pgrep -c getty          
 6

diff for pgrep.c and pgrep.1 attached.

bye,
    - michael
--- procps-3.2.5/pgrep.1        2004-07-15 15:58:46.000000000 +0200
+++ procps-3.2.5.own/pgrep.1    2005-07-29 12:17:08.000000000 +0200
@@ -38,6 +38,9 @@
 
 .SH OPTIONS
 .TP
+\-c
+Suppress normal output; instead print a count of matching processes.
+.TP
 \-d \fIdelimiter\fP
 Sets the string used to delimit each process ID in the output (by
 default a newline).  (\fBpgrep\fP only.)
--- procps-3.2.5/pgrep.c        2004-10-19 19:53:47.000000000 +0200
+++ procps-3.2.5.own/pgrep.c    2005-07-29 12:39:26.000000000 +0200
@@ -47,6 +47,7 @@
 static int opt_newest = 0;
 static int opt_negate = 0;
 static int opt_exact = 0;
+static int opt_count = 0;
 static int opt_signal = SIGTERM;
 
 static const char *opt_delim = "\n";
@@ -66,7 +67,7 @@
        if (i_am_pkill)
                fprintf (stderr, "Usage: pkill [-SIGNAL] [-fvx] ");
        else
-               fprintf (stderr, "Usage: pgrep [-flvx] [-d DELIM] ");
+               fprintf (stderr, "Usage: pgrep [-cflvx] [-d DELIM] ");
        fprintf (stderr, "[-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]\n"
                 "\t[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] "
                 "[PATTERN]\n");
@@ -501,7 +502,7 @@
                strcat (opts, "ld:");
        }
                        
-       strcat (opts, "fnovxP:g:s:u:U:G:t:?V");
+       strcat (opts, "cfnovxP:g:s:u:U:G:t:?V");
        
        while ((opt = getopt (argc, argv, opts)) != -1) {
                switch (opt) {
@@ -581,6 +582,9 @@
                                usage (opt);
                        opt_negate = 1;
                        break;
+               case 'c':
+                       opt_count = 1;
+                       break;
                // Solaris -x, the standard, does ^(regexp)$
                // OpenBSD -x, being broken, does a plain string
                case 'x':
@@ -617,13 +621,17 @@
                        if (kill (procs[i].num, opt_signal) != -1) continue;
                        if (errno==ESRCH) continue; // gone now, which is OK
                        fprintf (stderr, "pkill: %ld - %s\n",
-                                procs[i].num, strerror (errno));
+                               procs[i].num, strerror (errno));
                }
        } else {
-               if (opt_long)
-                       output_strlist (procs);
-               else
-                       output_numlist (procs);
+               if (opt_count) {
+                       fprintf(stdout, "%d\n", procs[0].num);
+               } else {
+                       if (opt_long)
+                               output_strlist (procs);
+                       else
+                               output_numlist (procs);
+               }
        }
        return ((procs[0].num) == 0 ? 1 : 0);
 }

Reply via email to