Skip to content

Instantly share code, notes, and snippets.

@zhum
Last active January 20, 2024 01:44
Show Gist options
  • Save zhum/8347b1b3f2aaae6d6b7b71ba8758872e to your computer and use it in GitHub Desktop.
Save zhum/8347b1b3f2aaae6d6b7b71ba8758872e to your computer and use it in GitHub Desktop.
A library of usefull BASH 4+ functions (many won't work in sh)
# Please, check https://github.com/dylanaraps/pure-bash-bible
# It is a great chest of treasures
######## STRINGS #############
# check if elemet is in the ARRAY
# Usage:
# arr=(a b x)
# containsElement 'x' arr
containsElement () {
local ee=$(shopt -po errexit)
set +e
local e match="$1"
shift
for e; do
if [[ "$e" == "$match" ]]; then
$ee # restore builtin
return 0
fi
done
$ee # restore builtin
return 1
}
# Trim spaces from string head and tail
# Usage:
# trim_string " example string "
trim_string() {
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
# Trim ALL spaces
# shellcheck disable=SC2086,SC2048
# Usage: trim_all " example string "
trim_all() {
set -f
set -- $*
printf '%s\n' "$*"
set +f
}
# Trim quotes from string
# Usage: trim_quotes "string"
trim_quotes() {
: "${1//\'}"
printf '%s\n' "${_//\"}"
}
# Split a string by delimiter
# Usage: split "string" "delimiter" ARRAY_NAME
split(){
IFS="$2" read -ra $3 <<< "$1"
}
######## ARRAYS #############
# Usage: reverse_array "array"
reverse_array() {
shopt -s extdebug
f()(printf '%s\n' "${BASH_ARGV[@]}"); f "$@"
shopt -u extdebug
}
# Usage: remove_array_dups "array"
remove_array_dups() {
declare -A tmp_array
for i in "$@"; do
[[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
done
printf '%s\n' "${!tmp_array[@]}"
}
######## JUST USEFUL #############
# Usage: get_term_size
get_term_size() {
# (:;:) is a micro sleep to ensure the variables are
# exported immediately.
shopt -s checkwinsize; (:;:)
printf '%s\n' "$LINES $COLUMNS"
}
# Sleep without external command (useful for short sleeps)
# read_sleep 0.2
read_sleep() {
read -rt "$1" <> <(:) || :
}
# Usage: hex_to_rgb "#FFFFFF"
# hex_to_rgb "000000"
hex_to_rgb() {
: "${1/\#}"
((r=16#${_:0:2},g=16#${_:2:2},b=16#${_:4:2}))
printf '%s\n' "$r $g $b"
}
# Generate UUID
uuid() {
C="89ab"
for ((N=0;N<16;++N)); do
B="$((RANDOM%256))"
case "$N" in
6) printf '4%x' "$((B%16))" ;;
8) printf '%c%x' "${C:$RANDOM%${#C}:1}" "$((B%16))" ;;
3|5|7|9)
printf '%02x-' "$B"
;;
*)
printf '%02x' "$B"
;;
esac
done
printf '\n'
}
# Print a progress bar
# Usage: bar 1 10 [optional bar symbol, default is '-']
# ^----- Elapsed Percentage (0-100).
# ^-- Total length in chars. (not includes brackets!)
bar() {
local b=${3:--}
((elapsed=$1*$2/100))
# Create the bar with spaces.
printf -v prog "%${elapsed}s"
printf -v total "%$(($2-elapsed))s"
printf '%s' "[${prog// /$b}${total}]"
}
# Prints an integer with suffix (K,M,G,etc)
# does not support fractions like 1500 -> 1.5K, always cuts (1500 -> 1K)
#
with_suffix(){
awk '{ suffix=" KMGT"; for(i=1; $1>1024 && i < length(suffix); i++) $1/=1024; print int($1) substr(suffix, i, 1), $3; }' <<<${1:-0}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment