*nix Plumbing
Published:
Unix-based systems generally have a similar command line interface (CLI). This interface is challenging at first, but allows users to do powerful operations in a fraction of the time required to do the same task in a GUI. An additional benefit is that actions are more repeatable via the CLI. One useful thing to do is chain together multiple commands into one mega-command. This chaining is accomplished with the pipe key |
. Since pipes are used to connect commands, this is colloquially referred to as “plumbing”.
Simple use case
Want to know what processes are running using vim? Run ps aux | grep vim
. This will print out all processes (ps aux
) and filter them through the search (grep) command with the search term “vim”.
Keyboard input
While developing or debugging a terminal program it is important to enter in text via the keyboard. If your input changes (by accident), you might mistakenly think that you fixed (or didn’t fix) the bug. It is also common to need to enter a lot of info, which can be time consuming and is likely to convince you to develop too much code in one step. However, if you place the inputs you would type into a text file, you can simply override the standard keyboard input with the contents of that teext file. For example cat my-inputs.txt | ./a.out
would run the a.out program and use the contents of my-inputs.txt just as if you typed it all in after starting the program.
For this input text file
12
Hey-o
and the following cpp file compiled as a.out
#include <iostream>
#include <string>
int main ()
{
std::cout << "Enter an integer value for Foo: ";
int foo;
std::cin >> foo;
std::cout << "Foo was: " << foo << '\n';
std::cout << "Enter a string value for bar: ";
std::string bar;
std::cin >> bar;
std::cout << "Bar was: " << bar << '\n';
return 0;
}
would output
Enter an integer value for Foo:
Enter a string value for bar:
Foo was: 12
Bar was: Hey-o
This simple input file might not convince you of the power of this approach, but if your program requires a lot of input, or just input that is challenging to type exactly right, the benefit is substantial.
Questions?
I hope this helps clear some things up. If you have questions, ask Google or StackOverflow. If you are my student, come on by my office hours or message me in Discord and we’ll get it figured out together.