delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2002/03/29/19:28:59

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Message-Id: <5.1.0.14.2.20020329190356.00ac6550@mail.earthlink.net>
X-Sender: jeremyhetzler AT mail DOT earthlink DOT net
Date: Fri, 29 Mar 2002 19:34:57 -0500
To: "Brian Warn" <bwarn-nm AT seattletimes DOT com>
From: Jeremy Hetzler <jeremyhetzler AT earthlink DOT net>
Subject: Re: ps within DOS
Cc: <cygwin AT cygwin DOT com>
In-Reply-To: <D4AE310621B50E4393EFE060F52D414C0F655B@EXCHANGE2.seatimes.
com>
Mime-Version: 1.0

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019