perl呼叫shell命令並獲取輸出
阿新 • • 發佈:2019-02-04
1 system
perl也可以用system呼叫shell的命令,它和awk的system一樣,返回值也是它呼叫的命令的退出狀態.如果向system傳遞一個字串作引數,則perl會呼叫shell來執行這個命令,在這個字串內也就不可以有perl的變量了;如果傳遞多個字串作引數,則perl會自己執行這個命令,且可以傳遞perl自己的變數給它,因為perl會對這些變數擴充套件成它們的值
注意上面只輸出了三次grep.test的資訊,且最後一行是空行,可見最後一個system呼叫的ls有錯誤,因為只傳遞一個字串給system,perl是不會把這個字串裡出現的變數進行擴充套件的.
perl也可以用system呼叫shell的命令,它和awk的system一樣,返回值也是它呼叫的命令的退出狀態.如果向system傳遞一個字串作引數,則perl會呼叫shell來執行這個命令,在這個字串內也就不可以有perl的變量了;如果傳遞多個字串作引數,則perl會自己執行這個命令,且可以傳遞perl自己的變數給它,因為perl會對這些變數擴充套件成它們的值
$ perl system("ls -l grep.test"); $long = "-l"; $file = "grep.test"; system "ls", $long, $file; system("ls $long $file"); -rw-r--r-- 1 Administrator None 25 Feb 27 21:37 grep.test -rw-r--r-- 1 Administrator None 25 Feb 27 21:37 grep.test -rw-r--r-- 1 Administrator None 25 Feb 27 21:37 grep.test |
注意上面只輸出了三次grep.test的資訊,且最後一行是空行,可見最後一個system呼叫的ls有錯誤,因為只傳遞一個字串給system,perl是不會把這個字串裡出現的變數進行擴充套件的.