Pipelined streams
It also can be useful to modify the stream. is able to run several applications connected by the streams.
script body:
#!/usr/bin/env bash
touch /tmp/a
ls /tmp/* 2> /dev/null | grep -w a
rm /tmp/a
The script running at the console:
Here we collect the listing and set “ls” standard output stream as grep standard input stream by using “|”. On “2>” standard errors stream is redirected to the termination file. The output stream of grep contains the “/tmp/a” filename from listing that matches by grep pattern.
Shell Scripting – Standard Input, Output and Error
Working on Linux applications we have several ways to get information from outside and to put it inside: command line args, environment variables, files. All of these sources are legal and good. But it has a finite size. Another way to establish communication is standard streams: input stream (stdin) used for getting data from outside of the app, output stream (stdout) to put data outside of the app, and error to put data outside of the app (stderr).
Each stream acts like a pipe: It has the same buffer to write and read the data. This buffer is available for reading from one application and available for writing from another one. On reading, the occupied buffer size will be reduced and it will be increased on writing. If the average rate of reading and writing is equal – then data passed over the stream can be any number of bytes long.
Table of Content
- Input/output streams operating in the example
- Error stream operating in the example
- Streams redirecting
- Discard the output
- Pipelined streams
- Here document
- Shell Scripting – Standard Input, Output and Error – FAQs