Files and Pipelines
Pipelines
Connecting Commands
A pipeline sends stdout from one command into stdin of the next. Each command does one small job.
Program
Play the script to see generated log text flow into grep.
pipelines.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
log=$'error\nok\nerror'
matches="$(printf '%s\n' "$log" | grep -c error)"
echo "matches=$matches"
log ← error, ok, error
3log=$'error\nok\nerror'4matches="$(printf '%s\n' "$log" | grep -c error)"values this steperror, ok, errorlogmatches ← 2
3log=$'error\nok\nerror'4matches="$(printf '%s\n' "$log" | grep -c error)"5echo "matches=$matches"values this step2matcheserror, ok, errorlogecho "matches=$matches"
4matches="$(printf '%s\n' "$log" | grep -c error)"5echo "matches=$matches"outputmatches=2values this step2matches
Watch stdout Flow
printfwrites three log lines to stdout.- The pipe sends those lines into
grep. grep -c errorcounts matching lines.- Command substitution stores the count in
matches.
printf stdout -> grep stdin -> count of "error"
| Line | Counted by grep -c error? |
| --- | --- |
| error | yes |
| ok | no |
| error | yes |
pipeline
`cmd1 | cmd2` connects the first command's stdout to the second command's stdin.
filter
A filter reads input, selects or transforms it, and writes a result.
Exercise: pipelines.sh
Pipe generated log lines into grep -c and print how many error lines matched