handle a pipe (|) character inside backticks?

C

Chris McMahon

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?
 
D

David

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"
 
D

David Vallner

--------------enigD11C168B34911E7A8D4CDABF
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Chris said:
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+6By6MhrS8astoRAs4XAJ917cFDU3FpuMCTjsn5lUWwHdxU2wCdELzw
UZQk77mF2XSiDehAqE4Tb6s=
=Ynvc
-----END PGP SIGNATURE-----

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top