2016-04-30 121 views
1

如何使用此代码创建更多命令。目前的版本是2.我如何做3或4或更多?perl通过反引号的3或4行命令

my $startprocess = `(echo "y" | nohup myprocess) &` 

原来的问题由用户DVK回答:

Can I execute a multiline command in Perl's backticks?

编辑:感谢您的答复塞巴斯蒂安。 我必须在一行中运行所有内容,因为我在终端内运行一个程序,并且我想进行渐进式命令。

例如命令1启动程序。命令2将我导航到菜单。命令3让我改变设置。命令4让我发出一个命令,提示我只能在新设置的条件下才能得到响应。

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

回答

5

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

您是否考虑过使用open

my $pid = open my $fh, '-|', 'myprocess'; 
print $fh "y\n"; 

有没有必要在一个(反引号)行中运行多个命令,为什么不只是使用多个?

$first = `whoami`; 
$second = `uptime`; 
$third = `date`; 

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

system '(echo "y" | nohup myprocess) &'; 

所有解决方案允许多个命令管道连接到一起,因为这是一个壳特征和所有的命令只是传递命令字符串到shell(除非它足够简单处理一下无壳):

击:

$ ps axu|grep '0:00'|sort|uniq -c|wc 

的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"; 

随时观看引号:system "foo "bar" baz";不会通过"bar" baz作为参数传递给foo命令。

很多常见的东西在这个答案:请更详细的问题,以获得更好的答复。