#!/bin/sh -u # A "backup" script - Non-Structured Programming Version (many exits). # 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 # Check argument count. # if [ $# -eq 0 ] ; then echo 1>&2 "$0: expecting exactly one file name - none found" exit 4 fi if [ $# -ne 1 ] ; then echo 1>&2 "$0: expecting exactly one file name, found $# ($*)" exit 4 fi # Test for readable # if [ ! -r "$1" ] ; then echo 1>&2 "$0: path '$1' is missing or is not readable" exit 3 fi echo "path $1 is readable" # Test for plain file # if [ ! -f "$1" ] ; then echo 1>&2 "$0: path '$1' is not a plain file" exit 2 fi echo "path $1 is is a plain file" # Test for existence of .bak file; abort if already there # if [ -e "$1.bak" ] ; then echo 1>&2 "$0: path '$1.bak' already exists" echo 1>&2 "$0: nothing copied" exit 1 fi # Finally! Do the copy. Test the exit code for failure. # if cp -p "$1" "$1.bak" ; then echo "File '$1' copied to '$1.bak'. Good job." else status=$? echo 1>&2 "$0: failed to copy '$1' to '$1.bak': status $status" exit $status fi