diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2023-08-02 15:19:25 +0200 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2023-08-02 15:19:25 +0200 |
commit | 9b791ac8786eed2d242d0909dd2379f008952042 (patch) | |
tree | 403c08eb322e18d045606822911a7eb6036f0070 /bin/common | |
parent | e614a330978ce7e7f6d130cc7d199150e16a409d (diff) | |
parent | 63c64f071ecd45cf66ab49d199a634cd8db27056 (diff) |
Merge branch 'main' of db:dotfiles
Diffstat (limited to 'bin/common')
-rwxr-xr-x | bin/common/aumount | 102 | ||||
-rwxr-xr-x | bin/common/cx | 2 | ||||
-rwxr-xr-x | bin/common/drop_cache | 2 | ||||
-rwxr-xr-x | bin/common/saf | 24 | ||||
-rwxr-xr-x | bin/common/sync-install.sh | 58 | ||||
-rwxr-xr-x | bin/common/toush | 14 | ||||
-rwxr-xr-x | bin/common/wt | 7 | ||||
-rwxr-xr-x | bin/common/y2feed | 15 |
8 files changed, 207 insertions, 17 deletions
diff --git a/bin/common/aumount b/bin/common/aumount new file mode 100755 index 0000000..19b33e0 --- /dev/null +++ b/bin/common/aumount @@ -0,0 +1,102 @@ +#!/bin/sh + +tmp="$(mktemp)" +test "$(id -u)" != "0" && sudo="sudo" + +die () +{ + echo "$@" >&2 +} + +# Read one character +read_char () +{ + die -n ">" + old_stty_cfg=$(stty -g) + stty raw + dd ibs=1 count=${1:-1} 2> /dev/null + stty $old_stty_cfg + die "" +} + +get_dev () +{ + grep "^$1\." "$tmp" | cut -f 2- -d ' ' +} + +# mount the device with $1 as the choice +mount () +{ + dev="$(get_dev "$1")" + test -z "$dev" && exit 1 + + die "Mounting /dev/$dev on /media/$dev" + mkdir -p /media/$dev + $sudo mount /dev/$dev /media/$dev > /dev/null 2>&1 || + return 1 +} + +# umount the device with $1 as the choice +umount () +{ + mountpoint="$(sed -n "${1}p" "$tmp" | + awk '{print $3}')" + test -z "$mountpoint" && exit 1 + + die "Unmounting $mountpoint" + $sudo umount "$mountpoint" || + return 1 +} + +ejekt () +{ + dev="$(get_dev "$1" | sed 's/.$//')" + test -z "$dev" && exit 1 + + die "Ejecting /dev/$dev" + $sudo eject /dev/$dev > /dev/null 2>&1 || + return 1 +} + +# print lsblk, use $1 to print only devices with mountpoints or without +pr_lsblk () +{ + clear + lsblk -o name,size,type,mountpoint + die "───────────────────────────────────" + lsblk --ascii -o name,mountpoint | + grep '^.-' | + while read line + do + words="$(printf "$line" | wc -w)" + test $words -eq ${1:-1} && continue + printf "%s\n" "$line" + done | + cut -f 2- -d "-" | + awk '{print NR ". " $0}' | + tee "$tmp" >&2 +} + +cleanup () +{ + rm -f "$tmp" +} + +trap cleanup EXIT INT + +die "m(ount) u(mount) (e)ject ?" +choice="$(read_char)" + +case $choice in + "m") i=2; cmd=mount ;; + "u") i=1; cmd=umount ;; + "e") i=2; cmd=ejekt ;; + *) exit 1 ;; +esac + +pr_lsblk $i +choice="$(read_char)" +printf "$choice" | grep -q "[0-9]" || exit 1 +$cmd $choice && + die "Successful." || + die "Failed." diff --git a/bin/common/cx b/bin/common/cx new file mode 100755 index 0000000..88fb817 --- /dev/null +++ b/bin/common/cx @@ -0,0 +1,2 @@ +#!/bin/sh +[ -x "$1" ] && chmod -x "$1" || chmod +x "$1" diff --git a/bin/common/drop_cache b/bin/common/drop_cache index 4833597..e4fc039 100755 --- a/bin/common/drop_cache +++ b/bin/common/drop_cache @@ -4,5 +4,5 @@ then echo "Please run as root." exit 1 fi -echo 3 | tee /proc/sys/vm/drop_caches >/dev/null +echo 3 > /proc/sys/vm/drop_caches echo "cache cleared." diff --git a/bin/common/saf b/bin/common/saf new file mode 100755 index 0000000..0c6fbc4 --- /dev/null +++ b/bin/common/saf @@ -0,0 +1,24 @@ +#!/bin/sh + +# Simple Ass Fetch by futxlii + +red="$(printf '\033[31m')" +green="$(printf '\033[32m')" +blue="$(printf '\033[34m')" +reset="$(printf '\033[0m')" + +for file in /etc/os-release /usr/lib/os-release +do + [ -f "$file" ] && . "$file" && break +done + +KERNEL="$(uname -r)" +UPTIME="$(uptime -p)"; UPTIME="${UPTIME##up }" +SHELL="$(basename "$SHELL")" + +cat <<EOF + ${red}- ${blue}${ID:-"unknown"} + ${red}- ${blue}$KERNEL + ${red}- ${blue}$UPTIME + ${red}- ${blue}$SHELL${reset} +EOF diff --git a/bin/common/sync-install.sh b/bin/common/sync-install.sh new file mode 100755 index 0000000..e26c74d --- /dev/null +++ b/bin/common/sync-install.sh @@ -0,0 +1,58 @@ +#!/bin/sh + +die () +{ + echo "$@" >&2 +} + +read_char () +{ + old_stty_cfg=$(stty -g) + stty raw -echo + dd ibs=1 count=1 2> /dev/null + stty $old_stty_cfg +} + +confirm () +{ + printf "$1 " + read_char | grep "[yY]" +} + +usage() +{ + >&2 printf 'Usage: %s <remote> <destination>\n' "${0##*/}" +} + +[ $# -lt 2 ] && usage && exit 1 +REMOTE="$1" +DEST="$2" +SCRIPT="${3:-sync.sh}" + +if ! ssh $REMOTE test -w $DEST 2> /dev/null +then + die "Not a valid remote or destination." + exit 1 +fi + +die "─────────────────────────────────────────────────────────────" +cat <<EOF | tee "$SCRIPT" >&2 +#!/bin/sh + +THISDIR="\$(dirname "\$(readlink -f "\$0")")" +inotifywait -m -e create,modify,delete --format "%f" "\$THISDIR" | +while read FILE +do + rsync -aP "\$THISDIR/" "$REMOTE:$DEST" + sleep 1m +done +EOF +die "─────────────────────────────────────────────────────────────" +die "located at $(readlink -f "$SCRIPT")" + +if confirm "good?" +then + chmod +x "$SCRIPT" +else + rm -f "$SCRIPT" +fi diff --git a/bin/common/toush b/bin/common/toush new file mode 100755 index 0000000..dd675b9 --- /dev/null +++ b/bin/common/toush @@ -0,0 +1,14 @@ +#!/bin/sh +# fork of +# https://codeberg.org/futxlii/bin/toush :) + +shebang='#!/bin/sh' + +usage() { >&2 printf 'Usage: %s' "${0##*/}"; exit 1 ;} + +[ "$1" ] || usage +while [ "$1" ]; do + [ -f "$1" ] && usage + printf '%s\n\n' "$shebang" > "$1" && chmod +x "$1" + shift +done diff --git a/bin/common/wt b/bin/common/wt index 0568b99..d19539b 100755 --- a/bin/common/wt +++ b/bin/common/wt @@ -1,10 +1,7 @@ #!/bin/sh - -echo "$@" > /tmp/truewhile.tmp while true do - sh /tmp/truewhile.tmp - sleep 1 clear + $@ + sleep 1 done -rm /tmp/truewhile.tmp diff --git a/bin/common/y2feed b/bin/common/y2feed index 196ecb7..e30bccf 100755 --- a/bin/common/y2feed +++ b/bin/common/y2feed @@ -1,12 +1,5 @@ #!/bin/sh -ua="$(yt-dlp --dump-user-agent)" - -test -z "${url:=$1}" && - url="$(cat /dev/stdin)" - -url="http://youtube.com/$(echo "$url" | - awk -F '/' '{print $NF}')" -echo "url: $url" >&2 - -curl -L -s -H "$ua" "$url" | - pup 'link[title=RSS] attr{href}' +echo "url: $1" >&2 +curl -L -s "$1" | + pup 'link[title=RSS] attr{href}' | + tee /dev/stderr |