Perl执行shell命令的几种方式及其区别

There are many ways to execute external commands from Perl. The most commons are:

  • system function
  • exec function
  • backticks (``) operator
  • open function

All of these methods have different behaviour, so you should choose which one to use depending of your particular need. In brief, these are the recommendations:


method

use if ...

system()

you want to execute a command and don't want to capture its output

exec

you don't want to return to the calling perl script

backticks

you want to capture the output of the command

open

you want to pipe the command (as input or output) to your script



More detailed explanations of each method follows:

  • Using system()
  •  
    system() executes the command specified. It doesn't capture the output of the command.
    system() accepts as argument either a scalar or an array. If the argument is a scalar, system() uses a shell to execute the command ("/bin/sh -c command"); if the argument is an array it executes the command directly, considering the first element of the array as the command name and the remaining array elements as arguments to the command to be executed.
    For that reason, it's highly recommended for efficiency and safety reasons (specially if you're running a cgi script) that you use an array to pass arguments to system()
    Example:
            
     #-- calling 'command' with arguments
     system("command arg1 arg2 arg3");
 
     #-- better way of calling the same command
     system("command", "arg1", "arg2", "arg3");
           

          The return value is set in $?; this value is the exit status of the command as returned by the 'wait' call; to get the real exit status of the command you have to shift right by 8            the value of $? ($? >> 8).

           If the value of $? is -1, then the command failed to execute, in that case you may check the value of $! for the reason of the failure.

           Example:   

           
     system("command", "arg1");
     if ( $? == -1 )  
     {
       print "command failed: $!\n";
     }
     else{
       printf "command exited with value %d", $? >> 8;
     }
         

          

  •  
  • Using exec()
  •  
    The exec() function executes the command specified and never returns to the calling program, except in the case of failure because the specified command does not exist AND the exec argument is an array.
    Like in system(), is recommended to pass the arguments of the functions as an array.
           
  •  
  • Using backticks (``)
  •  
    In this case the command to be executed is surrounded by backticks. The command is executed and the output of the command is returned to the calling script.
    In scalar context it returns a single (possibly multiline) string, in list context it returns a list of lines or an empty list if the command failed.
    The exit status of the executed command is stored in $? (see system() above for details).
    Example:
            
     #-- scalar context
     $result = `command arg1 arg2`;

     #-- the same command in list context
     @result = `command arg2 arg2`;

           

          Notice that the only output captured is STDOUT, to collect messages sent to STDERR you should redirect STDERR to STDOUT

          Example:

          

    #-- capture STDERR as well as STDOUT
    $result = `command 2>&1`;
        

        

  •  
  • Using open()
  •  
    Use open() when you want to:
    - capture the data of a command (syntax: open("command |"))
    - feed an external command with data generated from the Perl script (syntax: open("| command"))
    Examples:
            
     #-- list the processes running on your system
     open(PS,"ps -e -o pid,stime,args |") || die "Failed: $!\n";
     while ( )
     {
     #-- do something here
     }
 
     #-- send an email to user@localhost
     open(MAIL, "| /bin/mailx -s test user\@localhost ") || die "mailx failed: $!\n";
     print MAIL "This is a test message";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Perl脚本和Shell脚本都是用于编写脚本的编程语言,但它们有以下区别: 1. 语法不同:Perl脚本使用Perl语言编写,而Shell脚本使用Shell语言编写。 2. 功能不同:Perl脚本主要用于文本处理、网络编程、系统管理等方面,而Shell脚本主要用于系统管理、自动化任务等方面。 3. 执行效率不同:Perl脚本通常比Shell脚本执行效率更高,因为Perl脚本是编译型语言,而Shell脚本是解释型语言。 4. 跨平台性不同:Perl脚本可以在不同的操作系统上运行,而Shell脚本通常只能在Unix/Linux系统上运行。 总之,Perl脚本和Shell脚本各有优缺点,需要根据具体的需求选择合适的编程语言。 ### 回答2: Perl脚本和Shell脚本是两种常用的脚本语言,相较于编译型语言,它们更适合用于操作系统管理或批处理等任务中。虽然两种语言都是解释型脚本语言,但它们的特点和用途不同。 首先,PerlShell语言的目的不同。Perl语言专注于文本处理和正则表达式,可以轻松实现字符串操作、文件处理、数据库操作等任务。相比之下,Shell语言的重点在于操作系统上的进程管理和命令行界面。它可以轻松实现文件操作、进程调用、脚本控制等任务。 其次,Perl语言具有更强大的编程能力。Perl支持面向对象和模块化编程,可以应对较复杂的任务。Perl还内置众多的函数和数据类型,例如数组、散列等,方便编写具有灵活性的脚本。反观Shell语言,由于设计初衷是为了快速实现简单的任务,因此其语法和功能相对简单。 此外,Perl语言的跨平台性更好。Perl语言的设计思想是“一次编写,到处运行”,因此可以在多种操作系统平台上运行。Shell脚本也可以实现跨平台,但需要考虑一些操作系统差异导致的兼容性问题。 综上所述,Perl脚本和Shell脚本的区别在于它们的设计思想和应用场景不同。Perl更适用于文本处理和复杂的编程任务,而Shell更适用于快速实现简单的系统管理和批处理任务。对于需要编写脚本自动化操作的工程师或管理员来说,正确选择适合的语言可以极大地提升工作效率。 ### 回答3: Perl脚本和Shell脚本在编程语言和编写风格上有很大区别Perl是一种高级编程语言,Shell是一个命令行解释器。 首先,Perl脚本通常在Windows,Linux,Unix系统中运行,它是一种通用的编程语言,可以处理文本和数据,进行正则表达式处理,解析XML和HTML,提供各种数据库访问模块和接口,而Shell脚本通常在Unix系统中运行,它更适合系统管理和shell命令拼接等。 其次,Perl脚本更注重面向对象的编程方式,注重程序复杂性,编写更具有可重用性的代码,而Shell脚本更注重管道管控和脚本的简易性,通过执行一系列的脚本实现较简单的任务完成。 此外,Perl脚本更加灵活,可以实现多种算法和数据结构,并且对于复杂的任务和数据分析具有优势,而Shell脚本更适合对命令行工具的集成与整合,并且在编写和执行上更加方便。 总的来说,Perl脚本和Shell脚本都有各自的优缺点和适用范围。Perl脚本更加强调程序设计和分析能力,而Shell脚本更注重快速实现和重用命令行工具的优势。编写脚本时应根据程序的需要进行选择,或者根据您自己的工作经验和编程习惯来进行选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值