dotfiles/.local/share/bash/functions.sh

107 lines
2.7 KiB
Bash
Raw Permalink Normal View History

2022-06-01 10:00:25 +02:00
# shellcheck shell=bash
2019-05-28 17:44:30 +02:00
__usage() {
if (($1 < $2)); then
printf 'Usage: %s %s\n' "${FUNCNAME[1]}" "$3"
2023-11-15 18:27:45 +01:00
return 1
2019-05-28 17:44:30 +02:00
fi
2023-11-15 18:27:45 +01:00
return 0
2019-05-28 17:44:30 +02:00
}
__isnumber() {
[[ $1 == ?([+-])+([0-9])?([.,]+([0-9])) ]]
}
count() { # Counts the number of characters in a string
2023-11-15 18:27:45 +01:00
__usage $# 1 '<string>' || return 1
2019-05-28 17:44:30 +02:00
printf '%d\n' "${#1}"
}
2020-07-15 17:51:59 +02:00
trim() { # Trims leading and trailing whitespace
2023-11-15 18:27:45 +01:00
__usage $# 1 '<string>' || return 1
2019-05-28 17:44:30 +02:00
: "${1#"${1%%[![:space:]]*}"}"
: "${_%"${_##*[![:space:]]}"}"
printf '%s\n' "$_"
}
rot13() { # Encodes/Decodes string in rot13
2023-11-15 18:27:45 +01:00
__usage $# 1 '<string>' || return 1
2019-05-28 17:44:30 +02:00
tr 'A-Za-z' 'N-ZA-Mn-za-m' <<< "$1"
}
tempconv() { # Converts Fahrenheit to Celsius and vice versa
2023-11-15 18:27:45 +01:00
__usage $# 2 '<c | f> <degree>' || return 1
2019-05-28 17:44:30 +02:00
if ! __isnumber "$2"; then
printf 'The second argument must be a number.\n' >&2
return 1
fi
case "$1" in
2023-11-15 18:27:45 +01:00
[Ff]) printf '%d\u02DAC\n' "$((5 * ($2 - 32) / 9))"; return ;;
2021-09-16 09:26:36 +02:00
[Cc]) printf '%d\u02DAF\n' "$(($2 * 9 / 5 + 32))"; return ;;
2023-11-15 18:27:45 +01:00
*) __usage 0 1 '<c | f> <degree>' || return 1
2019-05-28 17:44:30 +02:00
esac
}
rgb2hex() { # Converts rgb colour to hex
2023-11-15 18:27:45 +01:00
__usage $# 3 '<red> <green> <blue>' || return 1
2019-05-28 17:44:30 +02:00
printf '#%02x%02x%02x\n' "$1" "$2" "$3"
}
hex2rgb() { # Converts hex colour to rgb
2023-11-15 18:27:45 +01:00
__usage $# 1 '<hex>' || return 1
declare -i r g b
((r=16#${1:0:2},g=16#${1:2:2},b=16#${1:4:2}))
printf '%s %s %s\n' $r $g $b
2019-05-28 17:44:30 +02:00
}
shuffle() { # Shuffles letters in string
2023-11-15 18:27:45 +01:00
__usage $# 1 '<string>' || return 1
2019-05-28 17:44:30 +02:00
perl -MList::Util=shuffle -F'' \
-lane 'print shuffle @F' <<< "$1"
}
2020-07-15 17:51:59 +02:00
weather() { # Shows weather info from wttr.in
2023-11-15 18:27:45 +01:00
__usage $# 1 '<place> [country]' || return 1
curl -fsS4 "v2d.wttr.in/${1}+${2:-Greece}" | head -n -2
2019-05-28 17:44:30 +02:00
}
sri() { # Prints the SRI hash of a resource
2023-11-15 18:27:45 +01:00
__usage $# 1 '<URL> [bits]' || return 1
2019-09-25 20:07:56 +02:00
printf 'sha%d-%s\n' "${2:-384}" \
"$(curl -Ss "$1" | shasum -ba "${2:-384}" - | xxd -r -p | base64)"
2019-05-28 17:44:30 +02:00
}
myip() { # What's my ip
dig +short +tls A myip.opendns.com @resolver1.opendns.com
2019-05-28 17:44:30 +02:00
}
urlencode() { # Encodes string for url
2023-11-15 18:27:45 +01:00
__usage $# 1 '<string>' || return 1
2020-07-15 17:51:59 +02:00
declare LANG=C len=${#1} char i
2019-05-28 17:44:30 +02:00
for ((i = 0; i < len; ++i)); do
char="${1:i:1}"
case $char in
[a-zA-Z0-9.~_-]) printf '%s' "$char" ;;
*) printf '%%%02X' "'$char" ;;
esac
done
}
urldecode() { # Decodes urlencoded string
2023-11-15 18:27:45 +01:00
__usage $# 1 '<string>' || return 1
2019-05-28 17:44:30 +02:00
printf '%b\n' "${*//%/\\x}"
}
utf8decode() { # Decodes =?UTF-8?B?...?= string
2023-11-15 18:27:45 +01:00
__usage $# 1 '<string>' || return 1
2019-09-25 20:07:56 +02:00
[[ $* =~ ^=\?[Uu][Tt][Ff]-8\?[Bb]\?([^?]+)\?=$ ]]
2019-05-28 17:44:30 +02:00
printf '%s\n' "$(base64 -d <<< "${BASH_REMATCH[1]}")"
}
2020-07-15 17:51:59 +02:00
iso2usb() { # Writes an iso to a usb device
2023-11-15 18:27:45 +01:00
__usage $# 2 '<iso> <usb> [bs]' || return 1
2020-07-15 17:51:59 +02:00
sudo dd if="$1" of="$2" status=progress bs="${3:-4M}" oflag=sync
}
2019-11-24 15:50:07 +01:00
# vim:fdm=syntax:fdl=0: