#! /bin/bash
false()
{
return 1
}
true()
{
return 0
}
phelp()
{
echo "$name: do a $cmd with extra checking and options.
$Usage
$name is used as a front end for $cmd to get the [icfo] options, and so
that a trailing / will force the last component of the path to be
interpreted as a directory, so that $name foo bar/ will fail if bar is
not an existing directory, instead of changing the name of foo to bar.
Effectively, $name foo bar/ is short for $name foo bar/foo
Options:
-h prints this help.
-c checks first for the existence of each file, and fails if it exists.
-i is like -c except that if the file exists and stdin and stdout are a
tty, a query is printed and a reply is read; a file is overwritten only
if the reply begins with 'y'.
-f unsets -c and -i (in case $cmd is aliased to $name).
-o (overwrite only) checks that the named file(s) exist and fails for any
that do not. It is the complement of the -c option.
Whichever of [cifo] comes later on the command line determines the behaviour.
Any of these options must come before any standard $cmd options."
}
typeset interactive=false noover=false overwrite=false debug=false
name=${0
case "$name" in
ncp|nmv) cmd=/bin/${name*) echo "$name: Must be invoked as ncp or nmv." 1>&2 ; exit 2;;
esac
Usage="Usage: $name [-cfhio] $cmd-cmd-line"
while getopts :cfhiox opt; do
case $opt in
h) phelp; exit 0;;
x) debug=true ;;
c) noover=true ;;
i) noover=true ; interactive=true ;;
f) noover=false ; interactive=false ;;
o) overwrite=true ; noover=false ; interactive=false;;
+?) echo "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
?) echo "$name: $OPTARG: bad option. Use -h for help." 1>&2 ; exit 2;;
esac
done
shift $((OPTIND - 1))
if [ $ echo -e "$Usage\nUse -h for help."
exit
fi
Check()
{
if [ ! -f "$1" ] && $overwrite; then
echo "$name: $1: File does not exist." 1>&2
return 1
elif [ -f "$1" ] && $noover; then
if [ $interactive = false ] || [ ! -t 0 ] || [ ! -t 1 ]; then
echo "$name: $1: File exists." 1>&2
return 1
else
while :; do
echo -n \
"$name: $1: File exists. Overwrite? (y)es/(n)o/(a)bort/(Y)es for all: " 1>&2
read reply
case "$reply" in
y*)
echo "$name: Overwriting $1."
return 0
;;
Y*)
echo "$name: Overwriting $1."
interactive=false
noover=false
return 0
;;
[nN]*)
echo "$name: Skipping $2."
return 1
;;
[aA]*)
echo "$name: Aborting."
exit 1
;;
*)
echo "$name: Invalid response." 1>&2
;;
esac
done
fi
else
return 0
fi
}
typeset -i i=0 lastarg=$(($
argv=("$@")
$debug && echo argv = "${argv[@]}" 1>&2
dest=${argv[lastarg]}
if $debug; then
echo \
"interactive=$interactive noover=$noover overwrite=$overwrite debug=$debug
lastarg=$lastarg dest=$dest name=$name cmd=$cmd
files=$*" 1>&2
fi
if $noover || $overwrite; then
$debug && echo "checking for existance of directories..." 1>&2
if [ $ Check "$dest" "$1" || exit 0 else
while [ $i -lt $lastarg ]; do
Check "$dest/${argv[i]##*/}" "${argv[i]}" || unset argv[i]
let i+=1
done
fi
fi
[ ${
lastisslash()
{
case "$1" in
*/) return 0;;
*) return 1;;
esac
}
if [ ${ $debug && echo "Appending filename." 1>&2
argv=("${argv[@]}")
argv[1]="${argv[1]}/${argv[0]##*/}"
fi
$debug && echo "Executing command: $cmd ${argv[@]}" 1>&2
exec $cmd "${argv[@]}"