1. 程式人生 > >Perl執行shell命令的幾種方式及其區別

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 [email protected]
     open(MAIL, "| /bin/mailx -s test user\@localhost ") || die "mailx failed: $!\n";
     print MAIL "This is a test message";

相關推薦

Perl執行shell命令方式及其區別

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

Spring容器建立物件的方式及其區別

1. 通過類路徑下的配置檔案獲取ApplicationContext    //在建立容器的時候建立物件          特點:佔用記憶體,但效率高 ApplicationContext ac=new ClassPathXmlApplicationContext("cl

實現多執行緒有兩方式及其區別

實現多執行緒有兩種方式:(自JDK1.5之後有三種,最後一種並不常用)    1.繼承Thread類    2.實現Runnable介面(Callable介面) 一個類如果實現了Runnable介面或者繼承了Thread類,那麼它就是一個多執行緒類,如果是要實現多執行緒,還需要重寫run()方法,所以ru

mysql 命令列中執行sql的方式

1.直接輸入sql執行mysql> select now(); +---------------------+ | now()               | +---------------------+ | 2013-09-18 13:55:45 | +-----

設計模式之單例模式【內附物件例項化方式、實現執行緒安全方式

繼續來複習常用的設計模式-單例模式,順便回憶一下執行緒安全的幾種實現方式。 一、什麼是單例模式 單例模式,簡單常用的一種設計模式,也很好的體現了程式碼控制物件在記憶體數量的一種方式,主要分2種實現方式: ①餓漢式,執行緒安全 ②懶漢式,執行緒不安全(新增鎖機制,可以實現執行緒安全)

bat批處理執行python 的方式 ———— 批處理, python打包成 exe檔案

第一種方式: @echo off C: cd C:\Users\ldl\Desktop start python test100.py start python 1.py start python 1.py 10 start python 1.py 100 exit 第二種方

開啟執行緒的方式

目錄 執行緒開啟方式(一)-非同步委託          建立執行緒的一種簡單方式是定義一個委託,並非同步呼叫它。 委託是方法的型別安全的引用。Delegate類 還支援非同步地呼叫方法。在後臺,Delegate類會建立一個執行任務的執行緒。

WPF實現動畫的方式及其小案例

WPF實現動畫的方式: 基於計時器的動畫         建立一個定時器,然後根據其頻率迴圈呼叫函式或者一個事件處理函式,在這個函式中可以手工更新目標屬性,直到達到最終值,這時可以停止計時器。 案例: 效果圖:

執行緒實現的兩方式及其區別

繼承Thread public class Demo2_Thread { public static void main(String[] args) { MyThrea

實現執行緒的兩方式及其結合內部類的多種變態

初學Java三十多天的小白,總結僅供參考!有錯誤希望大家踴躍指出,不勝感激!實現執行緒的方式一:繼承Thread類【1】正常形式下(最容易理解的形式)public class TestDemo { public static void main(String[] args)

Java建立執行緒的三方式及其對比

Java中建立執行緒主要有三種方式: 一、繼承Thread類建立執行緒類 (1)定義Thread類的子類,並重寫該類的run方法,該run方法的方法體就代表了執行緒要完成的任務。因此把run()方法稱為執行體。 (2)建立Thread子類的例項,即建立了執行緒物

Maven學習筆記(六)-使用Eclipse建立Maven WEB專案以及執行專案的方式

一、建立一個專案 在eclipse選單欄中選擇“File”---“News”---“Other”選單項,就會開啟如下對話視窗,在視窗中選擇“Maven”節點下“Maven Project”條目,如下

Java 建立執行緒的三方式及其對比

Java中建立執行緒主要有三種方式: 一、繼承Thread類建立執行緒類 (1)定義Thread類的子類,並重寫該類的run方法,該run方法的方法體就代表了執行緒要完成的任務。因此把run()方法稱為執行體。 (2)建立Thread子類的例項,即建立了執行緒物件。 (3)呼

iOS NSthread & Thread 開啟執行緒的方式

一、開啟執行緒執行指定物件的方法 /** 引數1: 執行引數2方法的物件 引數2: 開啟執行緒後執行的方法 引數3: 傳遞的物件資料(引數2的方法可以直接用) */ // OC - (

Java併發執行任務的方式

背景 在編寫業務程式碼時經常遇到併發執行多個任務的需求,因為序列執行太慢,會影響業務程式碼效能。特別對於直接面向普通使用者的業務來說使用者體驗至關重要,保證使用者體驗重要的一點是要“快”。業務程式碼中經常需要呼叫其它業務介面或者同時從多個數據源取資料再處理等,

4. 執行緒同步方式

執行緒同步的方式主要有以下四種:臨界區(Critical Section)、互斥量(Mutex)、訊號量(Semaphore)、事件(Event)的區別。 他們的主要區別和特點如下: 1)臨界區:通過對多執行緒的序列化來訪問公共資源或一段程式碼,速度快,適合控制資料訪問。在任意時刻只允許一個執行緒對共

iOS 執行緒同步方式

多執行緒同步目的有以下幾個方面:第一,對一段程式碼的執行進行保護,如果同時執行一段程式碼,中間的臨時變數可能會互相干擾造成結果不對;第二,對資源的保護,多個執行緒執行不同的程式碼,但是可能涉及同一個資源;第三,訊息傳遞,一個執行緒通知另外一個執行緒發生了一件事。

Java中建立執行緒的方式以及執行緒同步的方式

執行緒同步自己及基本就用過Thread和Runnable這兩種方式,還有其他很多方式如下: Executor框架簡介 建立執行緒有幾種不同的方式?你喜歡哪一種?為什麼? 而執行緒同步會用的方式就更少了,只會synchronized,其他方式如下: 關於執

【Java】—— java Web 啟動時自動執行程式碼的方式(總有些程式碼需要在虛擬機器啟動時執行)

Web容器啟動後執行程式碼的幾種方式其執行順序為:4===>5===>1===>2===>3即指定init-method的Bean開始執行接著實現Spring的Bean後置處理器開始執行然後是Servlet的監聽器執行再接下來是Servlet的過濾器執

建立執行緒的方式,以及為什麼啟動執行緒不用run,而用start方法!!!

首先,我們大家都知道,建立執行緒的兩種蛀主要的方法,一種是繼承Thread類,另一種是實現Runnable介面。對於第一種建立執行緒的方式有兩個不足: 1.當前執行緒重寫run方法定義該執行緒要完成的工作,這就導致了任務是定義線上程內部的,於是執行緒與任務有一個強耦合關