#!/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" }} {{ if (or (eq .chezmoi.osRelease.id "debian" "raspbian") (eq .chezmoi.osRelease.idLike "debian")) }} # 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 } # Show apt installation history debhistory() { zcat /var/log/apt/history.log.*.gz | \ /usr/bin/cat - /var/log/apt/history.log | \ grep -Po '^Commandline:(?= apt)(?=.* install ) \K.*|^Start-Date: \K.*' | \ grep -B1 "^apt" | \ grep -v -- "^--" | \ paste -d " " - - | \ sort } {{ end }} {{ end }} # Run 'top' by a list of processes matching name. topby() { local name=${1:?name required} local pidlist="$(pidof $name | sed 's/ /,/g')" top -p $pidlist } # 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 } # Python virtualenv helper function activate-venv() { local venv=${1:-noneselected} local ENV_DIR=$HOME/.virtual_envs if [[ -d "$ENV_DIR/$venv" ]]; then echo -e "\e[32msetting virtualenv to $ENV_DIR/$venv \e[0m" source "$ENV_DIR/$venv/bin/activate" else echo -e "\e[33mvirtualenv does not exist\e[0m" echo -e "Current virtualenvs available: \e[32m" ls "$ENV_DIR" echo -e "\e[0m" fi } function create-venv() { local venv=$1 local ENV_DIR=$HOME/.virtual_envs if [[ "$venv" =~ ^[a-zA-Z0-9_-]+$ ]]; then python3 -m venv "$ENV_DIR/$venv" echo -e "\e[32mvirtualenv created: $ENV_DIR/$venv \e[0m" else echo -e "\e[32minvalid name: $venv \e[0m" fi } PATH=$PATH:$HOME/bin