简单shell实现
http://blog.csdn.net/lishuhuakai/article/details/11928055
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
#define MAXLINE 80 void setup (char inputBuffer[], char *argv[])
{ int i, j;
char *p;
i = ;
for (p = inputBuffer; ;p++)/*将字符命令一段一段拆分开来,如输入ls -l,则argv[0]= "ls",argv[1]="-l",argv[2]=NULL*/
{
argv[i++] = p;
while (*p != ' ' && *p != '\0')
p++;
if (*p == '\0')
{
break;
}
*p = '\0';
}
argv[i] = NULL;/*最后一个参数须用空指针NULL作结束*/
execvp (argv[], argv);/*执行命令,argv[0]放着执行的命令,其余的argv放着参数*/
} int main()
{ char inputBuffer[MAXLINE];
char *argv[MAXLINE / - ]; while ()
{
printf ("COMMAND->");
gets (inputBuffer);/*输入命令*/
setup (inputBuffer, argv);
printf ("扑街!");
}
return ;
}
#include <stdio.h>
#include <unistd.h>
//#define SIZE 20
#define MAXLINE 80
char *p; void setup (char inputBuffer[], char *argv[])
{
int i, j;
i = ;
for (p = inputBuffer; ;p++)/*一个一个地将输入字符分开*/
{
argv[i++] = p;
while (*p != ' ' && *p != '\0')
p++;
if (*p == '\0')
{
break;
}
*p = '\0';
}
argv[i] = NULL;
pid_t pid
pid = fork();/*新建一个子进程*/
if (pid == )/*对于子进程,执行输入的命令*/
{
execvp (argv[], argv);
printf ("command not found\n");/*如果execvp执行失败的话,会返回-1,也就是这句话会被执行*/
} else if( pid > )/*对于父进程,等待*/
{
wait(NULL);/*父进程等待子进程执行完*/
} else /*否则的话,就出错了*/
{
printf ("fork error\n");
}
} int main()
{
char inputBuffer[MAXLINE];
char *argv[MAXLINE / - ]; while ()
{
printf ("COMMAND->");
gets (inputBuffer);
/*关于scanf和gets,scanf对于输入的字符遇到空格或回车就结束了,而gets对于输入的空格也接受*/
setup (inputBuffer, argv); }
return ;
}
#include <stdio.h>
#include <unistd.h>
#define MAXLINE 100
char *p;
char buffer[]; void setup (char inputBuffer[], char *argv[])
{
int i=,j;
for (p = inputBuffer; ;p++)
{
argv[i++] = p;
while (*p != ' ' && *p != '\0') p++;
if (*p == '\0') break;
*p = '\0';
} argv[i] = NULL;
int pid;
pid = fork();/*新建一个子进程*/
if (pid == )/*对于子进程,执行输入的命令*/
{
execvp (argv[], argv);
printf ("command not found\n");
}
else if( pid > ) wait(NULL);
else printf ("fork error\n");
} void getLoc()
{
//获取当前的工作目录,注意:长度必须大于工作目录的长度加一
char *p = getcwd(buffer , );
char *dir = NULL; printf("buffer:%s p:%s size:%d \n" , buffer , p , strlen(buffer));
//获取当前工作目录的名字
dir = (char *)get_current_dir_name();
printf("dir:%s \n" , dir); char *twd = NULL ; twd = getwd(buffer); //printf("buffer:%s twd:%s \n" , buffer , twd);
} int main()
{
char inputBuffer[MAXLINE];
char *argv[MAXLINE/ - ];
while ()
{
getLoc();
printf ("%s->",buffer);
gets (inputBuffer);
setup (inputBuffer, argv);
}
return ;
}
简单shell实现的更多相关文章
- grep查询文本:问一个简单shell问题,将grep的输出赋值给一个变量
问一个简单shell问题,将grep的输出赋值给一个变量 用grep命令得到的输出赋值给一个变量不成功. grep命令如下: 代码: $ grep -c '^abc' file.txt 输出为22,表 ...
- 简单shell脚本
简单shell脚本备忘 #!/bin/sh num= ] do table_num=`printf %03d ${num}` echo album_info_${table_num} #mys ...
- 基于u-boot源码的简单shell软件实现
一.概述 1.shell概念 Shell(命令解析器),它用于接收用户输入的命令,进行解析,然后调用相应的应用程序,为使用者提供了使用软件的界面. shell是操作系统最外面的一层.shell管理你与 ...
- 查看当前支持的shell,echo -e相关转义符,一个简单shell脚本,dos2unix命令把windows格式转为Linux格式
/etc/shells [root@localhost ~]# more /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bi ...
- 简单shell 编程
简单shell编程 by dreamboy #!/bin/bash while true do echo clear echo echo " 系统维护菜单 " echo &qu ...
- Shell基础(一):Shell基础应用、简单Shell脚本的设计、使用Shell变量、变量的扩展应用
一.Shell基础应用 目标: 本案例要求熟悉Linux Shell环境的特点,主要练习以下操作: 1> 切换用户的Shell环境 2> 练习命令历史.命令别名 3 ...
- Linux简单Shell脚本监控MySQL、Apache Web和磁盘空间
Linux简单Shell脚本监控MySQL.Apache Web和磁盘空间 1. 目的或任务 当MySQL数据库.Apache Web服务器停止运行时,重新启动运行,并发送邮件通知: 当服务器磁盘的空 ...
- zabbix通过简单shell命令监控elasticsearch集群状态
简单命令监控elasticsearch集群状态 原理: 使用curl命令模拟访问任意一个es节点可以反馈的集群状态,集群的状态需要为green curl -sXGET http://serverip: ...
- 简单shell指令
第一部分 简单的常用指令 1.date命令 date 2.显示日历 cal 3.显示当前目录 pwd 4.切换当前工作目录 默认情况下,超级用户的主目录是/root,而普通用户的主目录是/home下 ...
- linux之shell基本认知操作和简单shell练习
shell编程: 1.Shell的作用 命令解释器,“翻译官”.介于操作系统内核与用户之间,负责解释命令行. shell功能非常强大,除负责解释名另外,还可以将多个命令组合起来,完成复杂的任务,这就是 ...
随机推荐
- go 递归实现快排
package main import ( "fmt" ) func main() { arr := []int{1,2,5,8,7,4,3,6,9,0,12,13,45,78,8 ...
- Python如何在子类里扩展父类的property?
<python cookbook>8.8节讨论子类扩展property时,一开始都晕了,思考了半天才勉强弄懂一点,赶快记下来.废话不多说,先上代码: class Person: def _ ...
- SDUT OJ 学密码学一定得学程序
学密码学一定得学程序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Description ...
- 在 MVC 中使用 ninject Lazy Load的一个想法
这这里先声明一下,引用了一个 (http://www.edcourtenay.co.uk/musings-of-an-idiot/2012/11/23/lazy-binding-with-ninjec ...
- vector<vector<int>> 使用简单示例
#include <iostream> #include <vector> using namespace std; int main() { vector<vector ...
- ionic4 refresh组件位置变更:Ignored attempt to cancel a touchmove event with cancelable=false
io3 中 refresh组件位置可不定: io4 中 须置顶,否则报错,此外complete方法的调用位置改为target属性 参考:https://github.com/ionic-team/io ...
- QtLog
QtLog #include "mainwindow.h" #include <QApplication> #include <QByteArray> #i ...
- grafana使用小节
安装准备 安装grafana 安装mysql grafana操作步骤 新建数据源,支持mysql 数据库连接失败处理: https://www.jianshu.com/p/684bc3a77ac9 新 ...
- poj2420 A Star not a Tree? 找费马点 模拟退火
题目传送门 题目大意: 给出100个二维平面上的点,让你找到一个新的点,使这个点到其他所有点的距离总和最小. 思路: 模拟退火模板题,我也不懂为什么,而且一个很有意思的点,就是初始点如果是按照我的代码 ...
- Tyvj - 1305 单调队列优化dp
今天有点头痛就不写具体细节了,贴完走人 #include<iostream> #include<algorithm> #include<cstdio> #inclu ...