blob: b8e610b27e389c495472b6aca38566a758691cdc [file] [log] [blame]
kate.ward695ddde2008-06-26 22:22:26 +00001#!/bin/sh
kate.ward120b5042008-06-27 08:58:03 +00002#
kate.ward120b5042008-06-27 08:58:03 +00003# This script takes a filename as input and writes the current date to the
4# file. If the file already exists, it will not be overwritten unless the '-f'
5# (or '--force') flag is given.
6#
7# This script demonstrates several types of shFlags functionality.
8# - declaration of the FLAGS_HELP variable to customize the help output
9# - direct calling of the flags_help() function for script controlled usage
10# output
11# - handling of non-flag type command-line arguments that follow the flags
kate.wardc5210682009-03-30 18:54:36 +000012#
13# Try the following:
14# $ ./write_date.sh now.out
15# $ cat now.out
16#
17# $ ./write_date.sh now.out
18# $ cat now.out
19#
20# $ ./write_date.sh -f now.out
21# $ cat now.out
kate.ward695ddde2008-06-26 22:22:26 +000022
23# source shflags
24. ../src/shflags
25
kate.ward695ddde2008-06-26 22:22:26 +000026# configure shflags
27DEFINE_boolean 'force' false 'force overwriting' 'f'
28FLAGS_HELP="USAGE: $0 [flags] filename"
29
kate.ward695ddde2008-06-26 22:22:26 +000030
kate.wardc5210682009-03-30 18:54:36 +000031write_date()
32{
33 date >"$1"
34}
35
36die()
37{
38 [ $# -gt 0 ] && echo "error: $@" >&2
kate.ward695ddde2008-06-26 22:22:26 +000039 flags_help
40 exit 1
kate.wardc5210682009-03-30 18:54:36 +000041}
42
43
44# parse the command-line
45FLAGS "$@" || exit 1
46eval set -- "${FLAGS_ARGV}"
47
48# check for filename
49[ $# -gt 0 ] || die 'filename missing'
kate.ward695ddde2008-06-26 22:22:26 +000050filename=$1
51
kate.wardc5210682009-03-30 18:54:36 +000052[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \
53 && die 'filename exists; not overwriting'
54write_date "${filename}"