Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > handle a pipe (|) character inside backticks?

Reply
Thread Tools

handle a pipe (|) character inside backticks?

 
 
Chris McMahon
Guest
Posts: n/a
 
      10-19-2006
I can't figure this out:

# ps efxl |grep "grep"
0 0 9095 2224 15 0 3684 672 pipe_w S pts/1 0:00 |
\_ grep grep...etc.

but in IRB:

irb(main):026:0> r = `ps efxl |grep "grep"`
=> ""
irb(main):027:0> puts r

=> nil
irb(main):028:0>

The problem seems to be with the pipe character | but escaping it with
"\" has no effect.
How can put a "|" inside my backticks?

 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      10-19-2006
On Fri, Oct 20, 2006 at 05:35:13AM +0900, Chris McMahon wrote:
> I can't figure this out:
>
> # ps efxl |grep "grep"
> 0 0 9095 2224 15 0 3684 672 pipe_w S pts/1 0:00 |
> \_ grep grep...etc.
>
> but in IRB:
>
> irb(main):026:0> r = `ps efxl |grep "grep"`
> => ""
> irb(main):027:0> puts r
>
> => nil
> irb(main):028:0>
>
> The problem seems to be with the pipe character | but escaping it with
> "\" has no effect.


The problem might be the interaction of ps with termcap/terminfo. It
seems your ps accepts BSD-style options. Are you on Mac OS X? I do not
know what terminal information, if any, irb/ruby provide to their
children processes. If they do provide such information, say in the
TERM environment variable, ps will try to get obtain the terminal width
and truncate its output to fit. On my GNU/Linux system with Ruby 1.8.5,
forcing a wide display (using SYSV style arguments) works:

irb(main):011:0> p ENV['TERM']
"xterm"
=> nil
irb(main):012:0> r = `ps -efww | grep grep`
=> "ddooling 4900 4820 0 16:06 pts/1 00:00:00 sh -c ps -efww | grep grep\nddooling 4902 4900 0 16:06 pts/1 00:00:00 grep grep\n"

But using your BSD-style arguments, especially the f (forest)
hierarchical display, causes problems:

irb(main):013:0> r = `ps efxl | grep grep`
=> ""

removing the e (trailing environment) and f options

irb(main):014:0> r = `ps xl | grep grep`
=> "0 1509 4910 4908 19 0 2852 692 pipe_w S+ pts/1 0:00 grep grep\n"

or just the f option

irb(main):015:0> r = `ps exl | grep grep`
=> "0 1509 4913 4911 18 0 2848 692 pipe_w S+ pts/1 0:00 grep grep E\n"

or using your whole commandf but maximizing the xterm before running it
(your email program may wrap the two lines below)

irb(main):016:0> r = `ps efxl | grep grep`
=> "0 1509 4936 4820 17 0 3632 1172 wait S+ pts/1 0:00 \\_ sh -c ps efxl | grep grep EST_SOURCE=/home/trna/pkg/est_source MANPATH=/gsc/scripts/man:/gsc/man:/gsc/java/man:/gsc/teTeX/man:/usr/local/man:/usr/share/man:/usr/X11R6/man:/usr/openwi\n0 1509 4938 4936 20 0 2848 688 pipe_w S+ pts/1 0:00 \\_ grep grep EST_SOURCE=/home/trna/pkg/est_source MANPATH=/gsc/scripts/man:/gsc/man:/gsc/java/man:/gsc/teTeX/man:/usr/local/man:/usr/share/man:/usr/X11R6/man:/usr/openwin/share/man:\n"

--
David Dooling

 
Reply With Quote
 
 
 
 
David Vallner
Guest
Posts: n/a
 
      10-19-2006
--------------enigD11C168B34911E7A8D4CDABF
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Chris McMahon wrote:
> irb(main):026:0> r =3D `ps efxl |grep "grep"`
> =3D> ""
> irb(main):027:0> puts r
>=20
> =3D> nil
> irb(main):028:0>
>=20
> The problem seems to be with the pipe character | but escaping it with=


> "\" has no effect. =20
> How can put a "|" inside my backticks?
>=20


Dump, replace with sys-proctable?

http://rubyforge.org/projects/sysutils

David Vallner


--------------enigD11C168B34911E7A8D4CDABF
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFN+6By6MhrS8astoRAs4XAJ917cFDU3FpuMCTjsn5lU WwHdxU2wCdELzw
UZQk77mF2XSiDehAqE4Tb6s=
=Ynvc
-----END PGP SIGNATURE-----

--------------enigD11C168B34911E7A8D4CDABF--

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 04-13-2011 10:00 PM
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 01-25-2011 05:00 AM
[named pipe] i wanna know about validate of pipe handle of client lee, wonsun C++ 1 11-02-2004 04:29 AM
Why does IO::Pipe::END generate an EXCEPT pipe message? lvirden@gmail.com Perl Misc 1 06-02-2004 02:17 PM
File handle re-use woes using pipe within a loop Mark Gowans Perl Misc 12 01-29-2004 05:21 PM



Advertisments