The following is a simple shell script to easily create backup
copies of a file. Each copy of the file is sequentially numbered with
the most recent versions having the lowest numbers. This version of the
"mkversion" script will keep up to 10,000 copies of a file.
#!/usr/bin/ksh93
################################################################
# Program: mkversion_k93
#
# Description: Creates numbered versions of a file (up to 9999).
# The latest version has the lowest number following the file
# name. The oldest version has the highest number following the
# file name.
#
# Author: Dana French (dfrench@mtxia.com)
#
# Date: 06/17/2003
# Last Modified: 06/29/2015
#
################################################################
FNAME="${1:?# ERROR: Syntax \"${0} fileName\"}"
[[ ! -f ${FNAME} ]] && print -u 2 -- "# ERROR: ${FNAME} does not exist" && exit 1
F=( ${FNAME}.[0-9] ${FNAME}.[1-9][0-9] ${FNAME}.[1-9][0-9][0-9] ${FNAME}.[1-9][0-9][0-9][0-9] )
for (( i=${#F[@]}; i>=1; --i ))
do
[[ ! -e ${F[i-1]} ]] && unset F[i-1] && continue
[[ ! -w ${F[i-1]} ]] && print -u 2 -- "# ERROR: ${F[i-1]} is not writeable" && exit 2
[[ ! -r ${F[i-1]} ]] && print -u 2 -- "# ERROR: ${F[i-1]} is not readable" && exit 3
[[ ! -w . ]] && print -u 2 -- "# ERROR: . is not writeable" && exit 4
done
for (( i=${#F[@]}; i>=1; --i ))
do
cp ${F[i-1]} ${FNAME}.$(( ${F[i-1]#${FNAME}.} + 1 )) || exit 5
done
cp "${FNAME}" "${FNAME}.0"