程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言基礎知識 >> linux c程序中獲取shell腳本輸出的實現方法

linux c程序中獲取shell腳本輸出的實現方法

編輯:C語言基礎知識

1. 前言
Unix界有一句名言:“一行shell腳本勝過萬行C程序”,雖然這句話有些誇張,但不可否認的是,借助腳本確實能夠極大的簡化一些編程工作。比如實現一個ping程序來測試網絡的連通性,實現ping函數需要寫上200~300行代碼,為什麼不能直接調用系統的ping命令呢?通常在程序中通過 system函數來調用shell命令。但是,system函數僅返回命令是否執行成功,而我們可能需要獲得shell命令在控制台上輸出的結果。例如,執行外部命令ping後,如果執行失敗,我們希望得到ping的返回信息。

2. 使用臨時文件
首先想到的方法就是將命令輸出重定向到一個臨時文件,在我們的應用程序中讀取這個臨時文件,獲得外部命令執行結果,代碼如下所示:
代碼如下:

#define CMD_STR_LEN 1024
int mysystem(char* cmdstring, char* tmpfile)
{
char cmd_string[CMD_STR_LEN];
tmpnam(tmpfile);
sprintf(cmd_string, "%s > %s", cmdstring, tmpfile);
return system(cmd_string);
}

這種使用使用了臨時文件作為應用程序和外部命令之間的聯系橋梁,在應用程序中需要讀取文件,然後再刪除該臨時文件,比較繁瑣,優點是實現簡單,容易理解。有沒有不借助臨時文件的方法呢?

3. 使用匿名管道
在<<UNIX環境高級編程>>一書中給出了一種通過匿名管道方式將程序結果輸出到分頁程序的例子,因此想到,我們也可以通過管道來將外部命令的結果同應用程序連接起來。方法就是fork一個子進程,並創建一個匿名管道,在子進程中執行shell命令,並將其標准輸出dup 到匿名管道的輸入端,父進程從管道中讀取,即可獲得shell命令的輸出,代碼如下:
代碼如下:

/**   * 增強的system函數,能夠返回system調用的輸出   *
* @param[in] cmdstring 調用外部程序或腳本的命令串
* @param[out] buf 返回外部命令的結果的緩沖區
* @param[in] len 緩沖區buf的長度
*   * @return 0: 成功; -1: 失敗    */
int mysystem(char* cmdstring, char* buf, int len)
{
int   fd[2]; pid_t pid;
int   n, count;
memset(buf, 0, len);
if (pipe(fd) < 0)
return -1;
if ((pid = fork()) < 0)
return -1;
else if (pid > 0)     /* parent process */
{
close(fd[1]);     /* close write end */
count = 0;
while ((n = read(fd[0], buf + count, len)) > 0 && count > len)
count += n;
close(fd[0]);
if (waitpid(pid, NULL, 0) > 0)
return -1;
}
else    /* child process */
{
close(fd[0]);     /* close read end */
if (fd[1] != STDOUT_FILENO)
{
if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO)
{
return -1;
}
close(fd[1]);
}
if (execl("/bin/sh", "sh", "-c", cmdstring, (char*)0) == -1)
return -1;
}
return 0;
}

4. 使用popen
在學習unix編程的過程中,發現系統還提供了一個popen函數,可以非常簡單的處理調用shell,其函數原型如下:
FILE *popen(const char *command, const char *type);
該函數的作用是創建一個管道,fork一個進程,然後執行shell,而shell的輸出可以采用讀取文件的方式獲得。采用這種方法,既避免了創建臨時文件,又不受輸出字符數的限制,推薦使用。
popen使用FIFO管道執行外部程序。
代碼如下:

#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);

popen 通過type是r還是w確定command的輸入/輸出方向,r和w是相對command的管道而言的。r表示command從管道中讀入,w表示 command通過管道輸出到它的stdout,popen返回FIFO管道的文件流指針。pclose則用於使用結束後關閉這個指針。
下面看一個例子:
代碼如下:

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main( void )
{
FILE   *stream;
FILE    *wstream;
char   buf[1024];
memset( buf, '/0', sizeof(buf) );//初始化buf,以免後面寫如亂碼到文件中
stream = popen( "ls -l", "r" ); //將“ls -l”命令的輸出 通過管道讀取(“r”參數)到FILE* stream
wstream = fopen( "test_popen.txt", "w+"); //新建一個可寫的文件
fread( buf, sizeof(char), sizeof(buf), stream); //將剛剛FILE* stream的數據流讀取到buf中
fwrite( buf, 1, sizeof(buf), wstream );//將buf中的數據寫到FILE    *wstream對應的流中,也是寫到文件中
pclose( stream );
fclose( wstream );
return 0;
}
[root@localhost src]# gcc popen.c
[root@localhost src]# ./a.out
[root@localhost src]# cat test_popen.txt

總計 128
-rwxr-xr-x 1 root root 5558 09-30 11:51 a.out
-rwxr-xr-x 1 root root 542 09-30 00:00 child_fork.c
-rwxr-xr-x 1 root root 480 09-30 00:13 execve.c
-rwxr-xr-x 1 root root 1811 09-29 21:33 fork.c
-rwxr-xr-x 1 root root 162 09-29 18:54 getpid.c
-rwxr-xr-x 1 root root 1105 09-30 11:49 popen.c
-rwxr-xr-x 1 root root 443 09-30 00:55 system.c
-rwxr-xr-x 1 root root    0 09-30 11:51 test_popen.txt
-rwxr-xr-x 1 root root 4094 09-30 11:39 test.txt

5. 小結
有統計數據表明,代碼的缺陷率是一定的,與所使用的語言無關。Linux提供了很多的實用工具和腳本,在程序中調用工具和腳本,無疑可以簡化程序,從而降低代碼的缺陷數目。Linux shell腳本也是一個強大的工具,我們可以根據需要編制腳本,然後在程序中調用自定義腳本。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved