handle a pipe (|) character inside backticks?

Discussion in 'Ruby' started by Chris McMahon, Oct 19, 2006.

  1. 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?
    Chris McMahon, Oct 19, 2006
    #1
    1. Advertising

  2. Chris McMahon

    David Guest

    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
    David, Oct 19, 2006
    #2
    1. Advertising

  3. --------------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+6By6MhrS8astoRAs4XAJ917cFDU3FpuMCTjsn5lUWwHdxU2wCdELzw
    UZQk77mF2XSiDehAqE4Tb6s=
    =Ynvc
    -----END PGP SIGNATURE-----

    --------------enigD11C168B34911E7A8D4CDABF--
    David Vallner, Oct 19, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lee, wonsun
    Replies:
    1
    Views:
    478
    Jack Klein
    Nov 2, 2004
  2. Replies:
    1
    Views:
    215
    Ben Morrow
    Jun 2, 2004
  3. TechCrazy
    Replies:
    3
    Views:
    160
    TechCrazy
    May 19, 2005
  4. Replies:
    10
    Views:
    1,174
    Anno Siegel
    Apr 17, 2006
  5. PerlFAQ Server
    Replies:
    0
    Views:
    383
    PerlFAQ Server
    Jan 25, 2011
Loading...

Share This Page