Executing programs from other programs to handle a specific task appeals to the UNIX philosophy of limiting the scope of individual programs, and using divide-and-conquer to tackle large, complex problems. Counter-intuitively, however, creating pipelines is only convenient in shell scripts.
Orchestrating pipelines from native binaries typically
involves passing a command line (originating from the
nature of shells) to a shell via
-c CMD. This forces the caller
to glob command arguments together to a single string. Furthermore, the
user input must now be sanitized to prevent unintended behavior, such as
pipeln addresses this issue.
Instead of parsing the pipeline definition from a single arg,
uses the entire argument list it is invoked with, making it trivial to
reason about how many and what arguments each invoked program will receive.
The only input validation required by the caller is to ensure that
is not included in user-controlled arguments.
The program fits inside a single, small C file (~140 LOC).
The source code is available at https://git.sinitax.com/pipeln.
Creating a pipeline using Python to find files that match a regular expression in the current directory:
#!/usr/bin/env python3 from subprocess import call from sys import argv, exit if len(argv) != 2 or argv == "|": exit(1) call(["pipeln", "find", "|", "grep", "-e", argv])