Piping commands with redirection

  • Thread starter opt!kal
  • Start date
  • Tags
    Piping
In summary, The conversation is about a problem with a programming assignment where the user has to input UNIX commands and redirect the output to a file. The speaker is having trouble with the redirect option and cannot figure out how to make it work for all commands. The attempted solution involves using various functions and a loop to handle the commands, but there is still an issue with the redirection operator causing errors. The speaker is seeking help with this problem.
  • #1
opt!kal
19
0

Homework Statement


Hi there,

So I am working on this assignment where the user is supposed to input a variety of UNIX commands and they must behave as if one typed it into the terminal. My current problem lies with when a user tries to redirect the inputted commands to some file. Currently it only works when a user has the redirect symbol, like:

ls -l | sort -n > temp.txt

However if I try to input:
ls -l | sort -n

I just segfault. I know the problem most likely lies in how I wrote the redirect option, but I can't seem to figure out how to make it so that the two above commands work.

The Attempt at a Solution


Code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

/*get args function*/

#define MAXARGS 256
char ** getargs(char * cmd) {
	// assumes that cmd ends with NULL
	char** argsarray;
	int nargs = 0;
	int nlen = strlen(cmd);
	int i = 0;
	argsarray = (char**) malloc(sizeof(char*) * MAXARGS);
	argsarray[0] = strtok(cmd," ");
	i = 0;
	while (argsarray[i] != NULL){
		i++;
		argsarray[i] = strtok(NULL," ");
	}
	return argsarray;
}int main(void){

  pid_t childpid;
  int fd[256][2];
  char cmd[256];
  char * sepCmd[256];
  char * pch;
  char * srch;

  printf("Please enter a command sequence: \n");
  gets(cmd);
  //scanf("%s", cmd);
  printf("You have entered: %s \n", cmd);
  

  printf("Attempting to split up command: \n");
  pch = strtok (cmd, "|");
  
  srch = strchr(cmd, '>');
  *srch++ = '\0'
  while (*srch == ' ') ++srch;

  int count = 0;  
    while (pch != NULL && count < 256) {
      printf("%s\n", pch);
      sepCmd[count] = pch;
      printf("The value in this array value is: %s\n", sepCmd[count]);
      pch = strtok (NULL, "|");
      count++;
  }
  
  char ** argue;
  int k;
  
  /* Block that deals with the first command given by the user */
  k = 0;
  pipe(fd[k]);
  if(!fork()) {
	  	dup2(fd[k][1], STDOUT_FILENO);
		close(fd[k][0]);
		argue = getargs(sepCmd[k]);
		execvp(argue[0], argue);
		perror(argue[0]);
		exit(0);
  }

  /*Loop that will control all other comands except the last*/
  for(k = 1; k <= count - 2; k++) {
	  close(fd[k-1][1]);
	  pipe(fd[k]);
	  
	  if(!fork()) {
		  close(fd[k][0]);
		  dup2(fd[k-1][0], STDIN_FILENO);
		  dup2(fd[k][1], STDOUT_FILENO);
		  argue = getargs(sepCmd[k]);
		  execvp(argue[0], argue);
		  perror(argue[0]);
		  exit(0);
	  }
  }
  
  
  /*Block that will take care of the last command in the sequence*/
  k = count - 1;
  
  close(fd[k-1][1]);
  if(!fork()) {
	  dup2(fd[k-1][0], STDIN_FILENO);
	  argue = getargs(sepCmd[k]);
          if (srch) dup2(open(srch, O_RDWR|O_CREAT), STDOUT_FILENO);
	  execvp(argue[0], argue);
	  perror(argue[0]);
	  exit(0);
  }
  while(waitpid(-1, NULL, 0) != -1);
}
Any help is greatly appreciated
 
Last edited:
Physics news on Phys.org
  • #2
So a little update,

I changed gets to fgets and it seems like it will take any amount of comannds piped together, but only if it ends with a redirection operator or else I get errors like:

<COMMAND>: invalid option -- '

where COMMAND is the last command in the sequence
 
  • #3
.It seems like the issue lies in properly handling the redirect symbol in your code. It's important to make sure that the file descriptor for the output file is properly set and that any necessary file permissions are being taken into account. It may also be helpful to use the dup2() function to redirect the output to the file before executing the command, rather than trying to do it after the command has already been executed. Additionally, it may be helpful to use the perror() function to get more detailed information about any errors that are occurring. I would suggest reviewing your code and making sure that all necessary steps are being taken to properly handle the redirection of output.
 

What is piping in command line?

Piping is a feature in command line that allows you to connect the output of one command to the input of another command. This allows you to perform multiple operations in one line of code.

What is the symbol for redirection in piping?

The symbol for redirection in piping is the vertical bar or "pipe" symbol (|). This symbol separates the commands and directs the output of the first command to the input of the second command.

How do I redirect the output of a command to a file?

To redirect the output of a command to a file, you can use the greater than symbol (>) followed by the name of the file you want to save the output to. For example, "ls > files.txt" will save the output of the "ls" command to a file called "files.txt".

Can I use multiple pipes in one command?

Yes, you can use multiple pipes in one command to redirect the output of multiple commands. For example, "ls | grep .txt | sort" will list all files with a .txt extension and sort them alphabetically.

What is the difference between using a single pipe and a double pipe in piping commands?

A single pipe (|) will redirect the entire output of the first command to the input of the second command. A double pipe (||) will only redirect the output if the first command was successful. If the first command fails, the second command will not be executed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
878
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
925
  • Engineering and Comp Sci Homework Help
Replies
2
Views
950
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top