blob: 85695cf7a3ff455279c257d47aa5d97929f0cf97 [file] [log] [blame]
kate.ward695ddde2008-06-26 22:22:26 +00001#!/bin/sh
kate.ward120b5042008-06-27 08:58:03 +00002#
3# Copyright 2008 Kate Ward. All Rights Reserved.
4# Released under the LGPL (GNU Lesser General Public License)
5#
6# Author: kate.ward@forestent.com (Kate Ward)
7#
8# This script takes a filename as input and writes the current date to the
9# file. If the file already exists, it will not be overwritten unless the '-f'
10# (or '--force') flag is given.
11#
12# This script demonstrates several types of shFlags functionality.
13# - declaration of the FLAGS_HELP variable to customize the help output
14# - direct calling of the flags_help() function for script controlled usage
15# output
16# - handling of non-flag type command-line arguments that follow the flags
kate.wardc5210682009-03-30 18:54:36 +000017#
18# Try the following:
19# $ ./write_date.sh now.out
20# $ cat now.out
21#
22# $ ./write_date.sh now.out
23# $ cat now.out
24#
25# $ ./write_date.sh -f now.out
26# $ cat now.out
kate.ward695ddde2008-06-26 22:22:26 +000027
28# source shflags
29. ../src/shflags
30
kate.ward695ddde2008-06-26 22:22:26 +000031# configure shflags
32DEFINE_boolean 'force' false 'force overwriting' 'f'
33FLAGS_HELP="USAGE: $0 [flags] filename"
34
kate.ward695ddde2008-06-26 22:22:26 +000035
kate.wardc5210682009-03-30 18:54:36 +000036write_date()
37{
38 date >"$1"
39}
40
41die()
42{
43 [ $# -gt 0 ] && echo "error: $@" >&2
kate.ward695ddde2008-06-26 22:22:26 +000044 flags_help
45 exit 1
kate.wardc5210682009-03-30 18:54:36 +000046}
47
48
49# parse the command-line
50FLAGS "$@" || exit 1
51eval set -- "${FLAGS_ARGV}"
52
53# check for filename
54[ $# -gt 0 ] || die 'filename missing'
kate.ward695ddde2008-06-26 22:22:26 +000055filename=$1
56
kate.wardc5210682009-03-30 18:54:36 +000057[ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \
58 && die 'filename exists; not overwriting'
59write_date "${filename}"