Author: Adam Evyčędo <git@apiote.xyz>
shell style
.config/sh/functions | 246 +++++++++++++++------------------------------
diff --git a/.config/sh/functions b/.config/sh/functions index af70e9bda664e639db3b951a4d7956b450f89372..276fb1be7f0d6b28f1b63d08466ba1716d3fa4ba 100644 --- a/.config/sh/functions +++ b/.config/sh/functions @@ -59,8 +59,7 @@ } toggle_wifi() { state=$(doas connmanctl technologies | grep wifi -A3 | grep Powered | cut -d '=' -f 2 |tr -d ' ') - if [ "$state" = 'False' ] - then + if [ "$state" = 'False' ]; then doas connmanctl enable wifi sleep 5 doas /etc/init.d/connman restart @@ -73,15 +72,13 @@ datediff() { d1=$(date -d "$1" +%s) d2=$(date -d "$2" +%s) - if [ "$d1" -gt "$d2" ] - then + if [ "$d1" -gt "$d2" ]; then d=$(qalc -t -b '10 10' "$d1 - $d2") else d=$(qalc -t -b '10 10' "$d2 - $d1") fi d=$(qalc -t -b '10 10' "round($d / 86400)") - if [ "$d" -eq 1 ] - then + if [ "$d" -eq 1 ]; then printf "%s day\n" "$d" else printf "%s days\n" "$d" @@ -104,8 +101,7 @@ echo "$pid" >/tmp/guak_pid i=0 while [ "$i" -lt 50 ] do - if (swaymsg -t get_tree --raw|grep -q "\"pid\": ${pid},") - then + if (swaymsg -t get_tree --raw|grep -q "\"pid\": ${pid},"); then break fi i=$((i+=1)) @@ -121,8 +117,7 @@ swaymsg -t get_tree --raw| jq '.nodes[].nodes[].floating_nodes[].visible' | grep -q true } guakToggle() { - if guakIsVisible - then + if guakIsVisible; then swaymsg "[pid=${pid}] move scratchpad" else swaymsg "[pid=${pid}] scratchpad show" @@ -131,11 +126,9 @@ } guak() { set -x - if [ -e "/tmp/guak_pid" ] - then + if [ -e "/tmp/guak_pid" ]; then pid=$(cat /tmp/guak_pid) - if ! (ps -eopid | grep -q "$pid") - then + if ! (ps -eopid | grep -q "$pid"); then guakStart else guakToggle @@ -147,8 +140,7 @@ } hex2rgb() { hexinput=$(echo "$1"| tr '[:lower:]' '[:upper:]') - if [ "$(echo "$hexinput" | cut -c1)" = '#' ] - then + if [ "$(echo "$hexinput" | cut -c1)" = '#' ]; then hexinput=$(echo "$hexinput" |cut -c2-) fi @@ -189,8 +181,7 @@ hora=$(echo "$now" | colrm 3) dunatim=$(echo "$now" | colrm 5 | rev | colrm 3 | rev) tim=$(echo "$now" | rev | colrm 3 | rev) - if [ "$1" = "-t" ] - then + if [ "$1" = "-t" ]; then printf "{\"text\": \"%s.%s\"}\n" "$hora" "$dunatim" else printf "{\"text\": \"%s.%s.%s\"}\n" "$hora" "$dunatim" "$tim" @@ -198,11 +189,9 @@ fi } backlight() { - if [ "$1" = '-' ] - then + if [ "$1" = '-' ]; then cmd='-U' - elif [ "$1" = '+' ] - then + elif [ "$1" = '+' ]; then cmd='-A' fi brillo "$cmd" 5 @@ -212,8 +201,7 @@ printf '' > /tmp/wob.sock } streamDwd() { - if [ $# -lt 1 ] - then + if [ $# -lt 1 ]; then min=1800 else min=$(($1 * 60)) @@ -230,14 +218,11 @@ ffmpegQ() { height=$(ffprobe "$*" -show_entries stream=height -of csv=p=0 -v error -select_streams v:0) - if [ "$height" -lt 480 ] - then + if [ "$height" -lt 480 ]; then echo 37 - elif [ "$height" -lt 720 ] - then + elif [ "$height" -lt 720 ]; then echo 35 - elif [ "$height" -lt 1080 ] - then + elif [ "$height" -lt 1080 ]; then echo 34 else echo 33 @@ -259,22 +244,17 @@ #ffmpeg -i "$1" -crf "$Q" -threads 16 -b:v 0 -c:v libaom-av1 -r 25 -strict experimental -tile-columns 2 -pix_fmt yuv420p "${1%%.*}.av1.webm" } volume() { - if command -v pactl >/dev/null 2>&1 - then - if [ "$1" = '+' ] - then + if command -v pactl >/dev/null 2>&1; then + if [ "$1" = '+' ]; then pactl set-sink-volume @DEFAULT_SINK@ +5% - elif [ "$1" = '-' ] - then + elif [ "$1" = '-' ]; then pactl set-sink-volume @DEFAULT_SINK@ -5% - elif [ "$1" = 't' ] - then + elif [ "$1" = 't' ]; then pactl set-sink-mute @DEFAULT_SINK@ toggle fi mute=$(pactl get-sink-mute @DEFAULT_SINK@ | cut -d ' ' -f2) volume=$(pactl get-sink-volume @DEFAULT_SINK@ | head -n1 | cut -d '/' -f2 | tr -d ' %') - if [ "$mute" = 'yes' ] - then + if [ "$mute" = 'yes' ]; then echo '0' >/tmp/wob.sock else echo "$volume" >/tmp/wob.sock @@ -282,8 +262,9 @@ fi printf '' >/tmp/wob.sock else output=$(basename "$(ls -l ~/.config/alsa/asoundrc-default | cut -d '>' -f2 | tr -d ' ')") - if [ "$output" = 'null' ] - then + if [ "$output" = 'null' ]; then + amixer -M sset Master on + amixer -M sset Master 100% cmd='-M sset' ctl='Master' elif [ "$output" = 'asoundrc-bluealsa' ] @@ -294,30 +275,21 @@ else exit 1 fi - if [ "$1" = '+' ] - then - result=$(amixer $cmd "$ctl" 5%+) - elif [ "$1" = '-' ] - then - result=$(amixer $cmd "$ctl" 5%-) - elif [ "$1" = 't' ] - then - # TODO not master; headphones or speaker - result=$(amixer $cmd "$ctl" toggle) - if amixer sget "$ctl" | grep -qF '[off]' - then - result="[0%]" - fi + if [ "$1" = '+' ]; then + result=$(eval "amixer $cmd $ctl 5%+") + elif [ "$1" = '-' ]; then + result=$(eval "amixer $cmd $ctl 5%-") + elif [ "$1" = 't' ]; then + result=$(eval "amixer $cmd $ctl toggle") fi echo "$result" | sed -En 's/.*\[([0-9]+)%\].*/\1/p' | head -n1 > /tmp/wob.sock sleep 1 - printf '' > /tmp/wob.sock + printf '' >/tmp/wob.sock fi } lastmod() { - if [ "$1" = '' ] - then + if [ "$1" = '' ]; then dir="." else dir="$1" @@ -328,22 +300,18 @@ backup() { set -ex - if [ "$1" = 'mymlan' ] - then - if [ "$(id -u)" -eq 0 ] - then + if [ "$1" = 'mymlan' ]; then + if [ "$(id -u)" -eq 0 ]; then printf "don’t run as root" exit 1 fi rsync -rtP --delete /media/usb/adam/H500/Mårran/ /media/usb/adam/Mymlan/ fi - if [ "$1" = 'daily' ] - then + if [ "$1" = 'daily' ]; then case "$(hostname)" in mycroft) - if [ "$(id -nu)" != 'adam' ] - then + if [ "$(id -nu)" != 'adam' ]; then printf "run as adam" exit 1 fi @@ -365,8 +333,7 @@ # git-remotes in ~/Code/dwd tar cf "/keybase/private/bquiet/backup/mycroft/${name}.tar" --exclude dwd ~/Code/ ~/Documents/ ~/Downloads/ ~/Pictures/ ~/Templates/ /etc/runlevels /etc/connman/ /etc/doas.conf /etc/udev /etc/udisks2/ /etc/sysctl.conf /etc/hostname /etc/apk/world ;; DeepThought) - if [ "$(id -nu)" != 'backup' ] - then + if [ "$(id -nu)" != 'backup' ]; then printf "run as backup" exit 1 fi @@ -379,8 +346,7 @@ infraFiles=$(doas -u infra /home/infra/backup "$cmd") wwwFiles=$(doas -u www /var/www/backup "$cmd") - if [ -z "$cmd" ] - then + if [ -z "$cmd" ]; then find /keybase/private/bquiet/backup/deepthought/ -type f -iname 'backup_*.tar' -mtime '+6' -exec rm {} \; #shellcheck disable=SC2086 @@ -391,10 +357,8 @@ esac exit 0 fi - if ! (id -Gn | grep -q backup) - then - # shellcheck disable=SC1112 - printf 'run as a member of group ‘backup’' + if ! (id -Gn | grep -q backup); then + printf "run as a member of group ‘backup’" exit 1 fi @@ -413,8 +377,7 @@ esac } connected() { - if iwctl station list | grep wlan0 | grep -q 'connected' - then + if iwctl station list | grep wlan0 | grep -q 'connected'; then return 0 else return 1 @@ -422,8 +385,7 @@ fi } shouldBackupNow() { - if [ ! -f ~/.cache/lastBackup ] - then + if [ ! -f ~/.cache/lastBackup ]; then return 0 fi @@ -442,8 +404,7 @@ test "$passLastDate" -lt "$eezeLastDate" } network_backup() { - if connected - then + if connected; then shouldPassNow && notify-send -i backup -u critical 'JoeBlack should be run' 'There are changes to passwords since last JoeBlack sync' shouldBackupNow && ( notify-send -i backup 'Running daily backup' @@ -455,8 +416,7 @@ fi ip monitor address | while read -r _ do - if connected - then + if connected; then shouldPassNow && notify-send -i backup -u critical 'JoeBlack should be run' 'There are changes to passwords since last JoeBlack sync' shouldBackupNow && ( notify-send -i backup 'Running daily backup' @@ -478,8 +438,7 @@ bt() { trap bar_bluetooth INT - if [ "$1" = 'on' ] - then + if [ "$1" = 'on' ]; then ( ln -sf ~/.config/alsa/asoundrc-bluealsa ~/.config/alsa/asoundrc-default doas /usr/sbin/rfkill unblock bluetooth @@ -489,8 +448,7 @@ sleep 1 bluetoothctl connect 9E:53:D5:1C:FE:07 amixer -D bluealsa sset "$(amixer -D bluealsa scontrols | grep 'A2DP' | sed -E "s/^[^']*'//" | sed "s/',0//")" 50% >/dev/null ) - elif [ "$1" = 'off' ] - then + elif [ "$1" = 'off' ]; then bluetoothctl power off ln -sf /dev/null ~/.config/alsa/asoundrc-default else @@ -502,8 +460,7 @@ bar_bluetooth } fd() { - if [ "$(git rev-parse --show-toplevel 2>/dev/null)" = '/home/adam' ] - then + if [ "$(git rev-parse --show-toplevel 2>/dev/null)" = '/home/adam' ]; then /usr/bin/fd -I "$@" else /usr/bin/fd "$@" @@ -530,8 +487,7 @@ # shellcheck disable=SC2010 disks=$(ls -l /dev/disk/by-uuid/ | grep -v dm | grep -v nvme0n1 | cut -d '>' -f 2 | tail -n+2 | cut -d '/' -f 3) labels=$(ls -l /dev/disk/by-label/) - if [ -z "$disks" ] - then + if [ -z "$disks" ]; then echo "No disks attached" exit fi @@ -540,22 +496,19 @@ disk_labels=$(for disk in $disks do mountpoint=$(mount | grep "$disk" | cut -d ' ' -f 3- | rev | cut -d ' ' -f 4- | rev) label=$(echo "$labels" | grep "$disk" | awk -F '->' '{print $1}' | tr -s ' ' | cut -d ' ' -f 9) - if [ -z "$label" ] - then + if [ -z "$label" ]; then entry="$disk�$disk" else entry="$disk�$label" fi - if [ "$mountpoint" ] - then + if [ "$mountpoint" ]; then entry="$entry ($mountpoint)" fi echo "$entry" done) result=$(echo "$disk_labels" | fzf -m -d '�' --with-nth 2) - if [ -z "$result" ] - then + if [ -z "$result" ]; then exit fi @@ -563,8 +516,7 @@ label=$(echo "$result" | awk -F '�' '{print $2}') disk=$(echo "$result" | awk -F '�' '{print $1}') path="/dev/$disk" - if mount | grep -q "$path" - then + if mount | grep -q "$path"; then action='unmount' else action='mount' @@ -572,8 +524,7 @@ fi udisksctl "$action" -b "$path" - if [ "$action" = 'unmount' ] && ! [ "$1" = '-s' ] - then + if [ "$action" = 'unmount' ] && ! [ "$1" = '-s' ]; then echo "Ejecting $path" doas eject "$path" fi @@ -687,24 +638,20 @@ done | sort -u) result=$(for app in $apps do - if [ -f "$HOME/.local/share/applications/$app" ] - then + if [ -f "$HOME/.local/share/applications/$app" ]; then path="$HOME/.local/share/applications/$app" else path="/usr/share/applications/$app" fi name=$(grep -E '^Name=' "$path" | head -n1 | cut -d '=' -f 2) commands=$(grep -E '^Name=|^Exec' "$path" | sed 's/ %.//' | cut -d '=' -f2- | awk '(NR-1)%2 == 0 { printf("%s%s", $0, "�") }; (NR-1)%2 == 1 { print }') - if [ -z "$commands" ] - then + if [ -z "$commands" ]; then continue fi - if [ "$(echo "$commands" | wc -l)" -gt 1 ] - then + if [ "$(echo "$commands" | wc -l)" -gt 1 ]; then echo "$commands" | while read -r exec_command do - if [ "$(echo "$exec_command" | sed 's/�.*//')" = "$name" ] - then + if [ "$(echo "$exec_command" | sed 's/�.*//')" = "$name" ]; then echo "$exec_command" else echo "$name, $exec_command" @@ -720,8 +667,7 @@ } view() { # TODO add gemini - if beginswith 'http://' "$1" || beginswith 'https://' "$2" - then + if beginswith 'http://' "$1" || beginswith 'https://' "$2"; then luakit "$1" exit fi @@ -740,8 +686,7 @@ application/pdf) setsid zathura "$1" >/dev/null 2>&1 & ;; application/epub+zip) setsid zathura "$1" >/dev/null 2>&1 & ;; application/zip) unzip -l "$1" ;; application/pgp-keys) - if [ "$charset" = 'us-ascii' ] - then + if [ "$charset" = 'us-ascii' ]; then bat -f "$1" else hexyl "$1" @@ -812,8 +757,7 @@ esac } ws() { - if stat "$HOME/Pictures/wallpapers/$(hostname)_w$1.webp" >/dev/null 2>&1 - then + if stat "$HOME/Pictures/wallpapers/$(hostname)_w$1.webp" >/dev/null 2>&1; then wallpaper="$HOME/Pictures/wallpapers/$(hostname)_w$1.webp" else wallpaper="$HOME/Pictures/wallpapers/$(hostname).webp" @@ -833,29 +777,24 @@ copyright="CC-BY-SA" while [ $# -gt 0 ] do - if [ "$1" = "--author" ] - then + if [ "$1" = "--author" ]; then shift author=$1 fi - if [ "$1" = "--copyright" ] - then + if [ "$1" = "--copyright" ]; then shift copyright=$1 fi - if [ "$1" = "--no-exif" ] - then + if [ "$1" = "--no-exif" ]; then noExif=true fi - if [ "$1" = "--no-rename" ] - then + if [ "$1" = "--no-rename" ]; then noRename=true fi shift done - if [ "$noRename" ] - then + if [ "$noRename" ]; then printf "No renaming\n" else printf "Renaming\n" @@ -870,8 +809,7 @@ mv "$film" "$filmName.mov" done fi - if [ "$noExif" ] - then + if [ "$noExif" ]; then printf 'No exif\n' else printf "Setting exif data: %s, %s\n" "$author" "$copyright" @@ -1111,13 +1049,11 @@ update_git() { set -e pkgs="$1" - if [ "$pkgs" = '' ] - then + if [ "$pkgs" = '' ]; then echo package not specified exit 1 fi - if [ "$pkgs" = 'all' ] - then + if [ "$pkgs" = 'all' ]; then pkgs='libasciidoc glider doh' fi @@ -1249,8 +1185,7 @@ waterInit() { mkdir -p "$HOME/.local/state/water" - if [ ! -f "$HOME/.local/state/water/water.db" ] - then + if [ ! -f "$HOME/.local/state/water/water.db" ]; then sqlite3 "$HOME/.local/state/water/water.db" "create table devices (device_id text, device_type text)" meID=$(uuidgen -r) sqlite3 "$HOME/.local/state/water/water.db" "insert into devices values('$meID', 'me')" @@ -1263,8 +1198,7 @@ waterInit . ~/.config/sh/waterToken - if [ "$1" = 'sync' ] - then + if [ "$1" = 'sync' ]; then meID=$(sqlite3 "$HOME/.local/state/water/water.db" -noheader "select device_id from devices where device_type = 'me'" 2>/dev/null) worldView=$(sqlite3 "$HOME/.local/state/water/water.db" -noheader "select 'h:' || device_id || ':' || max(clock) from intakes group by device_id" 2>/dev/null) @@ -1294,14 +1228,13 @@ ;; esac done - updateLines=$(echo $intakeUpdates | sed 's/ /\n/g') + updateLines=$(echo "$intakeUpdates" | sed 's/ /\n/g') curl -s -XPUT https://monitoring.apiote.xyz/life -H"Authorization: $lifeToken" -d "$updateLines" exit fi - if [ -z "$1" ] - then + if [ -z "$1" ]; then today=$(date '+%Y%m%d') else today="$1" @@ -1313,13 +1246,11 @@ drank() { waterInit - if [ -z "$1" ] || [ -z "$2" ] - then + if [ -z "$1" ] || [ -z "$2" ]; then echo 'usage drank <amount> <drink> [time]' exit 1 fi - if echo "$1" | grep -Eq '^[0-9]+ml$' - then + if echo "$1" | grep -Eq '^[0-9]+ml$'; then amount=$(echo "$1" | grep -Eq '^[0-9]+') elif echo "$1" | grep -Eq '^[0-9.]+$' then @@ -1336,8 +1267,7 @@ Mug) amount=500 ;; bottle) amount=700 ;; esac fi - if [ -z "$3" ] - then + if [ -z "$3" ]; then date=$(date '+%Y%m%d') time=$(date '+%H%M%S') else @@ -1354,13 +1284,11 @@ lock_listen() { inotifywait -m -e delete /dev/ --format '%f' 2>/dev/null | while read -r dev do - if [ "$dev" = 'solokey' ] - then + if [ "$dev" = 'solokey' ]; then serials=$(lsusb -d 0x0483:0xa2ca -v 2>/dev/null | grep Serial | tr -s ' ' | cut -d ' ' -f 4) for serial in $serials do - if [ "$serial" = '208937A0324B' ] || [ "$serial" = '20833771324B' ] - then + if [ "$serial" = '208937A0324B' ] || [ "$serial" = '20833771324B' ]; then continue fi done @@ -1374,11 +1302,9 @@ gemget -o - "$1" | ~/Code/dwd/gmitohtml/gmitohtml -hostname "$1" } render_bar_bluetooth() { - if bluetoothctl info | grep -E '(Connected)' | grep -q 'yes' >/dev/null 2>&1 - then + if bluetoothctl info | grep -E '(Connected)' | grep -q 'yes' >/dev/null 2>&1; then printf "<span color='#00afff'>█</span>" - elif bluetoothctl show | grep Powered | grep -q 'yes' >/dev/null 2>&1 - then + elif bluetoothctl show | grep Powered | grep -q 'yes' >/dev/null 2>&1; then printf "<span color='#00afff'>░</span>" else printf '' @@ -1390,24 +1316,21 @@ bar_bluetooth() { mpv_count=$(cat /tmp/mpv_count 2>/dev/null) mpv_count=${mpv_count:-0} - if [ -e /tmp/swaybar/pipe ] && [ "$mpv_count" -eq 0 ] - then + if [ -e /tmp/swaybar/pipe ] && [ "$mpv_count" -eq 0 ]; then printf "bluetooth|%s\n" "$(render_bar_bluetooth)" >/tmp/swaybar/pipe fi } say() { - if [ -z "$1" ] - then - larynx -m /usr/share/larynx2/en/gb/northern_english_male/en_GB-northern_english_male-medium.onnx + if [ -z "$1" ]; then + larynx -m /usr/share/larynx2/en/us/glados/en-us-glados-high.onnx 2>/dev/null else - echo "$1" | larynx -m /usr/share/larynx2/en/gb/northern_english_male/en_GB-northern_english_male-medium.onnx + echo "$@" | larynx -m /usr/share/larynx2/en/us/glados/en-us-glados-high.onnx 2>/dev/null fi } signImage() { - if [ -f /media/nextcloud/me@cloud.apiote.xyz/Pictutes/IDs/logo/waterSign.svg ] - then + if ! ls /media/nextcloud/me@cloud.apiote.xyz/Pictutes/IDs/logo/waterSign.svg >/dev/null 2>&1; then echo 'water sign not mounted' exit 1 fi @@ -1430,8 +1353,7 @@ brightness=$(echo "scale=2; l($ilumination - $min_il + 1) * 3" | bc -l | cut -d '.' -f1) if [ "$brightness" -gt 100 ] then brightness=100 - elif [ "$brightness" -lt 0 ] - then + elif [ "$brightness" -lt 0 ]; then brightness=0 fi brillo -u 1000000 -S "$brightness"