Last active
December 31, 2023 02:26
-
-
Save mrjk/893ed5b403a091721bd07b3e61f70ce8 to your computer and use it in GitHub Desktop.
Bash utils lib
This file contains hidden or 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
# Logging and User interactions | |
# ============== | |
_log () | |
{ | |
local lvl=${1:-DEBUG} | |
shift 1 || true | |
local msg=${*} | |
if [[ "$msg" == '-' ]]; then | |
msg=$(cat - ) | |
fi | |
while read -u 3 line; do | |
>&2 printf '%-8s: %s\n' "$lvl" "${line:- }" | |
done 3<<<"$msg" | |
} | |
_log INFO "Hello world" | |
cat "Hello" | _log DEBUG - | |
_ask_to_continue () | |
{ | |
${RESTRAP_DRY:-false} && return || true | |
local msg="${1}" | |
local waitingforanswer=true | |
while ${waitingforanswer}; do | |
read -r -p "${msg}"$'\n(hit "y/Y" to continue, "n/N" to cancel) ' -n 1 ynanswer | |
case ${ynanswer} in | |
[Yy] ) waitingforanswer=false; break;; | |
[Nn] ) echo ""; echo "Operation cancelled as requested!"; return 1;; | |
* ) echo ""; echo "Please answer either yes (y/Y) or no (n/N).";; | |
esac | |
done | |
echo "" | |
} | |
# Dry mode | |
# ============== | |
_exec () | |
{ | |
local cmd=$@ | |
if ${APP_DRY:-false}; then | |
_log DRY "$cmd" | |
else | |
_log RUN "$cmd" | |
$cmd | |
fi | |
} | |
# Checks | |
# ============== | |
_check_bin () | |
{ | |
local cmds="${*:-}" | |
for cmd in $cmds; do | |
command -v "$1" >&/dev/null || return 1 | |
done | |
} | |
# Coding practices | |
# ============== | |
# Nested whiles | |
nested_whiles () | |
{ | |
local a="1 | |
2 | |
3" | |
local b="4 | |
5 | |
6" | |
while IFS=$' \t\n' read -r n1; do | |
echo "$n1 Chapter" | |
while IFS=$' \t\n' read -r -u 3 n2; do | |
echo " $n1.$n2 Section" | |
done 3<<<"$b" | |
done <<< "$a" | |
} | |
# Log to stderr | |
>&2 echo "My message" | |
# Bash debugging | |
# ============== | |
_dump_vars () | |
{ | |
local prefix=${1:-_} | |
declare -p | grep " .. $prefix" | |
} | |
# Lock functions | |
# ==================== | |
LOCK_FILE='/tmp/.lock_my_script' | |
function wait_lock () | |
{ | |
local tmout=${1:-60} | |
>&2 echo -n "INFO: Waiting for lock (${tmout}s) ..." | |
while ! lock 2>/dev/null ; do | |
if [[ "$tmout" < 1 ]]; then | |
>&2 echo -e "\nERROR: Lock timout expired, could not execute" | |
return 1 | |
fi | |
>&2 echo -n "." | |
sleep 1 | |
tmout=$(( tmout - 1 )) | |
done | |
# Do the last line return | |
>&2 echo " unlocked!" | |
} | |
function lock() { | |
if [[ -f ${LOCK_FILE} ]]; then | |
local curr_proc=$( cat "${LOCK_FILE}" ) | |
if kill -0 $curr_proc >&/dev/null; then | |
>&2 echo "ERROR: ${LOCK_FILE} already exists, please stop pid [$(cat ${LOCK_FILE})] before continue." | |
return 1 | |
else | |
>&2 echo "INFO: emove ${LOCK_FILE} as '$curr_proc' does not exist anymore" | |
rm "${LOCK_FILE}" | |
fi | |
else | |
echo "$$" > ${LOCK_FILE} | |
fi | |
trap unlock EXIT | |
} | |
function unlock() { | |
[[ -f $LOCK_FILE ]] || return 0 | |
>&2 echo "INFO: Release lock file" | |
rm -f $LOCK_FILE | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment