我怎样才能在后台运行Perl的系统命令? [英] How can I run Perl system commands in the background?

查看:566
本文介绍了我怎样才能在后台运行Perl的系统命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/usr/bin/env perl
use warnings; use strict;
use 5.012;
use IPC::System::Simple qw(system);

system( 'xterm', '-geometry', '80x25-5-5', '-bg', 'green', '&' );

say "Hello";
say "World";

我想这在后台运行在xterm命令,但它不工作:

I tried this to run the xterm-command in the background, but it doesn't work:

发现外壳没有绝对路径:放大器;

No absolute path found for shell: &

什么是正确的方法,使工作?

What would be the right way to make it work?

推荐答案

Perl的系统功能有两种模式:

Perl's system function has two modes:


  1. 取一个字符串并将它传递给命令外壳允许的特殊字符进行处理

  2. 取的字符串列表,exec'ing第一和传递剩余字符串作为参数

在第一种形式,你必须要小心逃脱,可能有特殊意义的shell字符。第二种形式是一般比较安全,因为参数直接传递给程序被exec'd,而不涉及外壳。

In the first form you have to be careful to escape characters that might have a special meaning to the shell. The second form is generally safer since arguments are passed directly to the program being exec'd without the shell being involved.

在你的情况似乎混合两种形式。在&安培; 字符只,如果它被传递到外壳在后台启动此程序的含义。在你的程序中,符号被安排在第5参数xterm命令过去了。

In your case you seem to be mixing the two forms. The & character only has the meaning of "start this program in the background" if it is passed to the shell. In your program, the ampersand is being passed as the 5th argument to the xterm command.

由于雅各布克鲁泽说,简单的答案是使用系统单一字符串形式。如果有任何的参数来自不受信任的来源来到你必须使用引用或转义,使他们的安全。

As Jakob Kruse said the simple answer is to use the single string form of system. If any of the arguments came from an untrusted source you'd have to use quoting or escaping to make them safe.

如果您preFER使用多参数的形式,那么你就需要调用叉(),然后可能使用 EXEC()而非系统()

If you prefer to use the multi-argument form then you'll need to call fork() and then probably use exec() rather than system().

这篇关于我怎样才能在后台运行Perl的系统命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆