#!/bin/sh # # 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 source ../src/shflags # configure shflags DEFINE_boolean 'force' false 'force overwriting' 'f' setglobal FLAGS_HELP = ""USAGE: $0 [flags] filename"" proc write_date { date >$1 } proc die { test $Argc -gt 0 && echo "error: $ifsjoin(Argv)" > !2 flags_help exit 1 } # parse the command-line FLAGS @Argv || exit 1 eval set -- $(FLAGS_ARGV) # check for filename test $Argc -gt 0 || die 'filename missing' setglobal filename = $1 test -f $(filename) -a $(FLAGS_force) -eq $(FLAGS_FALSE) \ && die 'filename exists; not overwriting' write_date $(filename)