通过反引号执行perl 3或4行命令

Mic*_*ris 1 terminal ubuntu perl

如何使用此代码创建更多命令.目前的版本2.我怎么做3或4或更多?

my $startprocess = `(echo "y" | nohup myprocess) &`
Run Code Online (Sandbox Code Playgroud)

用户DVK回答原始问题:

我可以在Perl的反引号中执行多行命令吗?

编辑:感谢塞巴斯蒂安的回复.我必须在一行中运行所有内容,因为我在终端内运行程序,我想制作渐进式命令.

例如,命令1启动程序.命令2导航我到菜单.命令3允许我更改设置.命令4允许我发出一个命令,提示我只能在新设置的条件下获得响应.

运行多个命令会让我陷入第一步.

Seb*_*ian 5

您引用的行包含一个管道连接的命令行.那不是运行多个命令.

你考虑过使用open吗?

my $pid = open my $fh, '-|', 'myprocess';
print $fh "y\n";
Run Code Online (Sandbox Code Playgroud)

不需要在一个(反引号)行中运行多个命令,为什么不使用多个命令呢?

$first = `whoami`;
$second = `uptime`;
$third = `date`;
Run Code Online (Sandbox Code Playgroud)

反引号用于捕获命令的输出,system只需运行命令并返回退出状态:

system '(echo "y" | nohup myprocess) &';
Run Code Online (Sandbox Code Playgroud)

所有解决方案都允许将多个命令连接在一起,因为这是一个shell功能,所有命令只是将命令字符串传递给shell(除非它很简单,无需shell即可处理):

击:

$ ps axu|grep '0:00'|sort|uniq -c|wc
Run Code Online (Sandbox Code Playgroud)

Perl的:

system "ps axu|grep '0:00'|sort|uniq -c|wc";
$result = `ps axu|grep '0:00'|sort|uniq -c|wc`;
open my $fh, '|-', "ps axu|grep '0:00'|sort|uniq -c|wc";
Run Code Online (Sandbox Code Playgroud)

始终注意引号:system "foo "bar" baz";不会"bar" baz作为参数传递给foo命令.

这个答案中有很多常见的东西:请在你的问题中更详细一些,以便得到更好的答复.