diff options
author | Kate Ward <kate.ward@forestent.com> | 2018-01-25 15:15:41 +0100 |
---|---|---|
committer | Kate Ward <kate.ward@forestent.com> | 2018-01-25 15:15:41 +0100 |
commit | 51ccc276e36e2554b593fc4c37ecf098bf96cc50 (patch) | |
tree | e237ab79dd3ba70d180a9f3c9bc9c169ac6f8af2 | |
parent | 9123f58a2e024398adc92e4d4eb32a06a3afc674 (diff) | |
download | shflags-51ccc276e36e2554b593fc4c37ecf098bf96cc50.tar.gz |
Prevent redirection operators from overwriting files.
-rwxr-xr-x | examples/write_date.sh | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/examples/write_date.sh b/examples/write_date.sh index b8e610b..2fa6895 100755 --- a/examples/write_date.sh +++ b/examples/write_date.sh @@ -20,35 +20,35 @@ # $ ./write_date.sh -f now.out # $ cat now.out -# source shflags -. ../src/shflags +# Prevent overwriting files with redirection operators. +set -C >/dev/null 2>&1 -# configure shflags +# Source shFlags. +. ../shflags || ( echo "fatal: unable to source shflags" >&2; exit 64; ) + +# Configure shFlags. DEFINE_boolean 'force' false 'force overwriting' 'f' FLAGS_HELP="USAGE: $0 [flags] filename" - -write_date() -{ - date >"$1" +write_date() { + date >"$1" } -die() -{ +die() { [ $# -gt 0 ] && echo "error: $@" >&2 flags_help exit 1 } - -# parse the command-line +# Parse the command-line. FLAGS "$@" || exit 1 eval set -- "${FLAGS_ARGV}" -# check for filename +# Check for filename. [ $# -gt 0 ] || die 'filename missing' filename=$1 -[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \ - && die 'filename exists; not overwriting' +[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] && \ + die "output file \"${filename}\" exists, not overwriting" + write_date "${filename}" |