0

I'm trying to implement a shell, and i got everything working perfectly fine, with the exception of multiple pipes. i.e ls -l -a -F | tr [a-z] [A-Z] | sort how can i approach this? i know i have to create multiple pipes to solve this but how exactly to do it? Can someone guide me in the right direction?

I currently manage only one pipe, but i'm not too sure on how to approach this when i have more than two pipes. I was wondering if anyone could provide me with some pseudo-code n how to approach this problem

  • you have to fork() (You would think forks would be a problem for pipes, but not in this case) – Hogan Nov 13 '13 at 18:42
  • 1
    @Hogan I'm sure he knows that, since you have to do that even with just one pipe. – Barmar Nov 13 '13 at 18:43
  • @Barmar Indeed, I didn't see your comment appearing while I was typing mine. – Sir Athos Nov 13 '13 at 18:43
  • You have to fork even with no pipes. – Barmar Nov 13 '13 at 18:44
  • yes! correct, i can manage a command such as `sort < txtFile | grep key` but i dont really know how to approach this when i have more than one pipe as in the example command i posted above. –  Nov 13 '13 at 18:46

2 Answers2

2

Just parse the string in order, when you get to a pipe symbol you fork off the last command and store the std in and std out. If you had a prior command you pump that commands std out to the std in of the new command. Then you loop.


Additional note:

The only difference between

A)    thing1 > thing2

and

B)    thing1 | thing2

Is this, in A) you are running thing1 (with fork) and setting the output to a file called thing2

In B) you are running both thing1 and thing2 with fork and setting the output of thing1 to the input of thing2.

So,

C)     thing1 | thing2 | thing3

Is just the same, you need to run (fork) thing1, thing2, thing3 and set the output of thing1 to the intput of thing2, the output of thing2 to the input of thing3.

Pipe works just like > but you run the "target" with fork.

Hogan
  • 69,564
  • 10
  • 76
  • 117
0

If you've indeed got everything working but multiple pipes, you can reduce them to single pipes using grouping or subshells.

{ ls -l -a -F | tr a-z A-Z; } | sort
(ls -l -a -F | tr a-z A-Z) | sort
JB.
  • 40,344
  • 12
  • 79
  • 106