如何在Perl脚本中调用shell命令?

时间:2022-06-27 07:12:53

Can anyone give me an example on how I can call a shell command, say 'ls -a' in a Perl script and the way to retrieve the output of the command as well.

谁能给我举个例子,告诉我如何调用shell命令,比如Perl脚本中的“ls -a”,以及如何检索命令的输出。

7 个解决方案

#1


52  

How to run a shell script from a Perl program

如何从Perl程序运行shell脚本

1. Using system system($command, @arguments);

1。使用系统系统($命令,@arguments);

For example:

例如:

system("sh", "script.sh", "--help" );

系统(“上海”、“脚本。sh”、“帮助”);

system("sh script.sh --help");

系统(“sh脚本。sh——帮助”);

System will execute the $command with @arguments and return to your script when finished. You may check $! for certain errors passed to the OS by the external application. Read the documentation for system for the nuances of how various invocations are slightly different.

系统将使用@arguments执行$命令,完成后返回到您的脚本。你可以检查美元!对于外部应用程序传递给操作系统的某些错误。请阅读系统文档,了解不同调用之间的细微差别。

2. Using exec

2。使用exec

This is very similar to the use of system, but it will terminate your script upon execution. Again, read the documentation for exec for more.

这非常类似于系统的使用,但是它将在执行时终止您的脚本。同样,请阅读exec的文档以了解更多信息。

3. Using backticks or qx//

3所示。使用引号或qx / /

my $output = `script.sh --option`;

我的美元= '脚本输出。sh -选项”;

my $output = qx/script.sh --option/;

我的输出美元= qx /脚本。sh——选项/;

The backtick operator and it's equivalent qx// excute the command and options inside the operator and return that commands output to STDOUT when it finishes.

反勾操作符,它等价于qx/ exlovely操作符中的命令和选项,并在结束时将命令输出返回给STDOUT。

There are also ways to run external applications through creative use of open, but this is advanced use; read the documentation for more.

也有通过创造性地使用open来运行外部应用程序的方法,但这是高级用法;更多信息请阅读文档。

#2


10  

Look at the open function in Perl - especially the variants using a '|' (pipe) in the arguments. Done correctly, you'll get a file handle that you can use to read the output of the command. The back tick operators also do this.

看看Perl中的open函数——特别是在参数中使用“|”(pipe)的变体。正确完成后,您将获得一个文件句柄,您可以使用它来读取命令的输出。后勾操作符也这样做。

You might also want to review whether Perl has access to the C functions that the command itself uses. For example, for ls -a, you could use the opendir function, and then read the file names with the readdir function, and finally close the directory with (surprise) the closedir function. This has a number of benefits - precision probably being more important than speed. Using these functions, you can get the correct data even if the file names contain odd characters like newline.

您可能还想检查Perl是否能够访问命令本身使用的C函数。例如,对于ls -a,您可以使用opendir函数,然后使用readdir函数读取文件名,最后使用closedir函数关闭目录。这有很多好处——精确可能比速度更重要。使用这些函数,即使文件名包含像newline这样的奇数字符,也可以获得正确的数据。

#3


8  

Examples

  1. `ls -l`;
  2. ' ls - l ';
  3. system("ls -l");
  4. 系统(ls - l);
  5. exec("ls -l");
  6. exec(ls - l);

#4


7  

From Perl HowTo, the most common ways to execute external commands from Perl are:

从Perl HowTo中,执行来自Perl的外部命令最常见的方法是:

  • my $files = `ls -la` — captures the output of the command in $files
  • 我的$files = ' ls -la ' -捕获命令在$files中的输出
  • system "touch ~/foo" — if you don't want to capture the command's output
  • 系统“touch ~/foo”—如果您不想捕获命令的输出
  • exec "vim ~/foo" — if you don't want to return to the script after executing the command
  • 执行“vim ~/foo”——如果您不想在执行命令之后返回到脚本
  • open(my $file, '|-', "grep foo"); print $file "foo\nbar" — if you want to pipe input into the command
  • 打开(我的$file, '|-', ' grep foo ');打印$file“foo\nbar”—如果您想将输入导入到命令中

#5


6  

There are a lot of ways you can call a shell command from perl script, such as:

有很多方法可以从perl脚本调用shell命令,例如:

  1. back tick ls which captures the output and gives back to you.
  2. 后勾选ls,捕捉输出并返回给您。
  3. system system('ls');
  4. 系统系统(ls);
  5. open
  6. 开放

Refer #17 here: perl programming tips

参考#17:perl编程技巧

#6


6  

As you become more experienced with using Perl, you'll find that there are fewer and fewer occasions when you need to run shell commands. For example, one way to get a list of files is to use Perl's built-in glob function. If you want the list in sorted order you could combine it with the built-in sort function. If you want details about each file, you can use the stat function. Here's an example:

随着您对使用Perl越来越有经验,您将发现需要运行shell命令的情况越来越少。例如,获取文件列表的一种方法是使用Perl的内置glob函数。如果您希望列表按排序顺序排列,您可以将它与内置的排序函数组合在一起。如果您想了解每个文件的详细信息,可以使用stat函数。这里有一个例子:

#!/usr/bin/perl

use strict;
use warnings;

foreach my $file ( sort glob('/home/grant/*') ) {
    my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)
        = stat($file);
    printf("%-40s %8u bytes\n", $file, $size);
}

#7


1  

You might want to look into open2 and open3 in case you need bidirectional communication.

如果需要双向通信,您可能需要研究open2和open3。

#1


52  

How to run a shell script from a Perl program

如何从Perl程序运行shell脚本

1. Using system system($command, @arguments);

1。使用系统系统($命令,@arguments);

For example:

例如:

system("sh", "script.sh", "--help" );

系统(“上海”、“脚本。sh”、“帮助”);

system("sh script.sh --help");

系统(“sh脚本。sh——帮助”);

System will execute the $command with @arguments and return to your script when finished. You may check $! for certain errors passed to the OS by the external application. Read the documentation for system for the nuances of how various invocations are slightly different.

系统将使用@arguments执行$命令,完成后返回到您的脚本。你可以检查美元!对于外部应用程序传递给操作系统的某些错误。请阅读系统文档,了解不同调用之间的细微差别。

2. Using exec

2。使用exec

This is very similar to the use of system, but it will terminate your script upon execution. Again, read the documentation for exec for more.

这非常类似于系统的使用,但是它将在执行时终止您的脚本。同样,请阅读exec的文档以了解更多信息。

3. Using backticks or qx//

3所示。使用引号或qx / /

my $output = `script.sh --option`;

我的美元= '脚本输出。sh -选项”;

my $output = qx/script.sh --option/;

我的输出美元= qx /脚本。sh——选项/;

The backtick operator and it's equivalent qx// excute the command and options inside the operator and return that commands output to STDOUT when it finishes.

反勾操作符,它等价于qx/ exlovely操作符中的命令和选项,并在结束时将命令输出返回给STDOUT。

There are also ways to run external applications through creative use of open, but this is advanced use; read the documentation for more.

也有通过创造性地使用open来运行外部应用程序的方法,但这是高级用法;更多信息请阅读文档。

#2


10  

Look at the open function in Perl - especially the variants using a '|' (pipe) in the arguments. Done correctly, you'll get a file handle that you can use to read the output of the command. The back tick operators also do this.

看看Perl中的open函数——特别是在参数中使用“|”(pipe)的变体。正确完成后,您将获得一个文件句柄,您可以使用它来读取命令的输出。后勾操作符也这样做。

You might also want to review whether Perl has access to the C functions that the command itself uses. For example, for ls -a, you could use the opendir function, and then read the file names with the readdir function, and finally close the directory with (surprise) the closedir function. This has a number of benefits - precision probably being more important than speed. Using these functions, you can get the correct data even if the file names contain odd characters like newline.

您可能还想检查Perl是否能够访问命令本身使用的C函数。例如,对于ls -a,您可以使用opendir函数,然后使用readdir函数读取文件名,最后使用closedir函数关闭目录。这有很多好处——精确可能比速度更重要。使用这些函数,即使文件名包含像newline这样的奇数字符,也可以获得正确的数据。

#3


8  

Examples

  1. `ls -l`;
  2. ' ls - l ';
  3. system("ls -l");
  4. 系统(ls - l);
  5. exec("ls -l");
  6. exec(ls - l);

#4


7  

From Perl HowTo, the most common ways to execute external commands from Perl are:

从Perl HowTo中,执行来自Perl的外部命令最常见的方法是:

  • my $files = `ls -la` — captures the output of the command in $files
  • 我的$files = ' ls -la ' -捕获命令在$files中的输出
  • system "touch ~/foo" — if you don't want to capture the command's output
  • 系统“touch ~/foo”—如果您不想捕获命令的输出
  • exec "vim ~/foo" — if you don't want to return to the script after executing the command
  • 执行“vim ~/foo”——如果您不想在执行命令之后返回到脚本
  • open(my $file, '|-', "grep foo"); print $file "foo\nbar" — if you want to pipe input into the command
  • 打开(我的$file, '|-', ' grep foo ');打印$file“foo\nbar”—如果您想将输入导入到命令中

#5


6  

There are a lot of ways you can call a shell command from perl script, such as:

有很多方法可以从perl脚本调用shell命令,例如:

  1. back tick ls which captures the output and gives back to you.
  2. 后勾选ls,捕捉输出并返回给您。
  3. system system('ls');
  4. 系统系统(ls);
  5. open
  6. 开放

Refer #17 here: perl programming tips

参考#17:perl编程技巧

#6


6  

As you become more experienced with using Perl, you'll find that there are fewer and fewer occasions when you need to run shell commands. For example, one way to get a list of files is to use Perl's built-in glob function. If you want the list in sorted order you could combine it with the built-in sort function. If you want details about each file, you can use the stat function. Here's an example:

随着您对使用Perl越来越有经验,您将发现需要运行shell命令的情况越来越少。例如,获取文件列表的一种方法是使用Perl的内置glob函数。如果您希望列表按排序顺序排列,您可以将它与内置的排序函数组合在一起。如果您想了解每个文件的详细信息,可以使用stat函数。这里有一个例子:

#!/usr/bin/perl

use strict;
use warnings;

foreach my $file ( sort glob('/home/grant/*') ) {
    my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)
        = stat($file);
    printf("%-40s %8u bytes\n", $file, $size);
}

#7


1  

You might want to look into open2 and open3 in case you need bidirectional communication.

如果需要双向通信,您可能需要研究open2和open3。