random unix notes (part II)
Sat 26 December 2015
[caption id="" align="alignright" width="75" class="zemanta-img"] Specific file descriptor (Photo credit: Wikipedia)[/caption]
Few weeks ago, I wrote down about some unix tools. Here are some other multiple process using the same stdout/stdin file desricptor.
file descriptor
The 3 most used file descriptors are stdin, stdout and stderr. A command's stdout can easily be redirected to another command's sdtin using pipe mechanism.
More about file descriptor can be found in this stackoverflow answer and this tiny tutorial. In short, file descriptors are located under /dev/fd/ (e.g. stdin is /dev/fd/0) and an unnamed pipe can be created using NN>&1 (redirecting stdout to /dev/fd/NN).
piping from multiple processes
The problem is the following: I have multiple command and I want to concatenate all the outputs. An obvious and easy way is to pipe all the outputs as input to the cat command. To do so, one must group all this commands together:
$ clear ; (ls /usr ; echo "foo bar" ; grep ntp /etc/passwd) | cat bin include info lib lib64 libexec local lost+found sbin share src var foo bar ntp:x:87:87:Network Time Protocol:/var/lib/ntp:/bin/false
moreutils
`moreutils <http://joeyh.name/code/moreutils/>`__ (available in both archlinux and debian) is a tool collection. One (pee) is fitted to pipe an output to multiple command:
$ cd /usr && ls -1 bin include info lib lib64 libexec local lost+found sbin share src var $ ls -1 | pee 'tail -1' 'head -1' var bin
Category: tools Tagged: Application programming interface File descriptor Standard streams tools Unix