1system
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是不會把這個字串裡出現的變數進行擴充套件的.
還有一種方法 可以自己拼出需要執行的命令
my $cmd="tree " . $dir . " | grep files | awk 'begin ' > /tmp/filecount.tmp";
system($cmd);
這樣就可以把我們的變數$dir加入到命令語句了,下面的反引號執行是不能有我們程式中的變數的 而且輸出重定向到了tmp檔案中,我們再去讀出輸出就可以了~~~
2反引號
perl的system函式和awk的一樣不能夠返回命令的輸出.要得到命令的輸出,就得使用和shell本身一樣的命令:` ` (一對反引號,鍵盤上esc鍵下面的那個)
$ perl
$now = `date`;
print $now;
sun mar 8 23:08:24 2009
3exec
最後,perl還可以使用exec來呼叫shell的命令. exec和system差不多,不同之處在於,呼叫exec之後,perl馬上就退出,而不會去繼續執行剩下的**
$ perl
exec("date");
print "this line will never be seen.";
sun mar 8 23:16:54 2009
Perl呼叫shell命令方法小結
一 system perl也可以用system呼叫shell的命令,它和awk的system一樣,返回值也是它呼叫的命令的退出狀態.複製 如下 root ax3sp2 cat aa.pl usr bin perl w file wt.pl system ls l wt.pl result syste...
Perl呼叫shell命令方法小結
一 system perl也可以用system呼叫shell的命令,它和awk的system一樣,返回值也是它呼叫的命令的退出狀態.複製 如下 root ax3sp2 cat aa.pl usr bin perl w file wt.pl system ls l wt.pl result syste...
C語言執行Linux的shell命令並獲得返回值
popen函式執行命令後,返回乙個指向該命令輸出的檔案控制代碼,接下來就可以用fgets等檔案操作函式去讀取輸出結果。include file popen const char command,const char type int pclose file stream type的引數只能是 r 或...