aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBob Badour <bbadour@google.com>2020-05-19 18:54:58 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2020-05-19 18:54:58 +0000
commit32260528c6f69472d26f87e02d33df7845f8293a (patch)
treeda581028905318f358769fc7bb6f8aa7373e2fb7 /examples
parentb64aac5c5f841c8f19dca3af8c9c54cb673e39de (diff)
parent7ef808bcfab8caf6982638109313922bd01ebabe (diff)
downloadshflags-32260528c6f69472d26f87e02d33df7845f8293a.tar.gz
Merge remote-tracking branch 'aosp/upstream-master' into mymerge am: 2e0d635a87 am: 3dac286b93 am: 7ef808bcfa
Change-Id: I195127d7fcb35686c41a0095d886ba39da60bc5c
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/debug_output.sh16
-rwxr-xr-xexamples/hello_world.sh8
-rwxr-xr-xexamples/write_date.sh47
3 files changed, 38 insertions, 33 deletions
diff --git a/examples/debug_output.sh b/examples/debug_output.sh
index b3ecec2..a5e61c8 100755
--- a/examples/debug_output.sh
+++ b/examples/debug_output.sh
@@ -11,10 +11,10 @@
# $ ./debug_output.sh sing
# $ ./debug_output.sh --debug sing
-# source shflags
-. ../src/shflags
+# Source shflags.
+. ../shflags
-# define flags
+# Define flags.
DEFINE_boolean 'debug' false 'enable debug mode' 'd'
FLAGS_HELP=`cat <<EOF
commands:
@@ -22,21 +22,17 @@ commands:
sing: sing something
EOF`
-
-debug()
-{
+debug() {
[ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return
echo "DEBUG: $@" >&2
}
-die() {
- [ $# -gt 0 ] && echo "error: $@" >&2
+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}"
diff --git a/examples/hello_world.sh b/examples/hello_world.sh
index 84b755a..e5fe81c 100755
--- a/examples/hello_world.sh
+++ b/examples/hello_world.sh
@@ -7,13 +7,13 @@
# '--name'). If a name is given, it is output, otherwise the default of 'world'
# is output.
-# source shflags
-. ../src/shflags
+# Source shflags.
+. ../shflags
-# define a 'name' command-line string flag
+# Define a 'name' command-line string flag.
DEFINE_string 'name' 'world' 'name to say hello to' 'n'
-# parse the command-line
+# Parse the command-line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
diff --git a/examples/write_date.sh b/examples/write_date.sh
index b8e610b..d9ae3ec 100755
--- a/examples/write_date.sh
+++ b/examples/write_date.sh
@@ -20,35 +20,44 @@
# $ ./write_date.sh -f now.out
# $ cat now.out
-# source shflags
-. ../src/shflags
+# Source shFlags.
+. ../shflags
-# configure 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
+die() {
+ [ $# -gt 0 ] && echo "error: $@"
flags_help
exit 1
}
-
-# parse the command-line
+# Parse the command-line.
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
-# check for filename
-[ $# -gt 0 ] || die 'filename missing'
+# Check for filename on command-line.
+[ $# -gt 0 ] || die 'filename missing.'
filename=$1
-[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \
- && die 'filename exists; not overwriting'
-write_date "${filename}"
+# Redirect STDOUT to the file ($1). This seemingly complicated method using exec
+# is used so that a potential race condition between checking for the presence
+# of the file and writing to the file is mitigated.
+if [ ${FLAGS_force} -eq ${FLAGS_FALSE} ] ; then
+ [ ! -f "${filename}" ] || die "file \"${filename}\" already exists."
+ # Set noclobber, redirect STDOUT to the file, first saving STDOUT to fd 4.
+ set -C
+ exec 4>&1 >"${filename}" # This fails if the file exists.
+else
+ # Forcefully overwrite (clobber) the file.
+ exec 4>&1 >|"${filename}"
+fi
+
+# What time is it?
+date
+
+# Restore STDOUT from file descriptor 4, and close fd 4.
+exec 1>&4 4>&-
+
+echo "The current date was written to \"${filename}\"."