Skip to content

Instantly share code, notes, and snippets.

@anniethiessen
Last active October 26, 2023 00:31
Show Gist options
  • Select an option

  • Save anniethiessen/efb6bc0e52ccfc8b330aa41364b53e97 to your computer and use it in GitHub Desktop.

Select an option

Save anniethiessen/efb6bc0e52ccfc8b330aa41364b53e97 to your computer and use it in GitHub Desktop.
Script file to run prior to other script files for more unified and pretty shell scripts.
#!/usr/bin/env bash
: '
Script file to run prior to other script files for more unified and pretty shell scripts.
Accepts -v (verbose output) and -q (quiet output) arguments.
Features:
- variable "QUIET_OUTPUT": set by -q option
(e.g. % command &> $\{QUIET_OUTPUT}\)
- variable "VERBOSE_OUTPUT": set by -v option
(e.g. % command &> $\{VERBOSE_OUTPUT}\)
- variable "OUTPUT": flag-specified output-type.
Defaults to verbose (e.g. % command &> $\{OUTPUT}\)
- variable "DEFAULT_FONT": (e.g. % echo "$\{DEFAULT_FONT\}text")
- variable "HEADER_FONT": (e.g. % echo "$\{HEADER_FONT\}header text")
- variable "INFO_FONT": (e.g. % echo "$\{INFO_FONT\}info text")
- variable "QUESTION_FONT": (e.g. % echo "$\{QUESTION_FONT\}question")
- variable "SUCCESS_FONT": (e.g. % echo "$\{SUCCESS_FONT\}success text")
- variable "WARNING_FONT": (e.g. % echo "$\{WARNING_FONT\}warning text")
- variable "ERROR_FONT": (e.g. % echo "$\{ERROR_FONT\}error text")
- variable "SUCCESS_MARK": beta, untested
- variable "WARNING_MARK": beta, untested
- variable "ERROR_MARK": beta, untested
- variable "PROMPT_VERBOSE": prints verbose prompt for debugging
(e.g. % output_error_message "text" $\{PROMPT_VERBOSE\})
- function "output_message": (e.g. % output_message "text")
- function "output_header_message": (e.g. % output_header_message "header text")
- function "output_info_message": (e.g. % output_info_message "info text")
- function "output_question_message": (e.g. % output_question_message "question")
- function "output_success_message": (e.g. % output_success_message "success text")
- function "output_warning_message": prompts verbose if ""OUTPUT"" is set to quiet
(e.g. % output_warning_message "warning text")
- function "output_error_message": prompts verbose if ""OUTPUT"" is set to quiet
(e.g. % output_error_message "error text")
- function "print_progress_bar": beta, untested
- function "exit_script": prints exit message and exits script with arg-specified or 1 code
Usage Example:
#!/usr/bin/env bash
wget -cO - "<this_file_url>" > "shell_script_essentials.sh"
chmod +x "shell_script_essentials.sh"
source "shell_script_essentials.sh" "$@"
rm -rf "shell_script_essentials.sh"
#The rest of your script
'
#--------------------------------------------
#----------------- CONSTANTS ----------------
#--------------------------------------------
PROMPT_VERBOSE="--p"
QUIET_OUTPUT="/dev/null"
VERBOSE_OUTPUT="/dev/stdout"
OUTPUT=${QUIET_OUTPUT}
DEFAULT_FONT="\033[0m"
HEADER_FONT="\033[1;35m"
INFO_FONT="\033[1;36m"
QUESTION_FONT="\033[1;34m"
SUCCESS_FONT="\033[1;32m"
WARNING_FONT="\033[1;33m"
ERROR_FONT="\033[1;31m"
SUCCESS_MARK="☑"
WARNING_MARK="☐"
ERROR_MARK="☒"
#--------------------------------------------
#----------------- FUNCTIONS ----------------
#--------------------------------------------
function set_options {
local OPTIND flag q v
while getopts ":qv" flag; do
case "${flag}" in
q) OUTPUT=${QUIET_OUTPUT};;
v) OUTPUT=${VERBOSE_OUTPUT};;
*)
esac
done
shift $((OPTIND-1))
}
function prompt_verbose {
if [[ ${OUTPUT} == "${QUIET_OUTPUT}" ]]; then
echo "Run script with -v flag for more info."
fi
}
function output_message {
local output_msg=$1
echo "${DEFAULT_FONT}${output_msg}"
}
function output_header_message {
local output_msg=$1
echo "${HEADER_FONT}${output_msg}${DEFAULT_FONT}"
}
function output_info_message {
local output_msg=$1
echo "${INFO_FONT}${output_msg}${DEFAULT_FONT}"
}
function output_question_message {
local output_msg=$1
echo "${QUESTION_FONT}${output_msg}${DEFAULT_FONT}"
}
function output_success_message {
local output_msg=$1
echo "${SUCCESS_FONT}${output_msg}${DEFAULT_FONT}"
}
function output_warning_message {
local output_msg=$1
local prompt_verbose=$2
echo "${WARNING_FONT}${output_msg}${DEFAULT_FONT}"
if [[ ${prompt_verbose} == "${PROMPT_VERBOSE}" ]]; then prompt_verbose; fi
}
function output_error_message {
local output_msg=$1
local prompt_verbose=$2
echo "${ERROR_FONT}${output_msg}${DEFAULT_FONT}"
if [[ ${prompt_verbose} == "${PROMPT_VERBOSE}" ]]; then prompt_verbose; fi
}
function print_progress_bar {
local current_state=$1
local total_state=$2
progress=$(((current_state*100)/(total_state*100)/100))
done=$((progress*4/10))
left=$((40-done))
fill=$(printf "%${done}s")
empty=$(printf "%${left}s")
printf "\rProgress : [${fill// /#}${empty// /-}] ${progress}%%"
if [[ ${current_state} -ge ${total_state} ]]; then
printf "\n"
fi
}
function exit_script {
local exit_code=$1
if [[ -z ${exit_code} ]]; then exit_code=1; fi
output_info_message "Exiting script ..."
exit ${exit_code}
}
#--------------------------------------------
#------------------- MAIN -------------------
#--------------------------------------------
set_options "$@"
@anniethiessen
Copy link
Author

anniethiessen commented Oct 15, 2023

TODO:

-implement progress bar
-implement marks

@anniethiessen
Copy link
Author

anniethiessen commented Oct 26, 2023

Change Log:

v1: Initial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment