suppose there are multiple commands passed by a user on shell say
command 1 | command 2 | command 3 | command 4
so I have written a sample program which reads command 1|command 2 inside char str[] (right now for simplicity I have hard coded the commands inside program)
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
char str[] = "ls -al| grep test.txt";
char *commands[10]; // Array to hold a max of 10 commands
char *semi = "|";
char *token = strtok(str, semi);
int i = 0;
while (token != NULL)
{
commands[i] = token;
++i;
token = strtok(NULL, semi);
}
int numCommands = i; // numCommands is the max number of input commands
i = 0;
while (i < numCommands)
{
printf("Command: %s\n", commands[i]);
char *args[10] = {}; // Array to hold command args
args[0] = strtok(commands[i], " ");
int tokenCounter = 0;
while (args[tokenCounter] != NULL)
{
tokenCounter++;
args[tokenCounter] = strtok(NULL, " ");
}
int childpid = fork();
if (childpid == 0)
{
if ((execvp(args[0], args)) < 0)
{
printf("Error! Command not recognized.\n");
}
exit(0);
}
else if (childpid > 0)
{
wait(&childpid);
}
else
{
printf("Error: Could not create a child process.\n");
exit(1);
}
++i;
}
return 0;
}
I understand that I need to use dup2 and pipe in this situation I went through many tutorials also, but in the code above when I am executing commands inside while loop i.e. while (i < numCommands)
then the commands are being executed independently while what I want to achieve here since the number of commands which user can pass on shell could be n so how can I implement n pipes which I can use in while loop for execution to read write.More specifically I want to concatenate out put of one command to other command in pipe.
Multiple piped programs in a command line are separated with the token "|". A command line will therefore have the following form:
<program1><arglist1> | <program2><arglist2> | ... | <programN><arglistN> [&]
Multiple processes I have launched in above program but how do I connect them using pipe in a normal situation when I would have been aware of how many pipes I am supposed to use I would have constructed them and passed the inputs. But here number is not specified as how many commands a user can pass.So how can I go for implementing multiple pipes in this situation. Any logic which can lead to solution of my problem is what I am looking for.