#!/bin/bash # /usr/local/bin/mkscript # Copyright 2000, Chris F.A. Johnson # Released under the terms of the GNU General Public License # USAGE: mkscript command # fix permissions for script "command"-sh in /usr/local/bin/scripts # create it if it doesn't exist # link it to "command" in /usr/local/bin die() { exitcode=$1 shift if [ "$*" ] then echo "$*" fi exit ${exitcode:-1} } version() { echo $version } usage() { echo " $progname - USAGE: $progname [OPTIONS] OPTIONS: -d DESC - description -f - force=1 ;; # replace file if it exists -r - remove=1 ;; # remove script if is exists -R - remove=2 ;; # not used -a OPTS - options that take arguments -o OPTS - options that do not take arguments -u - user=$OPTARG ;; -a OPTS - argopts=$OPTARG ;; -h - help: print this message -H - help: print more detailed message -v - verbose: -V - print version information Copyright 2001, Chris F.A. Johnson " } put_header() { echo "#!/bin/bash # `date` # NAME: $command # Copyright `date +%Y`, $author # Released under the terms of the GNU General Public License " } func_die() { echo 'die() { exitcode=$1 shift if [ "$*" ] then echo "$*" fi exit ${exitcode:-1} } ' } func_version() { echo 'version() { echo " $progname, version $version Copyright $copyright, $author $email This is free software, released under the terms of the GNU General Public License. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. " } ' } func_usage() { echo "usage() { echo \" \$progname - \$description USAGE: \$progname [OPTIONS] OPTIONS: \"" { n=0 while [ $n -lt ${#argopts} ] do l=${argopts:$n:1} echo " -$l arg - " n=$(( $n + 1 )) done n=0 while [ $n -lt ${#opts} ] do l=${opts:$n:1} echo " -$l - " n=$(( $n + 1 )) done } | sort echo " [ \$longusage -eq 1 ] && echo \" -h - help: print shorter help message -H - help: print this message\" || echo \" -h - help: print this message -H - help: print more detailed message\" echo \" -v - verbose: -V - print version information \" [ \$longusage -eq 1 ] && version || echo \" Copyright $copyright, $author \" } " } put_vars() { echo " description=\"$description\" verbose=0 longusage=0 version=\"1.0\" copyright=$copyright author=\"$author\"" echo 'progname=${0##*/}' } put_getopts() { [ -n "$argopts" ] && { o=0 while [ $o -lt ${#argopts} ] do l=${argopts:$o:1} [ "$l" ] && getopts="$getopts${NL} $l) ${l}_arg=\$OPTARG ;;" optstr=${optstr}${l}: o=$(( $o + 1 )) done } [ -n "$opts" ] && { o=0 while [ $o -lt ${#opts} ] do l=${opts:$o:1} [ "$l" ] && getopts="$getopts${NL} $l) ${l}_opt=1 ;;" optstr=${optstr}${l} o=$(( $o + 1 )) done } echo " while getopts vVhH-:$optstr var do case \$var in" echo "$getopts" | sort echo ' -) case $OPTARG in help) usage; exit ;; help_long) longusage=1; usage; exit ;; version) version; exit ;; esac ;; h) usage; exit ;; H) longusage=1; usage; exit ;; v) verbose=$(( $verbose + 1 )) ;; V) version; exit ;; *);; esac done shift $(( $OPTIND - 1 )) ' } bindir="/usr/local/bin" shdir="${bindir}/scripts" version="1.1" verbose=0 force=0 remove=0 longusage=0 progname=${0##*/} NL=$'\n' while getopts vVhHfro:a:d:u: var do case "$var" in d) description=$OPTARG ;; f) force=1 ;; # replace file if it exists r) remove=1 ;; R) remove=2 ;; a) argopts=$OPTARG ;; o) opts=$OPTARG ;; u) user=$OPTARG ;; a) argopts=$OPTARG ;; v) verbose=$(( $verbose + 1 )) ;; V) version; exit ;; h) usage; exit ;; H) longusage=1; usage; exit ;; *);; esac done shift $(( $OPTIND - 1 )) echo \*=$* echo opts=$opts echo argopts=$argopts if [ ! "$1" ] then die 1 "${progname}: command name required" fi cd ${shdir} command=${1} shcommand=${command}-sh author="`grep -w ^$USER /etc/passwd | cut -d: -f5 | cut -d "," -f1`" copyright=`date +%Y` if [ $remove -ge 1 ] then if [ -f "$shcommand" -a $remove -eq 2 ] then rm -f $shcommand fi if [ -f $bindir/$command ] then rm -f $bindir/$command fi ## add code to remove any other aliases in /usr/local/bin exit fi if [ ! -f $bindir/$command ] then touch $shdir/$shcommand cd $bindir ln -s $shdir/$shcommand $command fi if [ -f "$shcommand" -a $force = 0 ];then echo " $shcommand already exists Use -f to replace it" exit fi ( put_header func_version func_usage put_vars { [ -n "$argopts" ] && { echo $argopts | while read -n1 l do [ "$l" ] && echo "${l}_arg=" done } [ -n "$opts" ] && { echo $opts | while read -n1 l do [ "$l" ] && echo "${l}_opt=0" done } } | sort put_getopts ) > ${shdir}/$shcommand chown ${user:-$USER} ${shdir}/$shcommand chmod a+x ${shdir}/${shcommand} cd $bindir pwd ln -fs scripts/$shcommand $command ls -l ${shdir}/$shcommand