Turn a pipeline into a shell script

Synopsis

Make the frequency report reusable. Pass filenames to a script as arguments, produce one labeled report per file, and signal a failure if an input is missing. Use an ordinary account and the observations file from Streams, redirection, and filters.

Put one command in a file

Return to the practice directory. Confirm the location and input before continuing.

Shell input
cd "$HOME/unix-practice"
pwd
ls observations.txt

The shell accepts commands from a text file as well as from a terminal. Save the familiar pipeline in a disposable file. The redirection replaces one-report.sh in the practice directory.

Shell input
printf '%s\n' 'LC_ALL=C sort observations.txt | uniq -c' > one-report.sh
sh one-report.sh

The result contains one robin and two sparrows. The sh process reads the file. The script needs read permission for this form of invocation. At present the input name is fixed, and a failure in the first filter can go unnoticed.

Accept named inputs

Save report.sh in the practice directory. At a text console, open vi report.sh and enter the contents below in a new file. Press Esc, type :wq, and press Enter to save. The opening #! line identifies the interpreter for systems that support starting a script directly.

File contents · report.sh

#!/bin/sh

if [ "$#" -eq 0 ]; then
    printf 'Usage: sh report.sh file ...\n' >&2
    exit 2
fi

sorted=$(mktemp "${TMPDIR:-/tmp}/bird-report.XXXXXXXXXX") || exit 1
trap 'rm -f "$sorted"' 0
trap 'exit 1' HUP INT TERM

status=0
for file in "$@"; do
    if [ ! -f "$file" ] || [ ! -r "$file" ]; then
        printf 'Cannot read a regular file: %s\n' "$file" >&2
        status=1
        continue
    fi
    LC_ALL=C sort < "$file" > "$sorted" || exit 1
    printf '%s:\n' "$file" || exit 1
    uniq -c < "$sorted" || exit 1
done
exit "$status"

The argument count is $#. With no arguments, the script writes a usage line to standard error (>&2) and exits with status 2. Otherwise, the for loop assigns each argument in turn to file. The quotes in "$@" preserve each original argument, including spaces.

The conditional checks whether the name refers to a readable, regular file. On failure, continue moves to the next argument, and status remembers that at least one input failed. The shell opens each successful input with < "$file". Even a filename beginning with a dash can be passed this way because sort reads standard input.

The two filters still run in the same order. This version saves the sorted lines in a temporary file so that a failed sort has its own exit status. The shell runs the command after || only when the preceding command fails. The mktemp utility creates a unique file in the temporary directory and prints its path. The trap commands arrange cleanup when the shell exits, including after the listed signals. OpenBSD mktemp(1) · Debian mktemp(1)

Run the script

Generate a report for the original file.

Shell input
sh report.sh observations.txt

Example output

observations.txt:
      1 robin
      2 sparrow

Create a second, disposable input. The command replaces the practice copy of pond notes.txt if it exists.

Shell input
printf '%s\n' heron heron > 'pond notes.txt'
sh report.sh observations.txt 'pond notes.txt'

Look for two headings. The second heading reads pond notes.txt: and has a single row with a count of two for heron. Without the quotes, the calling shell would pass pond and notes.txt as separate arguments.

Test a failure

Choose a name that does not exist in the practice directory. The following invocation deliberately places it before a valid name.

Shell input
sh report.sh missing-input.txt observations.txt

Expect a diagnostic naming missing-input.txt and a normal report for observations.txt. Inspect the status before entering another command.

Shell input
printf '%s\n' "$?"

Expect 1. Repeating the test with the missing name removed gives status 0. A caller can therefore distinguish complete success from a partial report even when both runs produce output.

If sh reports a syntax error after manual entry, check the line ending the loop (done) and the one ending each conditional (fi). Run sh -n report.sh to check shell syntax without running the report.

Continue with Build and install a small program.

UNIX is a registered trademark of The Open Group.