kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 1 | #!/bin/sh |
kate.ward | 120b504 | 2008-06-27 08:58:03 +0000 | [diff] [blame] | 2 | # |
| 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.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame^] | 17 | # |
| 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.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 27 | |
| 28 | # source shflags |
| 29 | . ../src/shflags |
| 30 | |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 31 | # configure shflags |
| 32 | DEFINE_boolean 'force' false 'force overwriting' 'f' |
| 33 | FLAGS_HELP="USAGE: $0 [flags] filename" |
| 34 | |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 35 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame^] | 36 | write_date() |
| 37 | { |
| 38 | date >"$1" |
| 39 | } |
| 40 | |
| 41 | die() |
| 42 | { |
| 43 | [ $# -gt 0 ] && echo "error: $@" >&2 |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 44 | flags_help |
| 45 | exit 1 |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame^] | 46 | } |
| 47 | |
| 48 | |
| 49 | # parse the command-line |
| 50 | FLAGS "$@" || exit 1 |
| 51 | eval set -- "${FLAGS_ARGV}" |
| 52 | |
| 53 | # check for filename |
| 54 | [ $# -gt 0 ] || die 'filename missing' |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 55 | filename=$1 |
| 56 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame^] | 57 | [ -f "${filename}" -a ${FLAGS_force} -eq ${FLAGS_FALSE} ] \ |
| 58 | && die 'filename exists; not overwriting' |
| 59 | write_date "${filename}" |