私は宿題のために自分のシェルを書いていますが、この問題に直面しています:
出力をファイルにリダイレクトするコマンド (つまり、ls -al > output.txt) を入力するたびに、シェルはコマンドを処理して出力をリダイレクトする必要があります。ただし、次のメッセージが表示され続けます。ls: write error: Bad file descriptor
他のいくつかのフォーラムで、メモリ量の超過に関連している可能性があることを見ましたが、それがどのように問題になるのかわかりません。これが私のコードの一部です(明確にするためにさらに必要な場合は、お気軽にお問い合わせください):
loc[0] = argv[0]; //used for execution
while(argv[count] != 0){ //loop through commands
if(strcmp(argv[count], "<") == 0) //and test for certain flags
inFlag = 1;
else if(strcmp(argv[count], ">") == 0){
argv[count] = argv[count+1] = 0;
outFlag = 1;
}
else if(strcmp(argv[count], "&") == 0)
bgFlag == 1;
else if(strcmp(argv[count], "|") == 0){
argv[count] = 0;
loc[pipes+1] = argv[count+1];
pipes++;
}
count++;
}
for(k = 0; k <= pipes; k++){
if(j < pipes){
pipe(r_tube);
j++;
}
pid = fork();
if(pid > 0){
if(j > 0){
close(l_tube[0]);
close(l_tube[1]);
}
l_tube[0] = r_tube[0];
l_tube[1] = r_tube[1];
}
else if(pid == 0){
if((k == 0) && (inFlag == 1)){
int n = open("input.txt", "r");
close (0);
dup (n);
close (n);
}
else if((k > 0) && (k < pipes)){
}
else if((k == pipes) && (outFlag == 1)){ //<-----issue
int out = open("output.txt", 0666);
close (1);
dup (out);
close (out);
}
else if(k == pipes){
}
execvp(argv[loc[k]], &argv[loc[k]]);