我想運行以下命令「ls | date」,但每當我隔離ls和日期時,它們都不會使用execvp執行。C中的Shell命令(使用Execvp時出錯)
問題:
Isolated:ls
ls: impossible to acess 'ld-linux-x86-64.so.2': Unknown directory or file
ls: impossible to acess 'ld-linux-x86-64.so.2': Unknown directory or file
Isolated: date
ls: impossible to acess 'date': Unknown directory or file
在代碼中,我想要查詢的 「;」但是當我檢查「」& &「|」它隔離我想要的參數......但它們不運行。
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
char command_line[256]="ls | date";
char *ptr_current_command;
char *saveptr1;
char *saveptr2;
char *saveptr3;
char *ptr_current_parameter;
char *ptr_current_parameter2;
char *str[256];
int i=0;
int wait_child;
pid_t pid;
//Uses Strtok to recognize token ";"
ptr_current_command=strtok_r(command_line, ";", &saveptr1);
while (ptr_current_command != NULL) {
//printf("current_command: %s\n", ptr_current_command);
//Uses Strtok to recognize token " "
ptr_current_parameter=strtok_r(ptr_current_command, " ", &saveptr2);
while(ptr_current_parameter != NULL){
//printf("\tcurrent_parameter: %s\n", ptr_current_parameter);
//Uses Strtok to recognize token "|"
ptr_current_parameter2=strtok_r(ptr_current_parameter, "|", &saveptr3);
while(ptr_current_parameter2 != NULL){
printf("\t\tIsolei: %s\n", ptr_current_parameter2);
str[i]=ptr_current_parameter2;
i++;
//Closes token by token until final NULL of "|"
ptr_current_parameter2=strtok_r(NULL, "|", &saveptr3);
}
pid=fork();
if (pid < 0) { perror("fork"); exit(errno); }
if (pid == 0){
execvp(str[0], str);
perror("execvp"); exit(errno);
}
if (wait_child)
waitpid(pid, NULL, 0);
//Fecha Delimitador Espaço
ptr_current_parameter=strtok_r(NULL, " ", &saveptr2);
}
//Closes token by token until final NULL of ";"
ptr_current_command=strtok_r(NULL, ";", &saveptr1);
}
return 0;
}
嘗試'/斌/ ls' ... – Stargateur
哪裏這個 「'不可能acess'」 從何而來? – alk
我不知道... – Bitsized