Step 6 - Challenge: Supporting Pipes
Step 6 - Challenge: Supporting Pipes
Build Your Own Shell (Python Edition)
Step 1 - Create The Simplest Possible Command Line Shell
Step 1 - Create The Simplest Possible Command Line Shell
Step 2 - Challenge: Handle Multiple Commands
Step 2 - Challenge: Handle Multiple Commands
Step 3 - Non Existent Commands
Step 3 - Non Existent Commands
Step 4 - External Commands With Arguments
Step 4 - External Commands With Arguments
Step 7 - Handling Signals
Step 7 - Handling Signals
Conclusion
Conclusion
In this step your goal is to support pipes. If you’ve read any of the Coding Challenges that build Unix command like tools, you’ll have seen how much I rely on pipes to chain together simple tools to build complex data munging solutions.
If you’re not familiar with the terminology, most shells allow us to ‘pipe’ the output from one command into the next command using the | character.
For example in the build your own cut challenge we piped the output of a cut command into uniq and then used wc:
cut -f2 -d, fourchords.csv | uniq | wc -l
To do this in your shell, you need to find out how to redirect the standard input and output streams with the Python programming language.
To test that we’ve implemented pipes correctly we can do something similar:
% ccsh
ccsh> curl https://www.gutenberg.org/cache/epub/132/pg132.txt -o test.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 333k 100 333k 0 0 366k 0 --:--:-- --:--:-- --:--:-- 368k
ccsh> cat test.txt | wc -l
7145
ccsh> exit
%
Here we downloaded a book from Project Gutenberg to use as a test, saving it as test.txt. Then we piped the text of test.txt into the command line tool wc and counted the number of lines.