SlideShare a Scribd company logo
#!/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
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

More Related Content

TXT
Cpsh sh
PDF
The Magic Of Tie
ODP
Maintaining your own branch of Drupal core
TXT
Pop3ck sh
PPT
Shell and perl scripting classes in mumbai
PDF
Perl Bag of Tricks - Baltimore Perl mongers
KEY
Total World Domination with i18n (es)
RTF
Cpsh sh
The Magic Of Tie
Maintaining your own branch of Drupal core
Pop3ck sh
Shell and perl scripting classes in mumbai
Perl Bag of Tricks - Baltimore Perl mongers
Total World Domination with i18n (es)

What's hot (20)

PDF
Parsing JSON with a single regex
TXT
Logrotate sh
PPT
spug_2008-08
KEY
Advanced Shell Scripting
PDF
WordPress Cuztom Helper
PDF
2014 database - course 2 - php
PDF
Yahoo! JAPANとKotlin
PDF
Shell实现的windows回收站功能的脚本
PDF
My First Ruby
PDF
Bag of tricks
PPT
An Elephant of a Different Colour: Hack
PPTX
Ch3(working with file)
PDF
Perl Fitxers i Directoris
PDF
Simplifying code monster to elegant in n 5 steps
PPT
Oo Perl
ODP
MuseScore MusicHackDay Presentation
PPTX
The groovy puzzlers (as Presented at JavaOne 2014)
PDF
Taking Perl to Eleven with Higher-Order Functions
PPT
Unix shell scripting basics
ODT
Huong dan cai dat hadoop
Parsing JSON with a single regex
Logrotate sh
spug_2008-08
Advanced Shell Scripting
WordPress Cuztom Helper
2014 database - course 2 - php
Yahoo! JAPANとKotlin
Shell实现的windows回收站功能的脚本
My First Ruby
Bag of tricks
An Elephant of a Different Colour: Hack
Ch3(working with file)
Perl Fitxers i Directoris
Simplifying code monster to elegant in n 5 steps
Oo Perl
MuseScore MusicHackDay Presentation
The groovy puzzlers (as Presented at JavaOne 2014)
Taking Perl to Eleven with Higher-Order Functions
Unix shell scripting basics
Huong dan cai dat hadoop
Ad

Viewers also liked (15)

TXT
Getfilestruct zbksh(1)
PDF
Analysis of the Performance of Sea Level Stations at Haiti
PPTX
Firewall
TXT
Getfilestruct zbksh
PDF
Slowinski_Portfolio
PDF
TXT
An a z index of the bash commands
TXT
Bouncingballs sh
TXT
Applecmdlista zs
PDF
Stefanie Lopez Angel - PPP Final
TXT
Xz file-format-1.0.4
TXT
An a z index of windows power shell commandss
TXT
Compound var
DOCX
Cuadro sipnotico
PDF
Pcad mision sucre
Getfilestruct zbksh(1)
Analysis of the Performance of Sea Level Stations at Haiti
Firewall
Getfilestruct zbksh
Slowinski_Portfolio
An a z index of the bash commands
Bouncingballs sh
Applecmdlista zs
Stefanie Lopez Angel - PPP Final
Xz file-format-1.0.4
An a z index of windows power shell commandss
Compound var
Cuadro sipnotico
Pcad mision sucre
Ad

Similar to Mkscript sh (20)

PDF
Unix 1st sem lab programs a - VTU Karnataka
PDF
BSDM with BASH: Command Interpolation
PDF
Unleash your inner console cowboy
PDF
Unleash your inner console cowboy
PPT
Talk Unix Shell Script
PPT
Talk Unix Shell Script 1
PDF
BASH Variables Part 1: Basic Interpolation
PDF
Shell scripting
PDF
Linux/Unix Commands
PPT
Unix 5 en
PDF
Bash production guide
PPT
Linux ppt
PDF
Bash Scripting Workshop
PPT
ODP
ODP
DOCX
PDF
Lets make better scripts
PPTX
Unix Trainning Doc.pptx
Unix 1st sem lab programs a - VTU Karnataka
BSDM with BASH: Command Interpolation
Unleash your inner console cowboy
Unleash your inner console cowboy
Talk Unix Shell Script
Talk Unix Shell Script 1
BASH Variables Part 1: Basic Interpolation
Shell scripting
Linux/Unix Commands
Unix 5 en
Bash production guide
Linux ppt
Bash Scripting Workshop
Lets make better scripts
Unix Trainning Doc.pptx

More from Ben Pope (8)

PDF
Programming collaborative-ref
TXT
Popstat1 sh
TXT
Pop3stat sh
TXT
Phadd sh
TXT
Phdel sh
TXT
Menu func-sh
TXT
Menu func-sh(1)
TXT
Luhn sh
Programming collaborative-ref
Popstat1 sh
Pop3stat sh
Phadd sh
Phdel sh
Menu func-sh
Menu func-sh(1)
Luhn sh

Recently uploaded (20)

PDF
Business model innovation report 2022.pdf
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PPTX
Business Ethics - An introduction and its overview.pptx
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
PPTX
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PDF
Laughter Yoga Basic Learning Workshop Manual
PDF
Traveri Digital Marketing Seminar 2025 by Corey and Jessica Perlman
PPTX
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
PDF
A Brief Introduction About Julia Allison
PPTX
Probability Distribution, binomial distribution, poisson distribution
PDF
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
PDF
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
PPT
340036916-American-Literature-Literary-Period-Overview.ppt
PPTX
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PDF
Deliverable file - Regulatory guideline analysis.pdf
PDF
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry
PDF
How to Get Funding for Your Trucking Business
PDF
Unit 1 Cost Accounting - Cost sheet
Business model innovation report 2022.pdf
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
Business Ethics - An introduction and its overview.pptx
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
job Avenue by vinith.pptxvnbvnvnvbnvbnbmnbmbh
unit 1 COST ACCOUNTING AND COST SHEET
Laughter Yoga Basic Learning Workshop Manual
Traveri Digital Marketing Seminar 2025 by Corey and Jessica Perlman
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
A Brief Introduction About Julia Allison
Probability Distribution, binomial distribution, poisson distribution
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
340036916-American-Literature-Literary-Period-Overview.ppt
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
Ôn tập tiếng anh trong kinh doanh nâng cao
Deliverable file - Regulatory guideline analysis.pdf
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry
How to Get Funding for Your Trucking Business
Unit 1 Cost Accounting - Cost sheet

Mkscript sh

  • 1. #!/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()
  • 2. { 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
  • 3. -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
  • 4. 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`
  • 5. 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
  • 6. 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