dotfiles/dot_bashrc.d/executable_09.functions.tmpl

143 lines
3.3 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Bash Functions sourced by .bashrc / .sshrc
#
# Get SSL Certificate information
# Based on https://serverfault.com/questions/661978/displaying-a-remote-ssl-certificate-details-using-cli-tools
function getCertInfo() {
local myServer=${1:?a Servername is required}
local myPort=${2:-443}
echo | openssl s_client -showcerts -servername "$myServer" -connect "$myServer":"$myPort" 2>/dev/null | openssl x509 -inform pem -noout -text
}
#
# Some functions (some by c't magazine)
#
# change directory and list
cdl() {
if cd "$@"; then
ls
fi
}
backup() {
# local @files = "$@"
for f in "$@"
do
if [ -d "$f" ]; then
cp -p -r "$f" "$f".backup-"$(date +%Y-%m-%d_%H_%M_%S)"
else
cp -p "$f" "$f".backup-"$(date +%Y-%m-%d_%H_%M_%S)"
fi
done
}
extract() {
case $1 in
*.tar.bz2) tar xvjf "$1";;
*.tar.gz|*.tgz) tar xvfz "$1";;
*.zip) unzip "$1";;
*) echo "Not supported: $1";;
esac
}
# Misc functions
# convert epoch timestamp to human readable date
epoch2date() {
local string="${1:-0}"
date -d "@$string"
}
# inspired by
# https://unix.stackexchange.com/questions/59501/convert-file-path-to-uri-in-bash
# https://www.unix.com/shell-programming-and-scripting/159863-how-store-files-names-directory-array.html
file2uri() {
local root=${1:-.}
local filter=${2:-*}
if ! python3 --version &> /dev/null
then
echo "python not available!"
return 2
fi
echo "get files for $root"
#for f in "$root/*"; do
# echo "checking $f"
# if [ -f $f ]; then
# echo "is a file: $f"
# python -c "import sys, pathlib; print(pathlib.Path(input()).resolve().as_uri())" <<< "$f"
# fi
#done
# arr_uris=(`ls $root/*.ma4`)
arr=()
for f in $root/*.m4a; do
echo "f: $f"
echo python3 -c "import sys, pathlib; print(pathlib.Path(input()).resolve().as_uri())" <<< "$f"
uri=$(python3 -c "import sys, pathlib; print(pathlib.Path(input()).resolve().as_uri())" <<< "$f")
arr[${#arr[@]}]="$uri"
done
for a in "${arr[@]}"; do
echo "$a"
done
}
enqueueFiles() {
local root="${1:-.}"
if ! rhythmbox-client --version &> /dev/null
then
echo "No rhythmbox-client available!"
return 3
fi
file2uri "$root"
# echo "Array is ${arr[*]}"
for a in "${arr[@]}"; do
echo rhythmbox-client --no-start --enqueue $a
done
}
# List SSH keys
# See https://prefetch.net/blog/index.php/2010/12/13/locating-the-ssh-key-type-and-key-size-from-a-public-key-file/
list-ssh-keys() {
for f in ~/.ssh/*.pub
do
printf "%-50s -> $(ssh-keygen -l -f $f)\n" $f
done
}
{{- if (eq .chezmoi.os "linux") }}
# Show version of a Debian package
debversion() {
local pkgname="${1:?packagename required}"
dpkg -s $pkgname | grep Version
}
# Show files of a Debian package
deblist() {
local pkgname="${1:?packagenamme required}"
dpkg-query -L $pkgname
}
{{- end }}
# Docker helpers
# Update docker-compose containers
# https://stackoverflow.com/questions/49316462/how-to-update-existing-images-with-docker-compose
dcupdate() {
# TODO: check when sudo is needed
docker-compose pull
docker-compose up -d
docker image prune
# TODO: logfile
}
PATH=$PATH:$HOME/bin