如何在 Perl 中使用 bash 命令更多地回显字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7348523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:43:54  来源:igfitidea点击:

How do I echo string with bash command more in Perl?

perlbash

提问by lexer

This is what I tried:

这是我尝试过的:

my $s = "s" x 1000;
my $r = `echo $s |more`;

But it doesn't work, my program exits directly...

但是不行,我的程序直接退出了...

回答by TLP

It does not work in your example, because you never print $r. The output is captured in the variable $r. By using system()instead, you can see the output printed to STDOUT, but then you cannot use the output as you (probably) expected.

它在您的示例中不起作用,因为您从不打印$r. 输出被捕获在变量中$r。通过使用system(),您可以看到打印到 STDOUT 的输出,但是您不能像您(可能)预期的那样使用输出。

Just do:

做就是了:

print $r;

Update: I changed sayto print, since "echo" already gives you a newline.

更新:我换sayprint,因为“回声”已经为您提供了一个换行符。

To escape shell meta characters, as mentioned in the comments, you can use quotemeta.

要转义 shell 元字符,如评论中所述,您可以使用quotemeta。

You should also be aware that | morehas no effect when capturing output from the shell into a variable. The process is simply: echo | more | $r, and you might as well skip more.

您还应该知道,| more将 shell 的输出捕获到变量中时没有任何影响。过程很简单:echo | more | $r,你也可以跳过more

回答by Cédric Julien

try with the system()command :

尝试使用system()命令:

my $s = "s" x 1000;
my $r = system("echo $s |more");

will display all your 's', and in $ryou will have the result (0 in this case) of the command.

将显示您所有的“s”,并且$r您将获得命令的结果(在本例中为 0)。