Skip to content

Instantly share code, notes, and snippets.

@thazelart
Last active February 6, 2023 03:44
Show Gist options
  • Save thazelart/44172eb6a8c7653b6463147ce783451b to your computer and use it in GitHub Desktop.
Save thazelart/44172eb6a8c7653b6463147ce783451b to your computer and use it in GitHub Desktop.
Bash script template
#!/usr/bin/env bash
#-------------------------------------------------------------------
# Name:
# Author: Team - Author
#
# Description:
#
#-------------------------------------------------------------------
# CHANGELOG
# V0.0.1 Author
# Initialization
#===================================================================
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
function print_error() {
echo "${1}" >&2
}
function usage() {
cat <<END_USAGE
How to use the script :
${0} -r "my required arg" -o "my optional arg"
${0} --required "my required arg"
The available arguments :
(Required) -r, --required <value> : required argument
(Optional) -o, --optional <value> : optional argument
(Optional) -h, --help : show this help message and exit
END_USAGE
}
#-------------------------------------------------------------------#
# GLOBAL VARIABLES #
#-------------------------------------------------------------------#
NOW=$(date +"%Y%m%d%H%M%S")
while [[ "${#}" -gt 0 ]]
do
case "${1}" in
--help|-h)
usage
exit 0
;;
-r|--required)
shift #go to next parameter to get REQUIRED_ARG
REQUIRED_ARG="${1}"
;;
-o|--optional)
shift #go to next parameter to get OPTIONAL_ARG
OPTIONAL_ARG="${1}"
;;
esac
shift
done
if [ -z "${REQUIRED_ARG-}" ]
then
print_error "$(usage)"
exit 1
fi
while true
do
read -p "You're asking to ... ${REQUIRED_ARG}, is that right? (yes/no) " yesno
case $yesno in
[Yy]*|yes) break;;
[Nn]*|no ) exit;;
esac
done
# Get logs in logfile from now
exec > >(tee "${0%.*}-${NOW}.log") 2>&1
#-------------------------------------------------------------------#
# COMMON FUNCTIONS #
#-------------------------------------------------------------------#
# This function print in color
# Inputs:
# $1: log type
# error : red
# warning: yellow
# info : blue
# title : bold blue for section title
# $2: the text to print
#####################################################################
function print_in_color() {
if [ "${1}" == "error" ]; then
color=31
elif [ "${1}" == "warning" ]; then
color=93
elif [ "${1}" == "success" ]; then
color=92
elif [ "${1}" == "info" ]; then
color=34
elif [ "${1}" == "title" ]; then
color="34;1"
echo #Blank line before section title
else
echo -ne "\e[31mERROR : Non recognized color\e[39m"
exit 1
fi
echo -ne "\e[${color}m${2}\e[0m"
}
#-------------------------------------------------------------------#
# SCRIPT FUNCTIONS #
#-------------------------------------------------------------------#
#-------------------------------------------------------------------#
# MAIN #
#-------------------------------------------------------------------#
echo "Your required argument is: ${REQUIRED_ARG}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment