开发者

Is there any use in providing arguments as separate parameters to a system call using Perl?

开发者 https://www.devze.com 2023-01-18 11:07 出处:网络
On Unix, all these three generate the same result system(\"to开发者_如何转开发p -H -p $pid -n 1\");#ver1

On Unix, all these three generate the same result

system("to开发者_如何转开发p -H -p $pid -n 1");             #ver1
system("top", "H", "p $pid", "n 1");       #ver2
system("top", "-H", "-p $pid", "-n 1");    #ver3
  • What is the difference between ver2 and ver3?

  • Is there any reason I should use ver2 and ver3, and not ver1?

  • They do not even support piping the results, for example, are there any ver2 and ver3 equivalents of the following call?

    system("top -H -p $pid -n 1 | grep myprocess | wc -l");
    


Even it looks same it is not same:

$ perl -e 'system("./test.pl -H -p $$ -n 1");system("./test.pl", "H", "p $$", "n 1");system("./test.pl", "-H", "-p $$", "-n 1");'
-H,-p,10497,-n,1
H,p 10497,n 1
-H,-p 10497,-n 1
$ cat ./test.pl 
#!/usr/bin/perl
$\="\n";
$,=",";
print @ARGV;

It is up to top implementation that it works same. Other applications may not work same.


Quoth perlfunc for system:

Note that argument processing varies depending on the number of arguments. If there is more than one argument in LIST, or if LIST is an array with more than one value, starts the program given by the first element of the list with arguments given by the rest of the list. If there is only one scalar argument, the argument is checked for shell metacharacters, and if there are any, the entire argument is passed to the system's command shell for parsing (this is /bin/sh -c on Unix platforms, but varies on other platforms). If there are no shell metacharacters in the argument, it is split into words and passed directly to execvp , which is more efficient.

So if $pid is just digits, all are equivalent.

To interpolate results of an arbitrary shell command including pipes use qx and friends.


As a practical reason for using LIST, sometimes your command-line arguments contain spaces or other characters that would confuse your shell.

system("mplayer.exe", "--volume", "75",
       q[C:/Program Files/My Music Player/Music Library/The "Music" Song.mp3]);


  • What is the difference between ver2 and ver3?

Just in what arguments you're passing to top. I don't know of a version of top that will take switches without dashes like some versions of ps do, so you should use version 3.

  • Is there any reason I should use ver2 and ver3, and not ver1?

If you pass a single string to system it will run it via your shell. This means it will be shell interpreted. Any stray spaces or shell meta characters (quotes, dollar signs, etc...) in the arguments would be interpreted and possibly mess things up. It's also a potential security hole.

For example, if $pid was something like '10; echo pwnd; echo ' then you'd run top -H -p 10 then echo pwnd then echo -n1.

So for both safety and security, unless you need shell processing (see below) you should pass system a list.

  • Are there any ver2 and ver3 equivalents which allow pipes?

No, piping and redirection is done by the shell. You have to use something other than system. You can do it with open, but it's a pain in the ass. Easiest way is to use IPC::Run.

use IPC::Run;

my $out;
run ["echo", "foo\nbar\nbaz"], "|",
    ["grep", "ba"],            "|",
    ["wc",   "-l"],
    \$out;
print $out;  # 2

But really if you're just grepping and counting a handful of lines, use Perl.

my $out;
run ["echo", "foo\nbar\nbaz"], '>', \$out;

my $count = grep { /ba/ } split /\n/, $out;
print $count;
0

精彩评论

暂无评论...
验证码 换一张
取 消