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

任何人都可以给我一个例子,说明如何调用shell命令,在Perl脚本中说’ls -a’,以及检索命令输出的方式。

解决方法

How to run a shell script from a Perl program

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

For example:

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

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

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.

2. Using exec

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

3. Using backticks or qx//

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

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

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.

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

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...