Subject: Re: ps within DOS
From: Jeremy Hetzler <[email-masked]>
To: "Brian Warn" <[email-masked]>
Cc: <[email-masked]>
Date: Fri, 29 Mar 2002 19:34:57 -0500

At 02:54 PM 3/29/2002 -0800, Brian Warn wrote:
>As part of a (win32) perl program I'm running, I'm trying to run a
>system ps command and return to the DOS shell (or whatever the shell is
>known as in Win2K).  From the command line, I can do the following, but
>I stay in the bash shell:
>
>C:> c:\cygwin\cygwin.bat | ps | exit
>
>[ ps info here ]
>
>my_machine $
>
>
>The bottom line is that I want to read process info into an array as
>follows:
>
>@my_array=`cygwin.bat | ps | grep "desired string"`;
>
>Any help is appreciated!
>
>Thanks,
>Brian

Cygwin is working properly here, and I don't think running this code under 
Cygwin perl is going to help.

The problem is a poor understanding of the way shells work wrt input and 
output. Your command calls cmd.exe, the win32 shell, with the string 
'cygwin.bat | ps | grep "desired string"', or "run cygwin.bat, pipe the 
output through ps, and pipe that output through grep". Cygwin.bat probably 
calls "bash --login -i". The switch -i means "interactive shell", which is 
why you get a bash prompt. (If you really wanted to call bash just to run a 
single program -- a shell script, say -- you can do that with -c; see "man 
bash" or "info bash" for more information.) Then you try to pipe the output 
of cygwin.bat into ps, and then into grep.

Perhaps you're not aware that Cygwin utilities can be called under shells 
other than bash. Also, perl has a builtin grep that is more efficient than 
calling the external utility.

@my_array = grep {/regexp/} `ps`;

Or, using external grep:

@my_array = `ps | grep "string"`;

As mentioned above, this will pass the backquoted string to cmd.exe, so if 
you get a 'file not found' error, check your Windows PATH.

Jeremy

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/