我想在c中編寫一個shell,它主要工作,除了grep。每當我在shell中給出一個grep命令時,它都不會輸出任何內容。下面是我用來創建一個新的子進程,然後在其中運行execvp()的代碼部分。grep不能在自定義shell中工作
dup2中的文件描述符(fd_in和fd_out)作爲參數傳遞給具有此代碼的函數。最有趣的是,當我給'grep'或'grep --help'時,它照常顯示。我錯過了什麼?或者用grep做一些特別的事情?
這就是我的shell所發生的情況:當從bash運行時,最後一個命令輸出。
--> grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
--> wc /etc/hosts
11 33 314 /etc/hosts
--> grep -i "perror" shell.c
-->
下面是代碼:
void
create_process(char *cmd_argv[], int fd_in, int fd_out, char *buffer_copy) {
/*Flag bit for Background processes*/
int FLAG = 0;
pid_t cpid;
int status;
int i = 0,j = 0;
/*Find the no. of arguments*/
while(cmd_argv[j] != NULL)
j++;
/*Set the flag bit*/
if(strcmp("&", cmd_argv[j-1]) == 0) {
FLAG = 1;
cmd_argv[j-1] = NULL;
}
//Create a child process
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
//In the child...
if (cpid == 0) {
/*Checking if the file descriptors are already assigned*/
/*For stdin*/
if (fd_in != STDIN_FILENO) {
dup2(fd_in, STDIN_FILENO);
close(fd_in);
}
/*For stdout*/
if (fd_out != STDOUT_FILENO) {
dup2(fd_out, STDOUT_FILENO);
close(fd_out);
}
/*Run the cmd specified*/
status = execvp(cmd_argv[0], cmd_argv);
/*In case of errors*/
if(status < 0) {
perror("execvp ");
exit(1);
}
}
//In the parent...
else {
if(FLAG == 1) {
/*Find where the new bg process can be inserted*/
while(1) {
if (bgprocess[i].pid == 0) {
bgprocess[i].pid = cpid;
strcpy(bgprocess[i].cmd, buffer_copy);
break;
}
i++;
}
printf("[%d] : %s\n", cpid, cmd_argv[0]);
}
/*If not bg, wait for the process to exit*/
else
waitpid(cpid, NULL, 0);
}
}
向我們展示如何設置'cmd_argv'。 – cnicutar
char * cmd_argv [10];代碼的前一部分用適當的參數填充,然後是NULL。當你運行grep時, – 0x777
顯示你的shell的一個strace。另外,你應該關心stderr以及stdin/stdout。 – nos