maux
Star on GitHub Buy me a coffee

L4. Shell scripting

Learn / L4

Goal: write three scripts, each with one idea: count tests its arguments, each loops and matches names, mygrep drives ed from a here-document and cleans up after itself.

What you need

L1: a script is a text file that starts with #!/bin/sh. Nothing from the assembly lessons.

maux's sh is the Bourne shell of Research Unix Version 7 with a few later conveniences, and it has no shell functions: V7 had none either. That fixes what these scripts can use.

Writing them also turned up shell behaviour that is wrong today. These are defects, reported to the release and not yet fixed; the scripts below keep clear of them, and so should yours:

The files

count: arguments and tests

$# is the number of arguments, $1 the first, [ -f ... ] tests for an ordinary file, and exit 1 ends the script with a failure status.

examples/scripts/count — on the disk as /usr/src/examples/SCRIPTS/count
#!/bin/sh
# count FILE -- say how many lines FILE has
if [ $# -ne 1 ]
then
    echo usage: count file
    exit 1
elif [ -f "$1" ]
then
    wc -l "$1"
else
    echo count: $1: no such file
    exit 1
fi

each: loops and case

for f in * loops over the file names the shell expands; case matches each against patterns; while read line takes lines from standard input, here a file given after done <. The $( ) on the last line runs a command and puts its output in the line.

examples/scripts/each — on the disk as /usr/src/examples/SCRIPTS/each
#!/bin/sh
# each DIR -- describe every file in DIR
cd $1
for f in *
do
    case "$f" in
    *.s)    kind=source ;;
    *.o)    kind=object ;;
    *)      kind=other ;;
    esac
    echo $f is $kind
done
ls > /tmp/each.$$
while read line
do
    echo listed: $line
done < /tmp/each.$$
rm /tmp/each.$$
echo $(ls | wc -l) files

mygrep: a here-document, trap and set -e

Everything between <<EOF and EOF is the standard input of ed. The name grep is the ed command g/re/p: global, regular expression, print.

examples/scripts/mygrep — on the disk as /usr/src/examples/SCRIPTS/mygrep
#!/bin/sh
# mygrep PATTERN FILE -- grep, written in ed
set -e
tmp=/tmp/mygrep.$$
trap 'rm -f $tmp; exit 1' 2
ed > $tmp <<EOF
r $2
g/$1/p
q
EOF
cat $tmp
rm -f $tmp

The session

Each script is typed with cat > name and ended with ^D (control-D, end of file). cmp then compares what you typed with the copy on the disk; if it prints a line, you mistyped.

$ mkdir /tmp/l4
$ cd /tmp/l4
$ cat > count
[ type the file shown above, a line at a time, then press ^D ]
$ cmp count /usr/src/examples/SCRIPTS/count
$ cat > each
[ type the file shown above, a line at a time, then press ^D ]
$ cmp each /usr/src/examples/SCRIPTS/each
$ cat > mygrep
[ type the file shown above, a line at a time, then press ^D ]
$ cmp mygrep /usr/src/examples/SCRIPTS/mygrep
$ ./count each
19 each
$ ./count
usage: count file
$ echo $?
1
$ ./count nosuch
count: nosuch: no such file
$ echo $?
1
$ ./each /usr/src/examples/HELLO
hello.s is source
Makefile is other
listed: hello.s
listed: Makefile
2 files
$ ./mygrep regular /usr/man/GREP
       for lines matching the regular expression.  Normally,
              regular expression.
       The matcher is the same basic regular-expression engine
$ grep regular /usr/man/GREP
       for lines matching the regular expression.  Normally,
              regular expression.
       The matcher is the same basic regular-expression engine
$ ls /tmp | grep mygrep

What just happened

./count with no argument took the first branch and printed the usage line; with a file that does not exist it took the last. The shell's $? holds the status of the last command, and the session prints it with echo $?: 0 means success, anything else failure. That is the entire contract between a program and a script.

each takes a directory as its operand and changes to it (cd $1) before it looks at anything; the change belongs to the script's own process, so your shell is still where you left it. It writes the listing to a file under /tmp named with $$, the shell's own process number, so two copies never share one, and reads it back with while read line ... done < file. The word 2 files on its last line came from $(ls | wc -l).

mygrep printed the same lines, in the same order, as the real grep. set -e makes the script stop at the first command that fails; trap 'rm -f $tmp; exit 1' 2 says "if signal 2 arrives (^C), delete the temporary file and stop". The last line of the session shows that nothing named mygrep was left in /tmp.

Try next

Write backup: a script that copies the files you changed to a second volume, then archives them with nulib2. Give it a usage message and check its status the way count does.

← Your own filter | All lessons | make →