Errata

Shell Scripting Recipes

A Problem-Solution Approach

by Chris F.A. Johnson

Chapter 1

standard-vars

Page 9
For the sake of consistency with the code that supposedly created the file, the lines from "standard-vars" should read:

NL="
"
"R="
TAB="   "

Bourne Shell Expansions

A number of newlines are missing from this section, starting with the last line of Page 12 which should be:

$ echo /$var-y/
/y/
Page 13
$ echo /${var+C}/
/X/

  Finally, when the variable is set and not null, VALUE is used, with
or without the colon.

$ var=A
$ echo /${var:+X}/
/X/
$ echo /${var+X}/
/X/
An extra comma appears on Page 14; it should be:
>    list=${list:+$list,}$f

Thanks to Jens Schweikhardt for pointing out the errors in Chapter 1.

Chapter 3

Notes: gsub()

On Page 72 the '_gsub' function under "Notes" should be:

_gsub() { _GSUB=${1//"$2"/"$3"}; }

Chapter 5

3. Convert Degrees Celsius to Fahrenheit

Page 126, in the c2f (not '_c2f') function, '_C2F=' should be removed:
c2f()
{
    prompt="Degrees Celsius" arg "$@"
    echo "$arg" |
        awk '{
               printf "%" pr "f\n", $1 * 1.8 + 32
             }' pr=${precision:-.2}
}

Page 129, in the cm2i (not '_cm2i') function, 'Millimeters' should be 'Centimeters', and the function call and variable in the last two lines of 'mm2i' are wrong.

cm2i()
{
    units=inches
    prompt=Centimeters arg "$@"
    _cm2i "$arg"
    printf "%s%s\n" "$_CM2I" "${units:+ $units}"
}

Convert Millimeters to Inches

mm2i()
{
    units=inches
    prompt=Millimeters arg "$@"
    _mm2i "$arg"
    printf "%s%s\n" "$_MM2I" "${units:+ $units}"
}

The Lengths and Distances Menu

Page 134, the last item on the menu should be:

     8. mm2i - millimeters to inches

David Emme provided the corrections to Chapter 5; thanks, David.

Chapter 15

_put_csv

On Page 312, the '_put_csv' function didn't deal with leading null fields. Gregg Nelson provided a fix:

_put_csv()
{
    _PUT_CSV=
    for field in "$@"  ## loop through the fields (on command line)
    do
      case $field in
         ## If field contains non-numerics, enclose in quotes
         *[!0-9.-]*) _PUT_CSV=$_PUT_CSV\"$field\",
                     ;;
         '') _PUT_CSV=$_PUT_CSV,
             ;;
         *) _PUT_CSV=$_PUT_CSV$field,
           ;;
      esac
    done
    _PUT_CSV=${_PUT_CSV%,}     ## remove trailing comma
}

Chapter 20

In the install_script() function on Page 402, the variable '$file' should be '$1', and the path should be removed:

    filename=${1%$devel_suffix}               ## Command name, without suffix
    filename=${filename##*/}                  ## Remove path; use file name alone

Also in the install_script() function, on Page 403, removing write permissions from the script can cause problems in some instances. If they do, just remove them or comment them out:

#      ## Remove write permissions
#      chmod +rx,-w "$dest"

_uniqfile()

A function was missing from the cpsh script on Page 403. This should be inserted before "progname=${0##*/}".

_uniqfile()
{
    _base=$1
    _n=1
    while [ -f "$_UNIQFILE" ] || [ -d "$_UNIQFILE" ]
    do
      _zpad $_n ${VERSION_WIDTH}
      _UNIQFILE=$_base-$_ZPAD
      _n=$(( $_n + 1 ))
    done
}