i have problems regarding linux shell script. i want to do shell scripting using c programming. my real issues are how to do the i/o redirections using pipe. for example: ls < new.txt
how can i write the code for my shell to execute that command. so far i am stuck with this code:
if(strchr(input,'<') != NULL)
{
char *token, *arr1, *arr2, *argv1[10], *argv2[10];
int i, a, b, n;
int fds[2];
int fd;
i=0 , a=0, b=0;
token = strtok(input,"<");
arr1 = token;
printf("Arr1 is %s\n", arr1);
token = strtok(NULL,"<");
arr2 = token;
printf("Arr2 is %s\n", arr2);
//for(n=0; n < i; n++)
// printf("Array1[%d] is %s\nArray2[%d] is %s\n", n,arr1[n],n,arr2[n]);
token = strtok(arr1," \n");
argv1[a] = token; a++;
while(token!=NULL)
{
token = strtok(NULL," \n");
argv1[a] = token; a++;
}
for(n=0; n < a; n++)
printf("argv1[%d] = %s\n", n, argv1[n]);
token = strtok(arr2," \n");
argv2[b] = token; b++;
while(token!=NULL)
{
token = strtok(NULL," \n");
argv2[b] = token; b++;
}
for(n=0; n < b; n++)
printf("argv2[%d] = %s\n", n, argv2[n]);
pipe(fds);
if(fork() == 0)
{
close(fds[1]);
fd=open(argv2[0],O_RDWR,0600);
dup2(fd,STDOUT_FILENO);
dup2(fds[0],STDIN_FILENO);
close(fds[0]);
execvp(argv1[0],argv1);
exit(0);
}
else
{
if(fork() == 0)
{
close(fds[0]);
dup2(fds[1],STDIN_FILENO);
close(fds[1]);
execvp(argv2[0],argv2);
exit(0);
}
else
{
close(fds[0]);
close(fds[1]);
wait(NULL);
wait(NULL);
}
}
}
i appreciate anyone's help. thank you for your answers.