diff options
author | Kate Ward <kate.ward@forestent.com> | 2016-01-10 16:53:04 +0100 |
---|---|---|
committer | Kate Ward <kate.ward@forestent.com> | 2016-01-10 16:53:04 +0100 |
commit | 4b66ecdc0d3f6b89d3132b75f5aca1773a29a1a3 (patch) | |
tree | 9668b6e23a484f660f25ff34c98876db0276ffe6 /examples/write_date.sh | |
parent | b11509fad7f5e9e66a734116fcec7c418419ee1f (diff) | |
download | shflags-4b66ecdc0d3f6b89d3132b75f5aca1773a29a1a3.tar.gz |
restructured source for GitHub
Diffstat (limited to 'examples/write_date.sh')
-rwxr-xr-x | examples/write_date.sh | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/examples/write_date.sh b/examples/write_date.sh new file mode 100755 index 0000000..85695cf --- /dev/null +++ b/examples/write_date.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# +# Copyright 2008 Kate Ward. All Rights Reserved. +# Released under the LGPL (GNU Lesser General Public License) +# +# Author: kate.ward@forestent.com (Kate Ward) +# +# This script takes a filename as input and writes the current date to the +# file. If the file already exists, it will not be overwritten unless the '-f' +# (or '--force') flag is given. +# +# This script demonstrates several types of shFlags functionality. +# - declaration of the FLAGS_HELP variable to customize the help output +# - direct calling of the flags_help() function for script controlled usage +# output +# - handling of non-flag type command-line arguments that follow the flags +# +# Try the following: +# $ ./write_date.sh now.out +# $ cat now.out +# +# $ ./write_date.sh now.out +# $ cat now.out +# +# $ ./write_date.sh -f now.out +# $ cat now.out + +# source shflags +. ../src/shflags + +# configure shflags +DEFINE_boolean 'force' false 'force overwriting' 'f' +FLAGS_HELP="USAGE: $0 [flags] filename" + + +write_date() +{ + date >"$1" +} + +die() +{ + [ $# -gt 0 ] && echo "error: $@" >&2 + flags_help + exit 1 +} + + +# parse the command-line +FLAGS "$@" || exit 1 +eval set -- "${FLAGS_ARGV}" + +# check for filename +[ $# -gt 0 ] || die 'filename missing' +filename=$1 + +[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \ + && die 'filename exists; not overwriting' +write_date "${filename}" |