From: vincent.fourmond@9online.fr
Date: 2006-10-20T21:46:59+09:00
Subject: Re: handle a pipe (|) character inside backticks?
> # ps efxl |grep "grep"
> 0 0 9095 2224 15 0 3684 672 pipe_w S pts/1 0:00 |
> \_ grep grep...etc.
This is tricky: you are not guaranteed that grep will be already lanched by the time ps reads the process table. If you try that several times on the command-line, you'll find that some times it doesn't find grep.
> 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?
It has nothing to do with the |. The problem is, maybe when run under ruby, the timing is slightly different, and you come up more often with the case when grep is not started yet. See this:
irb(main):001:0> `ps efxl | grep irb`
=> "0 1000 426 401 15 0 4612 3248 - R+ pts/6 0:00 \\_ irb \n"
irb(main):002:0> `ps efxl | grep grep`
=> ""
irb(main):003:0> `ps efxl | grep grep`
=> "0 1000 435 433 15 0 1648 516 pipe_w S+ pts/6 0:00 \\_ grep grep MANPA\n"
Cheers !
Vince