Perl調(diào)用shell命令方法小結
更新時間:2014年05月10日 12:19:49 作者:
這篇文章主要介紹了Perl調(diào)用shell命令方法小結,需要的朋友可以參考下
一、system
perl也可以用system調(diào)用shell的命令,它和awk的system一樣,返回值也是它調(diào)用的命令的退出狀態(tài).
復制代碼 代碼如下:
[root@AX3sp2 ~]# cat aa.pl
#! /usr/bin/perl -w
$file = "wt.pl";
system("ls -l wt.pl");
$result = system "ls -l $file";
print "$result \n"; #輸出命令的退出狀態(tài)
system "date";
[root@AX3sp2 ~]# perl aa.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
-rwxr-xr-x 1 root root 126 12-16 15:12 wt.pl
0
2010年 12月 16日 星期四 15:58:34 CST
二、反引號
perl的system函數(shù)和awk的一樣不能夠返回命令的輸出.
要得到命令的輸出,就得使用和shell本身一樣的命令: ` `
復制代碼 代碼如下:
[root@AX3sp2 ~]# cat bb.pl
#! /usr/bin/perl
print `date`;
print "this is test \n";
[root@AX3sp2 ~]# perl bb.pl
2010年 12月 16日 星期四 15:51:59 CST
this is test
三、exec
最后,perl還可以使用exec來調(diào)用shell的命令. exec和system差不多,不同之處在于,調(diào)用exec之后,perl馬上就退出,而不會去繼續(xù)執(zhí)行剩下的代碼
復制代碼 代碼如下:
[root@AX3sp2 ~]# cat cc.pl
#! /usr/bin/perl
exec ("echo this is test");
print "good bye !\n"; #這句話不會被輸出
[root@AX3sp2 ~]# perl cc.pl
this is test
您可能感興趣的文章:
- perl批量查詢ip歸屬地的方法代碼
- perl中my和our的區(qū)別分析
- 解析posix與perl標準的正則表達式區(qū)別
- Perl中的正則表達式介紹
- 為Java程序員準備的10分鐘Perl教程
- Perl內(nèi)置特殊變量總結
- Perl Sort函數(shù)用法總結和使用實例
- perl 文件測試操作符匯總
- Perl使用nginx FastCGI環(huán)境做WEB開發(fā)實例
- perl查找進程PID的例子
- Perl一句話命令行編程中常用參數(shù)總結
- cpanm安裝及Perl模塊安裝教程
- Windows和Linux系統(tǒng)下perl連接SQL Server數(shù)據(jù)庫的方法
- 7個perl數(shù)組高級操作技巧分享
- Perl函數(shù)(子程序)學習筆記
- Perl Substr()函數(shù)及函數(shù)的應用