Last active
January 12, 2022 02:25
-
-
Save gronono/cbccbefdd93a3618ad400eb83cb50c4a to your computer and use it in GitHub Desktop.
Bash usefull tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# Script: example.sh | |
# Author: Example Author <autor@example.com> | |
# Description: | |
# This script is an example for writing good bash script. | |
# Source: Gronono's Github Gist (https://gist.github.com/gronono/cbccbefdd93a3618ad400eb83cb50c4a) | |
# Changelog: | |
# YYYY-MM-DD: some modifications | |
# 2022-01-11: Create this example. | |
# | |
# | |
# Edit this function to change script configuration | |
# | |
function configure() { | |
# Change the log level | |
LOG_LEVEL=${LOG_LEVEL_DEBUG} | |
} | |
# | |
# Begin of the script | |
# ! DO NOT EDIT BELOW THIS LINE ! | |
# | |
# | |
# Main function | |
# | |
function main() { | |
debug "Log some debug" | |
info "Log some information: ${SCRIPT_DIRECTORY}" | |
warn "Log some warning" | |
error "Log some error" | |
test_command_exists echo | |
try echo "Let's try something" | |
fatal "Fatal error" | |
exit 0 | |
} | |
# | |
# Utilities | |
# see https://gist.github.com/gronono/cbccbefdd93a3618ad400eb83cb50c4a | |
# | |
# Directory containing this file | |
# https://stackoverflow.com/a/1482133/2909535 | |
SCRIPT_DIRECTORY=$(dirname "$(readlink -f "$0")") | |
# Some colors | |
NO_COLOR='\033[0m' | |
COLOR_RED='\033[0;31m' | |
COLOR_GREEN='\033[0;32m' | |
COLOR_YELLOW='\033[0;33m' | |
COLOR_GRAY='\033[0;90m' | |
# Log levels | |
LOG_LEVELS="debug info warn error" | |
# Variables LOG_COLOR are used dynamically | |
# shellcheck disable=SC2034 | |
LOG_COLOR_DEBUG=${COLOR_GRAY} | |
# shellcheck disable=SC2034 | |
LOG_COLOR_INFO=${NO_COLOR} | |
# shellcheck disable=SC2034 | |
LOG_COLOR_WARN=${COLOR_YELLOW} | |
# shellcheck disable=SC2034 | |
LOG_COLOR_ERROR=${COLOR_RED} | |
# Default log level before configure called | |
LOG_LEVEL="info" | |
# Display a message with some color | |
echo_with_color() ( | |
color=$1 | |
shift 1 | |
printf "%s$*${NO_COLOR}\n" "${color}" | |
) | |
# | |
# Strings | |
# | |
to_uppercase() ( | |
echo "$1" | tr '[:lower:]' '[:upper:]' | |
) | |
# | |
# Arrays | |
# | |
index_of() ( | |
index=0 | |
found=-1 | |
for i in $1; do | |
if [ "$i" = "$2" ]; then | |
found=$index; | |
break; | |
fi | |
index=$((index+1)) | |
done | |
echo $found | |
) | |
# | |
# Logger | |
# | |
is_log_level_enabled() ( | |
actual_log_level_idx=$(index_of "$LOG_LEVELS" $LOG_LEVEL) | |
message_log_level_idx=$(index_of "$LOG_LEVELS" "$1") | |
[ "$message_log_level_idx" -ge "$actual_log_level_idx" ] | |
) | |
log() ( | |
this_log_level=$1 | |
shift 1 | |
is_log_level_enabled "${this_log_level}" | |
enabled=$? | |
if [ $enabled -eq 0 ]; then | |
var_log_color=$(to_uppercase "LOG_COLOR_$this_log_level" | tr '[:lower:]' '[:upper:]') | |
log_color=$(eval "echo \$${var_log_color}") | |
echo_with_color "${log_color}" "$@" | |
fi | |
) | |
# Log a message in debug mode | |
debug() ( log "debug" "$@"; ) | |
# Log a message in info mode | |
info() ( log "info" "$@"; ) | |
# Log a message in warning mode | |
warn() ( log "warn" "$@"; ) | |
# Log a message in error mode (error are also written in stderr) | |
error() ( log "error" "$@" >&2; ) | |
# Log a success message | |
success() ( echo_with_color "${COLOR_GREEN}" "$@"; ) | |
# Log an error and exit the script | |
die() ( error "$*"; exit 1; ) | |
# If the command failed, log the command and exit | |
try() ( "$@" || die "Command '$*' failed!"; ) | |
# Test if a command exists or die | |
test_command_exists() ( command -v "$1" > /dev/null || die "Command '$1' not found!"; ) | |
# | |
# Run script's instructions | |
# | |
configure | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment