modular shell boilerplate module¶
here's some ugly and old shell scripts that are actually usefull for boilerplates
todo: cleanup, update and finish
q: what the hell is this?
a: it's a dirty old command line boilerplate
you set up functions and call them with parameters
usage
./cli.sh dostuff
./cli.sh domorestuff
#!/bin/bash
#cli.sh
# basic shell boilerplate
# add your parameters here
# jobname (required) & date (optional)
jobname=$1
rundate=$2
# set date (for logging not for the job rundate)
LOGDATE=`date '+%Y-%m-%d %H:%M:%S'`
# start of the script echo host / user / date
echo '----) cli.sh starting - ' $LOGDATE
echo $HOSTNAME
runuser=$(whoami)
echo $runuser
# add your helpers here
# show functions for console help
show_functions(){
# job help
echo 'function list'
echo 'functions starting with job_ are accepted jobnames'
declare -F
}
# exit if params are jacked
bad_parameters () {
echo >&2 "$@"
echo ' '
echo '----) cli.sh ending - ' $LOGDATE
show_functions
exit 1
}
# add tasks here
test_task(){
echo ' ----> task one started '
echo ' '
echo 'i got passed ' $1
echo ' ----> task one ended '
echo ' '
}
test_task_two(){
echo ' ----> task one started '
echo ' '
echo 'i got passed ' $1
echo ' ----> task one ended '
echo ' '
}
# add jobs here
# jobs call multiple tasks typically
job_test(){
echo ' ====> test_job started'
echo ' '
echo $jobname ' is the jobname'
echo $rundate ' is the rundate'
echo 'job_test' was passed $1
test_task $LOGDATE
test_task_two wtf
echo ' ====> test_job ended'
echo ' '
}
# validation - runs with 1 or 2 parameters - echo usage & exit
echo ' '
echo 'validating parameters '
echo ' '
[ "$#" -lt 1 ] && bad_parameters "usage is cli (jobname - required) (yyyy-mm-dd - optional), $# provided"
[ "$#" -gt 2 ] && bad_parameters "usage is cli (jobname - required) (yyyy-mm-dd - optional), $# parameters ($@) exceeds the maximum"
[ "$#" -ge 1 ] && [ "$#" -le 2 ] && echo $@ && echo ' ' && echo "parameters are valid"
# if rundate is not set this automatically set rundate to today
[ "$#" -eq 1 ] && rundate=`date '+%Y-%m-%d %H:%M:%S'` && echo 'setting date'
# run the job
# add a call to your job function here
echo 'configuring settings for ' $jobname
if [ $jobname = job_test ]; then
echo 'starting job ' $jobname && job_test omg; else
echo $jobname ' did not match the job list' && show_functions
# todo: put an else snippet here
fi
echo ' '
echo '----) cli.sh ending - ' $LOGDATE