linux C 实现myshell问题(求教)

wizard5669 2012-08-25 01:40:49
我的程序是想实现输入命令,然后判断我输入了几个参数,第几个命令是什么,但是程序有点问题不着调哪里错了

输出的结果
[gang@localhost test]$ ./myshell
myshell# ls -a -l
ls -a -l
input : ls -a -l
cmd : ls
[gang@localhost test]$
我下面想实现的是输出我输出的第N个参数,但是实现不了,就是我下面注释掉的那段代码,请大虾给看看


#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(int argc, char **argv)
{
//input store a arr
char buf[30];
int len;

// output ti shi
char *tishi = "myshell# ";
if(write(1,tishi,strlen(tishi)) != strlen(tishi))
{
fprintf(stderr,"write tishi failed:%s\n",strerror(errno));
exit(-1);
}

if ((len = read(0,buf,strlen(buf))) < 0)
{
fprintf(stderr,"read failed from stdin:%s\n",strerror(errno));
exit(-1);
}
buf[len] = '\0';
if (write(1,buf,len) < 0)
{
fprintf(stderr,"write failed to stdout:%s\n",strerror(errno));
exit(-1);
}
printf("input : %s",buf);



//output cmd is ?
printf("cmd : ");
int i = 0;
while(*(buf+i) != ' ')
{
putchar(*(buf+i));
i++;
}
putchar('\n');

#if 0
i++;
//can shu
int num = 1;
while(*(buf+i) != '\n')
{
printf("the %d ca shu is : ",num++);
while(*(buf+i) != ' ')
{
putchar(*(buf+i));
i++;
}
putchar('\n');
}

#endif

return 0;
}
...全文
302 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
平凡的奋斗者 2012-08-26
  • 打赏
  • 举报
回复
while(*(buf+i) != '\n')
{
printf("the %d ca shu is : ",num++);
while(*(buf+i) != ' ')
{
putchar(*(buf+i));
i++;
}
putchar('\n');
}

以上的代码,产生了一个死循环的问题,比如当代码运行到while(*(buf+i) != ' ')这里,判断当前字符为空格,所以不执行循环,所以跳到外循环,而外循环while(*(buf+i) != '\n')仅仅判断字符是否为回车符, 因为空格不是'\n',所以外循环继续执行,但内循环条件又不满足,所以无法跳到下一个字符,所以就无限重复执行putchar('\n'); 导致错误。

改后的代码如下:



while(*(buf+i) != '\0')
{
printf("the %d ca shu is : ",num++);
while(*(buf+i) != ' ')
{
if(*(buf+i) == '\0')
{
return 0;
}
putchar(*(buf+i));
i++;
}
i++;
putchar('\n');
}



当然我在这里又加上了


if(*(buf+i) == '\0')
{
return 0;
}



这条语句,就是防止内循环直接越出你的buf界限。 你可以去掉试试。
luh713 2012-08-26
  • 打赏
  • 举报
回复
这个 。。。
1.用命令行参数做
2.exec函数族

/*
* 简单的Shell程序
* 主循环(无限循环)
* 打印命令提示符
* 读入一行数据
* 分析数据
* 满足退出条件则跳出主循环
* vfork
* 子进程中调用exec执行命令
* 父进程等待子进程结束
* 继续循环
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>

#define MAX_PARAMETER 10 //本shell最多接受的参数个数
#define CMD_TIP "lemon >" //命令提示符
#define EXIT_FLAG "quit" //结束标志
#define CMD_BUFFER_SIZE 256 //命令缓冲区大小

/*
* ctrl + c 处理
*/
void ctrlcHandle(int signum)
{
// printf("\n%s-------------", CMD_TIP);
}

int main(void)
{
char * argv[MAX_PARAMETER];
char cmdBuffer[CMD_BUFFER_SIZE];
pid_t pid;
int status;
int paraNum; //参数个数

//注册信号ctrl+c处理函数
signal(SIGINT, ctrlcHandle);

while(1) //主循环
{
printf(CMD_TIP); //打印命令提示符
fflush(stdout); //及时输出命令提示符
//读用户输入
fgets(cmdBuffer, CMD_BUFFER_SIZE, stdin);
if(cmdBuffer[0] == '\n')
{//简单回车
continue;
}
if(cmdBuffer[strlen(cmdBuffer)-1] == '\n')
{//去掉末尾回车符
cmdBuffer[strlen(cmdBuffer)-1] = 0;
}
//是否退出
if(!strcmp(cmdBuffer, EXIT_FLAG))
{
break;
}
//分割参数
paraNum = 1;
argv[0] = strtok(cmdBuffer, " "); //空格进行分割
while( (paraNum < MAX_PARAMETER) &&
(argv[paraNum] = strtok(NULL, " ")) )
{
paraNum++;
}
//创建子进程以执行命令
pid = vfork();
if( pid == 0 )
{//子进程执行命令
execvp(argv[0], argv);
perror("execvp");
exit(EXIT_FAILURE);
}else if( pid < 0 )
{//错误
perror("vfork");
exit(EXIT_FAILURE);
}else
{
waitpid(pid, &status, 0); //等待子进程结束
}
}
return 0;
}

wizard5669 2012-08-25
  • 打赏
  • 举报
回复
怎么没有人回答啊
wizard5669 2012-08-25
  • 打赏
  • 举报
回复
没人回答吗?自己顶下
wizard5669 2012-08-25
  • 打赏
  • 举报
回复
没人回答吗?自己顶下

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧