====== Template: Bash Script ======
**General Information**
Templates for creating two types of bash scripts:
- A single script with arguments
- A bash script that provides logging and launches a child script.
**Checklist**
* Distro(s): Any
----
====== Single Script with Arguments ======
This script has arguments and a usage/help function ready to go.
#!/bin/bash
# Name: script-name-here.sh
# Description: Script description here
# Last Modified: 2016-06-21
# Recent Changes: -Change 1
# -Change 2
##### Customize These Variables #####
# Customization
var1="myvar"
##### End of Customize Variables #####
#=====================================
# Functions; Main starts after
#=====================================
function show_usage
{
echo -e "\n==== Script Name Usage ===="
echo -e "\nDescripton: Script description HERE.."
echo -e "\n--Usage--"
echo -e "-h => Display usage."
echo -e "-d argument here => Another function."
echo -e "\n--Other Requirements--"
echo -e "-> Run as either root or regular user with sudo privileges."
echo -e
}
#=======================
# Get Script Arguments
#=======================
# Reset POSIX variable in case it has been used previously in this shell
OPTIND=1
while getopts "hd:" opt; do
case "${opt}" in
h) # -h (help) argument
show_usage
exit 0
;;
d) # -d argument-description
dir=${OPTARG}
;;
*) # invalid argument
show_usage
exit 0
;;
esac
done
#===================
# Pre-checks: Make sure we have good options set
#===================
# Ensure argument is provided
if [[ -z ${dir} ]]; then
echo -e ">> ERROR! A directory must be provided as an argument."
show_usage
exit 1
fi
#===================
# Main starts here
#===================
# Do some stuff
echo -e ">> Doing some stuff at: ${dir}"
echo "Hello" >> ${dir}/myfile.txt
echo -e "\n============================"
echo -e "=- Script Name Completed. -="
echo -e "============================"
----
====== Script with Logging and Child ======
This type of script has a parent script that provides the logging and control. It launches a child or worker script.
----
===== Parent Script =====
This is the script to execute that will provide logging and launch the child script.
###############################################################################################
#!/bin/bash
# Name: script_skel.sh
# Description: Template wrapper script for script_worker_skel.sh that provides logging.
###############################################################################################
##### Customize These Variables #####
# Log file
log_file="${HOME}/script.log"
# Name of worker script to execute
worker="script_worker_skel.sh"
#### End Customize Variables ####
# Store this script's name and figure out the base path
script_name="$(basename ${0})"
base_path=$(echo ${0} | sed "s/${script_name}//")
# Clear log and timestamp the beginning
echo -e ">>Logging output and errors to: ${log_file}\n"
cat /dev/null > ${log_file}
echo -e "---- Log Started: $(date) ----\n" >> ${log_file}
## Execute Script ##
${base_path}${worker} 2>&1 | tee -a ${log_file}
# Close log file
echo -e "\n---- Log Completed: $(date) ----" >> ${log_file}
# Reminder of log file location
echo -e "\n>>Logged output and errors was sent to: ${log_file}"
----
===== Child Script =====
This script does all the work and is launched via its parent script.
###############################################################################################
#!/bin/bash
# Name: script_worker_skel.sh
# Description: Template for a worker script. Launched via parent script: script_skel.sh
###############################################################################################
##### Customize These Variables #####
#variable1="value"
##### End of Customize Variables #####
#### Functions Here: Main Starts After ####
# function name{
#
#}
#### End of Functions ####
#==================
# Main Starts Here
#==================
# Pre-checks
#==============================================================
# Confirm running the script
#==============================================================
echo -e "======================================================"
echo -e "####=============== Name of Script ===============####"
echo -e "======================================================"
echo
echo -e "Warning: This script will..."
echo -e "\n=>Continue?[y/n]:\c"
read run_script
if [[ ${run_script} != "y" ]]; then
echo -e "\n>>Will not run the Name of Script. Exiting..."
exit 1
fi
# Do some stuff
echo -e "\n======================================================"
echo -e "####========== Name of Script Complete ===========####"
echo -e "======================================================"
----