#!/bin/sh -u # A "backup" script - Structured Programming Version (one entry / one exit). # Specifications: # - processes exactly one plain file name (not a directory) # - file name must be readable # - copy file to file.bak, if file.bak does not already exist # # NOTE: This script fragment is incomplete - it does not follow all # the script guidelines set out in "script_style.txt". # # -IAN! idallen@idallen.ca if [ $# -eq 1 ] ; then if [ -r "$1" ] ; then echo "path $1 is readable" if [ -f "$1" ] ; then echo "path $1 is a plain file" if [ ! -e "$1.bak" ] ; then # Finally! Do the copy. # Test the exit code for failure. # if cp -p "$1" "$1.bak" ; then returncode=0 echo "File '$1' copied to '$1.bak'. Good job." else returncode=$? echo 1>&2 "$0: failed to copy '$1'" \ "to '$1.bak': status $returncode" fi else echo 1>&2 "$0: path '$1.bak' already exists" echo 1>&2 "$0: nothing copied" returncode=1 fi else echo 1>&2 "$0: path '$1' is not a plain file" returncode=2 fi else echo 1>&2 "$0: path '$1' is missing or is not readable" returncode=3 fi else # Wrong number of arguments. Issue an error message on stderr. # if [ $# -eq 0 ] ; then echo 1>&2 "$0: expecting exactly one file name - none found" else echo 1>&2 "$0: expecting exactly one file name, found $# ($*)" fi returncode=4 fi exit "$returncode"