Perl執行shell命令的幾種方式及其區別 - 程式人生
文章推薦指數: 80 %
There are many ways to execute external commands from Perl. The most commons are: system function; exec function; backticks (``) operator ...
程式人生>>Perl執行shell命令的幾種方式及其區別
Perl執行shell命令的幾種方式及其區別
阿新••發佈:2019-01-28
TherearemanywaystoexecuteexternalcommandsfromPerl.Themostcommonsare:
system functionexec functionbackticks(``) operatoropen functionAllofthesemethodshavedifferentbehaviour,soyoushouldchoosewhichonetousedependingofyourparticularneed.Inbrief,thesearetherecommendations:
method
useif...
system()
youwanttoexecuteacommandanddon'twanttocaptureitsoutput
exec
youdon'twanttoreturntothecallingperlscript
backticks
youwanttocapturetheoutputofthecommand
open
youwanttopipethecommand(asinputoroutput)toyourscript
Moredetailedexplanationsofeachmethodfollows:
Usingsystem()
system()executesthecommandspecified.Itdoesn'tcapturetheoutputofthecommand.
system()acceptsasargumenteitherascalaroranarray.Iftheargumentisascalar,system()usesashelltoexecutethecommand("/bin/sh-ccommand");iftheargumentisanarrayitexecutesthecommanddirectly,consideringthefirstelementofthearray
asthecommandnameandtheremainingarrayelementsasargumentstothecommandtobeexecuted.
Forthatreason,it'shighlyrecommendedforefficiencyandsafetyreasons(speciallyifyou'rerunningacgiscript)thatyouuseanarraytopassargumentstosystem()Example: #--calling'command'witharguments
system("commandarg1arg2arg3");
#--betterwayofcallingthesamecommand
system("command","arg1","arg2","arg3");
Thereturnvalueissetin $?;thisvalueistheexitstatusofthecommandasreturnedbythe'wait'call;togettherealexitstatusofthecommandyouhavetoshiftrightby8 thevalue
of$?($?>>8).
Ifthevalueof $? is-1,thenthecommandfailedtoexecute,inthatcaseyoumaycheckthevalueof $! forthereasonofthefailure.
Example:
system("command","arg1");
if($?==-1)
{
print"commandfailed:$!\n";
}
else{
printf"commandexitedwithvalue%d",$?>>8;
}
Usingexec()
Theexec()functionexecutesthecommandspecifiedandneverreturnstothecallingprogram,exceptinthecaseoffailurebecausethespecifiedcommanddoesnotexistANDtheexecargumentisanarray.
Likeinsystem(),isrecommendedtopasstheargumentsofthefunctionsasanarray.
Usingbackticks(``)
Inthiscasethecommandtobeexecutedissurroundedbybackticks.Thecommandisexecutedandtheoutputofthecommandisreturnedtothecallingscript.
Inscalarcontextitreturnsasingle(possiblymultiline)string,inlistcontextitreturnsalistoflinesoranemptylistifthecommandfailed.
Theexitstatusoftheexecutedcommandisstoredin $? (seesystem()abovefordetails).Example:#--scalarcontext
$result=`commandarg1arg2`;
#--thesamecommandinlistcontext
@result=`commandarg2arg2`;.
NoticethattheonlyoutputcapturedisSTDOUT,tocollectmessagessenttoSTDERRyoushouldredirectSTDERRtoSTDOUT
Example:
#--captureSTDERRaswellasSTDOUT
$result=`command2>&1`;
Usingopen()
Useopen()whenyouwantto:
-capturethedataofacommand(syntax:open("command|"))
-feedanexternalcommandwithdatageneratedfromthePerlscript(syntax:open("|command"))Examples: #--listtheprocessesrunningonyoursystem
open(PS,"ps-e-opid,stime,args|")||die"Failed:$!\n";
while()
{
#--dosomethinghere
}
#--sendanemailto[email protected]
open(MAIL,"|/bin/mailx-stestuser\@localhost")||die"mailxfailed:$!\n";
printMAIL"Thisisatestmessage";
梅爾頻率倒譜系數(MFCC)學習筆記
«上一篇
SublimeText3之——markdown下一篇»
相關推薦
Perl執行shell命令的幾種方式及其區別
Therearemanywaystoex...
Spring容器建立物件的幾種方式及其區別
1.通過類路徑下的配置檔案獲取ApplicationContext
//在建立容器的時候建立物件 ...
多執行緒實現的兩種方式及其區別
繼承Thread
publicclassD...
實現多執行緒有兩種方式及其區別
實現多執行緒有兩種方式:(自JDK1.5之後有三種,最後一種並不常用)
1.繼承Thread類
2.實現Runnable介面(Callable介面...
js中陣列遍歷的幾種方法及其區別
第一種最常用的:for迴圈
for(j= 0; j
延伸文章資訊
- 1How to Run Perl Scripts in Linux Command Line
Learn to learn Perl scripts in Linux command line. Also learn a few tricks about running Perl com...
- 2Linux perl command help and examples - Computer Hope
The perl command is the interpreter of the Perl programming language. Description. "Perl" officia...
- 3How can I call a shell command in my Perl script?
System will execute the $command with @arguments and return to your script when ... I have been u...
- 4How to Install Perl on Linux? - GeeksforGeeks
Perl is a general-purpose, high level interpreted and dynamic programming language. Perl was orig...
- 5Running Perl Scripts on Linux Systems
There are many ways to run Perl scripts on Linux: 1. Run the "perl" command with the Perl script ...