C程式呼叫shell指令碼共有三種方法

2021-07-31 03:57:33 字數 752 閱讀 1599

c程式呼叫shell指令碼共有三種方法:

1、system()   #直接放入shell指令碼路徑

在c_call_shell.c檔案

1#include

2 #include

3 #include

4 #include

56 void c_call_shell(char *shell_path)

7 12

13 int main(int argc,char* argv)

14 shell.sh程式

#!/bin/sh

2 #shell.sh

34 echo "hello,world"

2、popen(char *command,char *type)

type為r或w

command 為shell指令碼路徑

popen()會呼叫fork()產生子程序,子程序呼叫/bin/sh -c來執行引數command指令。引數type可使用「r」代表讀,「w」代表寫。

popen()會建立管道連到子程序的標準輸出裝置或標準輸入裝置,然後返回乙個檔案指標,所有對檔案指標的操作都可以用,除了fclose()外。

c_print_shell.c 

#include

2 int main(int argc,char *argv)

3 shell.sh

#!/bin/sh

2 #shell.sh

34 echo "hello,world"

C程式呼叫shell指令碼共有三種方法

c程式呼叫shell指令碼共有三種法子 system popen exec系列函式 call exec1.c 內容為 system 不用你自己去產生程序,它已經封裝了,直接加入自己的命令 exec 需要你自己 fork 程序,然後exec 自己的命令 popen 也可以實現執行你的命令,比syste...

C語言呼叫SHELL指令碼

在linux 環境下shell指令碼具有非常強大的功能!使用shell可以很方便的使用和管理linux系統,最近學習了一點shell知識,所以一直在 想要是可以在c c 中呼叫shell指令碼那該有多好啊!因為使用c c 要幾百行 才能搞定的!使用shell只要短短十幾行就可以輕鬆搞定!includ...

Shell指令碼(三) Shell指令碼入門

1 指令碼格式 指令碼以 bin bash開頭 指定解析器 2 第乙個shell指令碼 helloworld 1 需求 建立乙個shell指令碼,輸出helloworld 2 案例實操 atguigu hadoop101 datas touch helloworld.sh atguigu hadoo...