C程序調用shell腳本共有三種方法

 C程序調用shell腳本共有三種法子 :system()、popen()、exec系列函數call_exec1.c ,內容爲:
system() 不用你自己去產生進程,它已經封裝了,直接加入自己的命令
exec 需要你自己 fork 進程,然後exec 自己的命令
popen() 也可以實現執行你的命令,比system 開銷小


1)system(shell命令或shell腳本路徑);
   
   system() 會調用fork()產生 子歷程,由子歷程來調用/bin/sh-c string來履行 參數string字符串所代表的命令,此命令履行 完後隨即返回原調用的歷程。在調用system()期間SIGCHLD 信號會被暫時擱置,SIGINT和SIGQUIT 信號則會被漠視 。
   
    返 回值:如果system()在調用/bin/sh時失敗則返回127,其他失敗原因返回-1。若參數string爲空指針(NULL),則返回非零值。 如果 system()調用成功 則最後會返回履行 shell命令後的返回值,但是此返回值也有可能爲system()調用/bin/sh失敗所返回的127,因 此最好能再反省 errno 來確認履行 成功 。
 
   system命令以其簡略 高效的作用得到很很廣泛 的利用 ,下面是一個例子

例:在/tmp/testDir/目錄下有shell腳本tsh.sh,內容爲
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->#!/bin/sh
wget $
1
echo 
"Done!"

在同層目錄下新建一個c文件

(以下代碼 可帶參數)

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->#include <stdio.h>
#include 
<string.h>
#include
<unistd.h>  
int main(int argc ,char *argv[])
{
    
char arg[300]="/tmp/testDir/tsh.sh ";
    
if ( argv[1!= NULL )
    {
        strcat(arg,argv[
1]);
        
        system(arg);
        printf(
"\ndone message in program\n");
        
return 1;
    }
    
else
    {
        printf(
"Error: Empty input\n");
        
return 0;
    }
    
}  

 
履行 效果 如下:
 

運行輸出
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
[root@localhost testDir]#gcc call_exec1.c 
-o call_exec1
[root@localhost testDir]#.
/call_exec1 http://www.baidu.com/img/logo-yy.gif
--2011-01-21 17:02:22--  http://www.baidu.com/img/logo-yy.gif
正在解析主機 www.baidu.com... 61.135.169.10561.135.169.125
Connecting to www.baidu.com
|61.135.169.105|:80... 已連接。
已發出 HTTP 請求,正在等待迴應... 
200 OK
長度:
1618 (1.6K) [image/gif]
Saving to: `logo
-yy.gif'

100%[======================================>1,618       --.-K/s   in 0.001s  

2011-01-21 17:02:34 (3.05 MB/s) - `logo-yy.gif' saved [1618/1618]

Done
!

done message 
in program




2)popen(char *command,char *type)   
 
    popen() 會調用fork()產生 子歷程,然後從子歷程中調用/bin/sh -c來履行 參數command的指令。參數type可應用 “r”代表讀取,“w”代表寫入。遵循此type值,popen()會建立 管道連到子歷程的標準 輸出設備 或標準 輸入設備 ,然後返回一個文件指針。隨後歷程便可利用 此文件指針來讀取子歷程的輸出設備 或是寫入到子歷程的標準 輸入設備 中。此外,所有應用 文 件指針(FILE*)操作的函數也都可以應用 ,除了fclose()以外。
 
    返回值:若成功 則返回文件指針,否則返回NULL,差錯 原因存於errno中。注意:在編寫具SUID/SGID權限的程序時請儘量避免應用 popen(),popen()會繼承環境變量,通過環境變量可能會造成系統安全的問題。
 
例:C程序popentest.c內容如下:
  
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> #include<stdio.h>
    main
    {
        FILE 
* fp;
        charbuffer[
80];
        fp
=popen(“~/myprogram/test.sh”,”r”);
        fgets(buffer,
sizeof(buffer),fp);
        printf(“
%s”,buffer);
        pclose(fp);
    }



 
 
履行 效果 如下:
 
運行輸出
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--> #include<stdio.h>
    main
    {
        FILE 
* fp;
        charbuffer[
80];
        fp
=popen(“~/myprogram/test.sh”,”r”);
        fgets(buffer,
sizeof(buffer),fp);
        printf(“
%s”,buffer);
        pclose(fp);
    }


from :

http://blog.163.com/redhatroot@126/blog/static/1730563022010102911243458/
  作部分修改

發佈了3 篇原創文章 · 獲贊 13 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章