1.5 从标准输入读命令并执行
2014-07-15 09:17
323 查看
proc/shell1.c
#include "apue.h" #include <sys/wait.h> int main(void) { char buf[MAXLINE]; /* from apue.h */ pid_t pid; int status; printf("%% "); /* print prompt (printf requires %% to print %) */ while (fgets(buf, MAXLINE, stdin) != NULL) { if (buf[strlen(buf) - 1] == '\n') buf[strlen(buf) - 1] = 0; /* replace newline with null */ if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { /* child */ execlp(buf, buf, (char *)0); err_ret("couldn't execute: %s", buf); exit(127); } /* parent */ if ((pid = waitpid(pid, &status, 0)) < 0) err_sys("waitpid error"); printf("%% "); } exit(0); }
相关文章推荐
- 1.5 从标准输入读取命令并执行
- 从标准输入读入命令并执行【UNIX 环境高 3ff0 级编程】
- 1-5 从标准输入读入命令并执行
- 从标准输入中读命令并执行
- 1.8 从标准输入读命令并执行2
- 从标准输入读命令并执行
- linux:从标准输入读取命令并执行
- APUE 1-10 从标准输入读命令并执行
- 笔记1-3: 从标准输入读取命令并执行
- APUE 1-7 从标准输入读命令并执行
- UNIX环境高级编程1-5从标准输入读命令并执行
- UNIX环境高级编程 chat1 从标准输入中读取命令执行
- 笔记1-3: 从标准输入读取命令并执行
- 重复执行以前输入命令的快捷方法
- CentOS下利用sshpass不用手动输入密码远程执行命令
- linux tr命令对来自标准输入的字符进行转换、删除及压缩
- 一起学Shell之(六)输入、输出、文件与命令执行 推荐
- 输入与输出,多次执行命令,管道
- 实现执行scp命令时不需要输入用户密码
- MySQL客户端如何忽略已经输入的多行命令,让其不执行!