UNIX高级编程之简易版shell
2015-07-17 20:43
323 查看
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
int main()
{
while(1)
{
#define MAX 1024
char buf[MAX];
fgets(buf,MAX,stdin);
int len =strlen(buf);
buf[len - 1] = 0;
if(strcmp("quit",buf) ==0)
{
exit(0);
}
pid_t pid = fork();
if(pid == 0)
{
if(execlp(buf,buf,NULL) <0)
{
perror("exec");
//exit(0);
}
/*setsid();
chdir("/");
umask(0);
int fdtablesize = getdtablesize();
int fd;
for(fd = 0;fd < fdtablesize;fd++)
close(fd);
fprintf(stdout,"rong?->$:");
*/
}
else if(pid > 0)
printf("rightrunning!\n");
else
perror("create child process wrong!");
}
exit(0);
}
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
int main()
{
while(1)
{
#define MAX 1024
char buf[MAX];
fgets(buf,MAX,stdin);
int len =strlen(buf);
buf[len - 1] = 0;
if(strcmp("quit",buf) ==0)
{
exit(0);
}
pid_t pid = fork();
if(pid == 0)
{
if(execlp(buf,buf,NULL) <0)
{
perror("exec");
//exit(0);
}
/*setsid();
chdir("/");
umask(0);
int fdtablesize = getdtablesize();
int fd;
for(fd = 0;fd < fdtablesize;fd++)
close(fd);
fprintf(stdout,"rong?->$:");
*/
}
else if(pid > 0)
printf("rightrunning!\n");
else
perror("create child process wrong!");
}
exit(0);
}
相关文章推荐
- Linux系统防CC攻击自动拉黑IP增强版Shell脚本
- 转:SWT中的Display 对象和 Shell对象
- shell中的${},##和%%的使用
- linux exec /bin/sh shellcode x86 and x86_64
- hbase shell
- 8则实用的Linux Shell命令
- bash数组
- 使用 shell 脚本对 Linux 系统和进程资源进行监控
- shell 拾遗
- linux shell 脚本自动从配置远程ftp服务上下载更新文件
- Linux Shell之sed命令
- [shell]join两个文件
- shell中的cut命令
- Linux Shell之sort命令
- 模式匹配运算符–Shell
- linux shell中 if else以及大于、小于、等于逻辑表达式介绍
- shell中各种括号的作用()、(())、[]、[[]]、{}
- linux shell 数组
- shell--getopts
- Shell 调用sh文件里的函数