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

文章推薦指數: 80 %
投票人數:10人

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 cType=... React創建組件的三種方式及其區別 系列scripthold方法信息出現NPU寫法createReact推出後,出於不同的原因先後出現三種定義... 搜尋 基礎教學 Mysql入門 Sql入門 Android入門 Docker入門 Go語言入門 Ruby程式入門 Python入門 Python進階 Django入門 Python爬蟲入門 最近訪問 Perl執行shell命令的幾種方式及其區別 Codeforces+-+363B.+Fence+&+466C.+Number+of+Ways 第一章+Google軟件測試介紹 Mesos:服務發現與負載均衡 Java虛擬機(一):JVM的運行機制 jsp+pageEncoding屬性詳解 checkbox設定成單選模式 邁拓維矩:視頻矩陣切換器選購時需註意事項 根據先序和中序(中序和後序)確定二叉樹 解決:ImportError:+DLL+load+failed:+找不到指定的模組



請為這篇文章評分?