system函数-linux
生活随笔
收集整理的這篇文章主要介紹了
system函数-linux
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
函數介紹:
system - execute a shell commandSYNOPSIS#include <stdlib.h>int system(const char *command);DESCRIPTIONThe system() library function uses fork(2) to create a child processthat executes the shell command specified in command using execl(3) asfollows:execl("/bin/sh", "sh", "-c", command, (char *) 0);system() returns after the command has been completed.bash? -c? cal
bash -c binfile
andrew@andrew-Thurley:~$ bash -c cal八月 2018 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31?
測試函數:
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <errno.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/wait.h>char *cmd1 = "/bin/date";int main(void) {system("clear");system(cmd1);return 0; }測試結果:
2018年 08月 28日 星期二 23:47:07 CST andrew@andrew-Thurley:~/work/apue.2e$編寫自己的mysystem函數: bash -c binfilename
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <errno.h> #include <stdio.h> #include <fcntl.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/wait.h>char *cmd1 = "date > s1.txt"; char *cmd2 = "date > s2.txt";void mysystem(char *cmd);int main(void) {system("clear");system(cmd1);mysystem(cmd2);return 0; }void mysystem(char *cmd) {pid_t pid;if(pid = fork() < 0){perror("fork error");exit(1);}else if(pid == 0){if(execlp("/bin/bash", "bin/bash", "-c", cmd, NULL) < 0){perror("execlp error");exit(1);}}wait(0);}?
總結
以上是生活随笔為你收集整理的system函数-linux的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安装并配置ssh服务器
- 下一篇: .bashrc和.vimrc以及一些比较