0

私の Perl プログラムでは、次のような変数があるところまで来ました。

echo -e \"use\nseveral\nlines\"

execこのコマンドをシェルを介して(を使用して)次のように実行したいと思います

echo -e "use\nseveral\nlines"

evalに渡す前に変数を試してみましたexecが、それは を解釈\nして改行に変更しました。

編集:

変数が与えられ、その入力方法を制御できないことに注意してください。したがって、変数がそのまま入力された場合、それを「引用解除」する方法はありますか?

4

2 に答える 2

1

Perl では、複雑な引用符のエスケープを避けるためにq{}or qq{}(実行には or) を使用する必要があります。qx{}

これはうまくいくはずです(q{}補間を避けるために使用します\n):

my $str = q{echo -e "use\nseveral\nlines"};

これで、次を使用して実行できますqx

qx{$str}
于 2013-09-19T04:09:30.663 に答える
0

When you pass

echo -e \"use\nseveral\nlines\"

to the shell, it passes the following three args to the exec systems call:

echo
-e
use\nseveral\nlines

How does one create that last string? Here are a few ways:

"use\\nseveral\\nlines"  # Escape \W chars in double-quoted strings.
'use\\nseveral\\nlines'  # Escape \ and delimiters in single-quoted strings
'use\nseveral\nlines'    #    ...although it's optional if unambiguous.

The corresponding Perl command would be therefore be

exec('echo', '-e', 'use\nseveral\nlines');

system('echo', '-e', 'use\nseveral\nlines');

open(my $fh, '-|', 'echo', '-e', 'use\nseveral\nlines');
my @output = <$fh>;
于 2013-09-19T12:57:57.287 に答える