LINUX SHELL编程

1、编写命令
2、编写新shell程序。
3、将1中编写的命令作为内部命令嵌入到编写的shell中。
4、编写相关的程序说明文档。

#include <stdio.h>
#include <signal.h>

#define MAXARGS 20 /* cmdline args */
#define ARGLEN 100 /* token length */

main()
{
char *arglist[MAXARGS+1]; /* an array of ptrs */
int numargs; /* index into array */
char argbuf[ARGLEN]; /* read stuff here */
char *makestring(); /* malloc etc */

numargs = 0;
while ( numargs < MAXARGS )
{
printf("Arg[%d]? ", numargs);
if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n' )
arglist[numargs++] = makestring(argbuf);
else
{
if ( numargs > 0 ){ /* any args? */
arglist[numargs]=NULL; /* close list */
execute( arglist ); /* do it */
numargs = 0; /* and reset */
}
}
}
return 0;
}

execute( char *arglist[] )
/*
* use fork and execvp and wait to do it
*/
{
int pid,exitstatus; /* of child */

pid = fork(); /* make new process */
switch( pid ){
case -1:
perror("fork failed");
exit(1);
case 0:
execvp(arglist[0], arglist); /* do it */
perror("execvp failed");
exit(1);
default:
while( wait(&exitstatus) != pid )
;
printf("child exited with status %d,%d\n",
exitstatus>>8, exitstatus&0377);
}
}
char *makestring( char *buf )
/*
* trim off newline and create storage for the string
*/
{
char *cp, *malloc();

buf[strlen(buf)-1] = '\0'; /* trim newline */
cp = malloc( strlen(buf)+1 ); /* get memory */
if ( cp == NULL ){ /* or die */
fprintf(stderr,"no memory\n");
exit(1);
}
strcpy(cp, buf); /* copy chars */
return cp; /* return ptr */
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-17
不知道你是在什么系统下面用的shell,在linux很简单
1 vi /mnt/ file.sh 这里的路径可以随便的,在系统的任何位置都可以
#!/bin/bash 文件的头
你要作的事情
chmod +x file.sh 给脚本加上可执行的权限,
第2个回答  2010-05-18
1楼的可以采纳,程序说明文档可以直接#加注释就可以了。或者另外做个文档
相似回答