shell-like program(shell程序的基本实施部分)
2015-08-16 10:09
501 查看
直接上代码:
很多时候不喜欢csdn这种代码显示,再贴上vim的
测试:
这个小程序的功能限制就是不能给命令传入参数,比如我们不能指定目录名给list,我们只能在当前工作目录中运行。
#include "apue.h" #include <sys/wait.h> int main(void) { char buf[MAXLINE]; /* form apue.h 4096 */ pid_t pid; int status; printf("%% "); /* print promt (printf requires %% to print %) */ while (fgets(buf, MAXLINE, stdin) != NULL) { if (buf[strlen(buf) - 1] == '\n') { buf[strlen(buf) - 1] = 0; /* replace newline while null */ } if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid == 0) { 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); }
很多时候不喜欢csdn这种代码显示,再贴上vim的
测试:
这个小程序的功能限制就是不能给命令传入参数,比如我们不能指定目录名给list,我们只能在当前工作目录中运行。
相关文章推荐
- Linux Shell脚本编程初体验
- Shell及其Shell编程
- Classic Shell 4.2.4 中文版已经发布
- linux shell 25问
- [linux服务器][bash]让切换目录更方便
- 用shell写的俄罗斯方块游戏
- linux_shell_类似sql的orderby 取最大值
- Bash shell位置参数
- 一句shell命令搞定代码行数统计
- hello shell
- Linux C语言程序设计(十二)——Shell编程的基础知识
- 常用shell
- Linux学习笔记(18) Shell编程之流程控制
- newlisp获得bash该命令的退出状态
- bash常用
- Xshell配置ssh免密码登录-密钥公钥(Public key)与私钥(Private Key)登录
- 利用Shell脚本进行文件内容去重
- 利用Shell脚本进行文件内容去重
- BASH
- XShell5 连接虚拟机上的Lunix