bash管道在ruby #system中不起作用,在ruby Shell#system中不起作用

时间:2018-07-20 14:31:11

标签: ruby linux bash perl

我使用以下代码打开应用程序图标

xprop -notype 32c -id 109051905 _NET_WM_ICON \
                  |    perl -0777 -pe '@_=/\d+/g;
                       printf "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n", splice@_,0,2;
                       $_=pack "N*", @_;
                       s/(.)(...)/$2$1/gs' \
                  | convert pam:- /tmp/icon.png

有效

但是当我在ruby中调用以上bash时,管道不起作用

system %Q(xprop -notype 32c -id 109051905 _NET_WM_ICON \
                  |    perl -0777 -pe '@_=/\d+/g;
                       printf "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n", splice@_,0,2;
                       $_=pack "N*", @_;
                       s/(.)(...)/$2$1/gs' \
                  | convert pam:- /tmp/icon.png)

提高

roroco@roroco ~/Downloads $ ruby /home/roroco/Dropbox/rb/ro_cmds_global/chk/v.rb
convert: negative or zero image size `-' @ error/pnm.c/ReadPNMImage/415.
convert: no images defined `/tmp/icon.png' @ error/convert.c/ConvertImageCommand/3210.

我尝试使用Shell#system,但仍然无法正常工作

require 'shell'
sh = Shell.new
sh.system(%Q(xprop -notype 32c -id 109051905 _NET_WM_ICON )) | sh.system(%Q(
                   perl -0777 -pe '@_=/\d+/g;
                       printf "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n", splice@_,0,2;
                       $_=pack "N*", @_;
                       s/(.)(...)/$2$1/gs')) |
    sh.system(%Q(convert pam:- /tmp/icon.png))

提高

roroco@roroco ~/Downloads $ ruby /home/roroco/Dropbox/rb/ro_cmds_global/chk/v2.rb
shell(#<Th:0x000000015de228>): /usr/bin/xprop -notype 32c -id 109051905 _NET_WM_ICON
shell(#<Th:0x000000015de228>): /bin/sh -c 
                   perl -0777 -pe '@_=/d+/g;
                       printf "P7
WIDTH %d
HEIGHT %d
DEPTH 4
MAXVAL 255
TUPLTYPE RGB_ALPHA
ENDHDR
", splice@_,0,2;
                       $_=pack "N*", @_;
                       s/(.)(...)/$2$1/gs'
shell(#<Th:0x000000015de228>): /usr/bin/convert pam:- /tmp/icon.png
shell(#<Th:0x000000015de228>): Process finishing, but active shell exists
                               You can use Shell#transact or Shell#check_point for more safe execution.
convert: negative or zero image size `-' @ error/pnm.c/ReadPNMImage/415.
convert: no images defined `/tmp/icon.png' @ error/convert.c/ConvertImageCommand/3210.

所以它如何工作

我发现原因是,使用我使用ruby #system,在perl脚本中@_是空的,所以我认为这是perl收到输入问题,如何解决?

我可以将perl脚本转换为ruby,也许我认为它可以工作,但是我想知道为什么perl无法在ruby #system中接收输入

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,当使用过于复杂的bash代码时,我应该使用%q()而不是%Q,以下代码将起作用:

system %q(xprop -notype 32c -id ) + 111149125.to_s + %q( _NET_WM_ICON \
                  |    perl -0777 -pe '@_=/\d+/g;
                       printf "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n", splice@_,0,2;
                       $_=pack "N*", @_;
                       s/(.)(...)/$2$1/gs' \
                  | convert pam:- jpg:-)