#!/bin/ksh # touchvi - Author: Edwin Arneson - $Revision: 1.2 $ - $Date: 2007/03/10 19:38:11 $ # Q&D script to touch a temp file before editing the named file, then # touching the newly modified file referencing the temp file to reset the datestamp. # FORMAT: touchvi # CHANGE HISTORY: # 070215 EJA 1.1 - Created # 070310 EJA 1.2 - Added the -e flag so that automatic edits can be made to the # specified file instead of manually editing it. # CALLED BY: # CALLS: # SEE ALSO: # SCRIPTS: # PERMISSIONS: # HOSTS THIS FILE IS IDENTICAL ON: # FILES: # GLOBALS: # LOCALS: # COMMANDS: # INPUT: # OUTPUT: # OUTPUT USED BY: # EXAMPLES: # HINTS: # RESTRICTIONS: # BUGS: # WARNINGS: # 40 - The localize script could not be found. Global machine settings can not be set. # ERROR CODES: # 51 - An unknown option was passed to the script, or syntax of the parameters is wrong. # 52 - Unknown edit command >$ecmd< # 53 - The file >$1< is not writable and, hence, can not be edited. # 56 - The USAGE was requested using -z or -?. # DIAGNOSTICS: # RCS.SH VERSION: 00010 # RCS INFORMATION: # $Id: touchvi,v 1.2 2007/03/10 19:38:11 earneson Exp $ # $Author: earneson $ # $Date: 2007/03/10 19:38:11 $ # $Revision: 1.2 $ # $Source: /Users/cvs/scripts/all/Users/earneson/bin/touchvi,v $ # $State: Exp $ ####### ####### Initialization ####### # Special check for first parameter of "-x" to turn on tracing of the script execution if [[ "$1" = "-x" ]] ; then set -x fi # Find and execute the localization script designed to set common environment variables based name characteristics of the executing computer binDir=`dirname $0` # get the directory this script is in and check for localize there if [[ -r "$binDir/localize" ]] ; then . $binDir/localize else echo "$0: WARNING 40 - The localize script could not be found. Global machine settings can not be set." fi # Ensure these three are set if [[ -z "$localhost" ]] ; then localhost="`uname -n`" ; export localhost ; fi if [[ -z "$awkname" ]] ; then if [[ -x "/usr/bin/nawk" ]] ; then awkname="nawk" ; else awkname="awk" ; export awkname ; fi ; fi if [[ -z "$OStype" ]] ; then OStype="`uname -s`" ; export OStype ; fi ####### ####### Functions ####### ####### ####### Other variables ####### basename=`basename $0` USAGE="$basename restores the original datestamp on a file after editing it \$Revision: 1.2 $ - \$Date: 2007/03/10 19:38:11 $ usage: $basename [-e cmd [-d]] [-x]* [-X]* <-d> diff the before and after files (-e only) <-e cmd> instead of interactively editing a file, perform an edit operation on it, where cmd is: delcr - delete carriage return characters cr2nl - change carriage return characters to newline characters <-x> does 'set -x' (may be used in functions and external script calls) <-X> sets debugging level--the number of times -X is specified" debuglevel=0 # -X: stores the number of times -X is specified (debugging level) xlevel=0 # -x: stores the number of times -x is specified (for optional "set -x" call in functions) and does 'set -x' dflag="" # -d: ecmd="" # -e: ####### ####### Parse parameters. ####### while getopts :de:xXz optchar ; do if [[ $debuglevel -ge 1 ]] ; then echo ">optchar=$optchar< >OPTARG=$OPTARG> >OPTIND=$OPTIND<" ; fi case $optchar in d) dflag="diff" ;; e) ecmd="$OPTARG" ;; x) xcmd="$xcmd -x" ; ${xcmd:+set -x} ; let "xlevel=$xlevel + 1" ;; # xcmd stores any -x parameters for passing to other scripts X) Xcmd="$Xcmd -X " ; let "debuglevel=$debuglevel + 1" ;; # Xcmd stores any -X parameters for passing to other scripts \?|z) echo "$USAGE" >&2 ; exit 56 ;; *) if [[ -z "$OPTARG" ]] ; then case "$optchar" in ?) errflag="-$optchar" ;; +?) errflag="$optchar" ;; *) errflag="unknown1" ;; esac else errflag="$OPTARG" fi echo "$basename: ERROR 51 - Unknown option >$errflag<" >&2 echo "$USAGE" >&2 exit 51 ;; esac done shift `expr $OPTIND - 1` ####### ####### Additional checks ####### if [[ ! -w $1 ]] ; then echo "$basename: ERROR 53 - The file >$1< is not writable and, hence, can not be edited." >&2 echo "$USAGE" >&2 exit 53 fi ####### ####### Main processing ####### tmpstr="$$.$RANDOM.$RANDOM" tmpfile="/tmp/$basename.$tmpstr" touch -r $1 $tmpfile case "$ecmd" in cr2nl) tr "\015" "\012" <$1 >$1.$tmpstr cp $1.$tmpstr $1 rm $1.$tmpstr ;; delcr) tr -d "\015" <$1 >$1.$tmpstr cp $1.$tmpstr $1 rm $1.$tmpstr ;; fixmus1) case "$1" in music_book_catalog-by_book.html) sedcmd="s/XYXYXY/Music Book Catalog--By Music Book/" ;; music_book_catalog-by_title.html) sedcmd="s/XYXYXY/Music Book Catalog--By Title|First Line Verse|First Line Chorus/" ;; esac ( echo "" tr "\015" "\012" <$1 \ | sed \ -e 's%