From 04e890db1b659b62ba17e0252917a05b40e54cf3 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Thu, 29 Jul 2021 05:44:12 -0700 Subject: [PATCH] first pass over sh: improve compatibility, fix stuff --- sh/arith | 8 ++++- sh/arrays | 2 ++ sh/aur | 2 ++ sh/autosync | 4 +++ sh/bak | 6 ++-- sh/baks | 5 ++- sh/bin | 4 ++- sh/cdbusiest | 2 +- sh/colors | 2 ++ sh/compandy | 2 +- sh/compile | 87 +++++++++++++++++++++++++++------------------------ sh/confirm | 2 ++ sh/countdiff | 3 ++ sh/cutv | 2 ++ sh/days | 4 ++- sh/dbusiest | 2 ++ sh/dfu | 2 ++ sh/disf | 6 +++- sh/e | 2 ++ sh/ea | 4 ++- sh/echo2 | 2 ++ sh/explore | 3 ++ sh/ff | 2 ++ sh/has | 2 ++ sh/hex | 5 +++ sh/ify | 2 ++ sh/is_empty | 6 ++-- sh/isup | 4 +++ sh/maybesudo | 6 +++- sh/minutemaid | 27 ++++++++-------- sh/noccom | 4 +++ sh/note | 2 ++ sh/now | 10 ++++-- sh/pacbm | 2 ++ sh/pause | 8 +++-- sh/pegg | 4 +++ sh/pippy | 2 +- sh/pre | 4 ++- sh/psbm | 3 ++ sh/randir | 4 ++- sh/refresh | 2 ++ sh/sc | 6 ++-- sh/scramble | 5 ++- sh/scropt | 10 ++++-- sh/similar | 3 ++ sh/slit | 11 +++++-- sh/slitt | 15 ++++++--- sh/sum | 2 ++ sh/sv | 5 ++- sh/tpad | 9 +++--- sh/trash | 2 ++ sh/trunc | 5 ++- sh/unscreen | 2 ++ sh/wat | 2 ++ sh/wipe | 14 ++++++--- 55 files changed, 246 insertions(+), 100 deletions(-) diff --git a/sh/arith b/sh/arith index 523981b..340a8ba 100755 --- a/sh/arith +++ b/sh/arith @@ -1,8 +1,14 @@ #!/usr/bin/env zsh # YES_ZSH +# YES_BASH +# YES_DASH + +# though technically compatible with other shells, +# extra functionality is through zsh's extended arithmetic functions. arith() { - <<<"$(($@))" +# <<<"$(($@))" + printf "%s\n" "$(($@))" } [ "${SOURCING:-0}" -gt 0 ] || arith "$@" diff --git a/sh/arrays b/sh/arrays index 5d5a7bb..fc62902 100644 --- a/sh/arrays +++ b/sh/arrays @@ -1,5 +1,7 @@ #!/usr/bin/env false # YES_ZSH +# YES_BASH +# NO_DASH whoa=( -Wall diff --git a/sh/aur b/sh/aur index 89fa85f..f3803ff 100755 --- a/sh/aur +++ b/sh/aur @@ -2,6 +2,8 @@ # http://aur.sh with massive feature creep # YES_ZSH +# TODO: check compatibility with bash and dash. + aur() { trap 'exit 1' SIGINT SIGTERM diff --git a/sh/autosync b/sh/autosync index e3b1f6b..d6c8fd0 100755 --- a/sh/autosync +++ b/sh/autosync @@ -1,5 +1,9 @@ #!/usr/bin/env zsh # YES_ZSH +# NO_BASH +# NO_DASH + +# TODO: portable way of dodging aliases to allow other shell support? autosync() { target="$1" diff --git a/sh/bak b/sh/bak index c7e0005..c5e257f 100644 --- a/sh/bak +++ b/sh/bak @@ -1,14 +1,16 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH bak() { [ $# -gt 0 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } local ret=0 for f; do - if [ -s "$f.bak" ]; then + if [ -e "$f.bak" ]; then bak "$f.bak" || ret=1 fi - cp -p "$f" "$f.bak" + cp -p "$f" "$f.bak" || ret=1 done return $ret } diff --git a/sh/baks b/sh/baks index c28400f..baa9987 100644 --- a/sh/baks +++ b/sh/baks @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH #SOURCING=$((SOURCING+1)) #. ~/sh/note # FIXME: don't do this? @@ -11,7 +13,8 @@ baks() { # backup, timestamped local now="$(date -u '+%y-%m-%d_%H-%M-%S')" local bak="$fp.$now.bak" if [ -s "$bak" ]; then - note "how in the hell?" "$bak" "already exists" + #note "how in the hell?" "$bak" "already exists" + printf "%s %s %s\n" "how in the hell?" "$bak" "already exists" ret=1 fi cp -p "$fp" "$bak" || ret=1 diff --git a/sh/bin b/sh/bin index bb8cfe4..24f24b8 100644 --- a/sh/bin +++ b/sh/bin @@ -1,9 +1,11 @@ #!/usr/bin/env zsh # YES_ZSH +# YES_BASH +# YES_DASH bin() { local a="$(($@))" - let 'a=(((((((((((((a + 0x0FFFFF80) & 0x1000007F) + 0x00FFFFC0) & 0x1100003F) + 0x000FFFE0) & 0x1110001F) + 0x0000FFF0) & 0x1111000F) + 0x00000FF8) & 0x11111007) + 0x000000FC) & 0x11111103) + 0x0000000E) & 0x11111111' + a="$(((((((((((((((a + 0x0FFFFF80) & 0x1000007F) + 0x00FFFFC0) & 0x1100003F) + 0x000FFFE0) & 0x1110001F) + 0x0000FFF0) & 0x1111000F) + 0x00000FF8) & 0x11111007) + 0x000000FC) & 0x11111103) + 0x0000000E) & 0x11111111))" printf "%08X\n" "$a" } diff --git a/sh/cdbusiest b/sh/cdbusiest index 2ccf324..484a05b 100755 --- a/sh/cdbusiest +++ b/sh/cdbusiest @@ -4,7 +4,7 @@ cdbusiest() { dbusiest | read -r c d [ -z $c ] && return 1 - <<<"$c $d" + printf "%s\n" "$c $d" cd $d } diff --git a/sh/colors b/sh/colors index c57219f..10e94d7 100755 --- a/sh/colors +++ b/sh/colors @@ -1,5 +1,7 @@ #!/usr/bin/env bash # YES_ZSH +# YES_BASH +# NO_DASH colors() { printf "\e[%dm$(printf " \e[%dm mV \e[40m" {4,10}{0..7})\e[0m\n" {3,9}{0..7} diff --git a/sh/compandy b/sh/compandy index b1d5e78..9a4aa75 100755 --- a/sh/compandy +++ b/sh/compandy @@ -1,6 +1,6 @@ #!/usr/bin/zsh -# YES_ZSH # script to make sense of compand's parameters +# YES_ZSH compandy() { if [ $# -eq 0 ]; then diff --git a/sh/compile b/sh/compile index 9af05e1..69477c6 100755 --- a/sh/compile +++ b/sh/compile @@ -1,5 +1,7 @@ #!/usr/bin/env zsh # YES_ZSH +# NO_BASH +# NO_DASH # I'll just leave this here... __setup_clang_ubuntu() { @@ -54,51 +56,54 @@ compile() { local winkit printf "%s\n" "/c/Program Files (x86)/Windows Kits/"*(on/N[1]) | read -r winkit - [ -n "$winkit" ] || { echo "failed glob; missing winkit" >&2; return 1 } - printf "%s\n" "$winkit/Lib/"*(On/N[1]) | read -r winkit - [ -n "$winkit" ] || { echo "failed glob; missing winkit" >&2; return 1 } + [ -z "$winkit" ] || printf "%s\n" "$winkit/Lib/"*(On/N[1]) | read -r winkit - # detect MSVC. - local clarch - local arch - local msvc_dig_deep - [ "$MSYSTEM" = MINGW64 ] && clarch="/amd64" || clarch="" - [ "$MSYSTEM" = MINGW64 ] && arch="x64" || arch="x86" - if [ -d "/c/Program Files (x86)/Microsoft Visual Studio" ]; then # 2017+ - printf "%s\n" "/c/Program Files (x86)/Microsoft Visual Studio/20"*(On/N[1]) | read vc - printf "%s\n" "$vc"/*/VC | read vc - printf "%s\n" "$vc/Tools/MSVC/"*(On/N[1]) | read vc - msvc_dig_deep="yes" - else # older versions - printf "%s\n" "/c/Program Files (x86)/Microsoft Visual Studio "*(On/N[1]) | read vc - vc="$vc/VC" - fi + if [ -z "$winkit" ]; then + #echo "failed glob; missing winkit" >&2 + : + else + # detect MSVC. + local clarch + local arch + local msvc_dig_deep + [ "$MSYSTEM" = MINGW64 ] && clarch="/amd64" || clarch="" + [ "$MSYSTEM" = MINGW64 ] && arch="x64" || arch="x86" + if [ -d "/c/Program Files (x86)/Microsoft Visual Studio" ]; then # 2017+ + printf "%s\n" "/c/Program Files (x86)/Microsoft Visual Studio/20"*(On/N[1]) | read vc + printf "%s\n" "$vc"/*/VC | read vc + printf "%s\n" "$vc/Tools/MSVC/"*(On/N[1]) | read vc + msvc_dig_deep="yes" + else # older versions + printf "%s\n" "/c/Program Files (x86)/Microsoft Visual Studio "*(On/N[1]) | read vc + vc="$vc/VC" + fi - # setup MSVC. - if [ -n "$msvc_dig_deep" ] && [ -e "$vc/bin/Host$arch/$arch/cl" ]; then - cl="$vc/bin/Host$arch/$arch/cl" - export PATH="$PATH:$vc/bin/Host$arch/$arch" - export LIB="$(cygpath -w "$vc/lib/$arch")" - export LIBPATH="$(cygpath -w "$vc/lib/$arch")" - elif [ -d "$vc/bin$clarch" ] && [ -e "$vc/bin$clarch/$cl" ]; then - cl="$vc/bin$clarch/cl" - export PATH="$PATH:$vc/bin$clarch" - export LIB="$(cygpath -w "$vc/LIB$clarch")" - export LIBPATH="$(cygpath -w "$vc/LIB$clarch")" - fi + # setup MSVC. + if [ -n "$msvc_dig_deep" ] && [ -e "$vc/bin/Host$arch/$arch/cl" ]; then + cl="$vc/bin/Host$arch/$arch/cl" + export PATH="$PATH:$vc/bin/Host$arch/$arch" + export LIB="$(cygpath -w "$vc/lib/$arch")" + export LIBPATH="$(cygpath -w "$vc/lib/$arch")" + elif [ -d "$vc/bin$clarch" ] && [ -e "$vc/bin$clarch/$cl" ]; then + cl="$vc/bin$clarch/cl" + export PATH="$PATH:$vc/bin$clarch" + export LIB="$(cygpath -w "$vc/LIB$clarch")" + export LIBPATH="$(cygpath -w "$vc/LIB$clarch")" + fi - # finish up. - if [ -n "$cl" ]; then - export INCLUDE="$(cygpath -w "$vc/INCLUDE")" - export INCLUDE="$INCLUDE;$(cygpath -w "${winkit/Lib/Include}/ucrt")" - export LIB="$LIB;$(cygpath -w "$winkit/um/$arch")" - export LIB="$LIB;$(cygpath -w "$winkit/ucrt/$arch")" + # finish up. + if [ -n "$cl" ]; then + export INCLUDE="$(cygpath -w "$vc/INCLUDE")" + export INCLUDE="$INCLUDE;$(cygpath -w "${winkit/Lib/Include}/ucrt")" + export LIB="$LIB;$(cygpath -w "$winkit/um/$arch")" + export LIB="$LIB;$(cygpath -w "$winkit/ucrt/$arch")" - for d in "${(@s/;/)INCLUDE}"; do - clang_flags+=(-I"$d") - done - # ignore MSVC's non-standard deprecation warnings. - clang_flags+=(-D_CRT_SECURE_NO_WARNINGS) + for d in "${(@s/;/)INCLUDE}"; do + clang_flags+=(-I"$d") + done + # ignore MSVC's non-standard deprecation warnings. + clang_flags+=(-D_CRT_SECURE_NO_WARNINGS) + fi fi fi diff --git a/sh/confirm b/sh/confirm index 4b3267d..d3d6513 100755 --- a/sh/confirm +++ b/sh/confirm @@ -1,5 +1,7 @@ #!/usr/bin/env bash # YES_ZSH +# YES_BASH +# NO_DASH if [ -n "${ZSH_VERSION:-}" ]; then confirm() { diff --git a/sh/countdiff b/sh/countdiff index d7402dd..a6fc670 100644 --- a/sh/countdiff +++ b/sh/countdiff @@ -1,7 +1,10 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH countdiff() { + [ $# -gt 1 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } [ $# -le 2 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } git --no-pager diff --stat --no-color --no-index "$1" "$2" \ | awk '/changed/{print $4+$6;a=1}END{if(!a)print 0}' diff --git a/sh/cutv b/sh/cutv index 4e62f17..5a2200c 100644 --- a/sh/cutv +++ b/sh/cutv @@ -1,5 +1,7 @@ #!/usr/bin/env zsh # YES_ZSH +# NO_BASH +# NO_DASH cutv() { [ $# -le 4 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } diff --git a/sh/days b/sh/days index 3918aec..268dac7 100755 --- a/sh/days +++ b/sh/days @@ -1,5 +1,7 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH days() { [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } diff --git a/sh/dbusiest b/sh/dbusiest index 87ea4c0..5f745b6 100755 --- a/sh/dbusiest +++ b/sh/dbusiest @@ -1,5 +1,7 @@ #!/usr/bin/env zsh # YES_ZSH +# NO_BASH +# NO_DASH dbusiest() { [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } diff --git a/sh/dfu b/sh/dfu index 7f75d36..250be7a 100755 --- a/sh/dfu +++ b/sh/dfu @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH dfu() { [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } diff --git a/sh/disf b/sh/disf index ed469fa..c0daae9 100755 --- a/sh/disf +++ b/sh/disf @@ -1,6 +1,10 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # disassembles a function given its symbolic name. # YES_ZSH +# YES_BASH +# YES_DASH + +# TODO: actually test that this works with dash. function disf() { [ $# -le 2 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } diff --git a/sh/e b/sh/e index 5a6f2cc..946808d 100755 --- a/sh/e +++ b/sh/e @@ -1,5 +1,7 @@ #!/usr/bin/env zsh # YES_ZSH +# NO_BASH +# NO_DASH e() { local editor=(${=EDITOR}) diff --git a/sh/ea b/sh/ea index 6033d91..be94d53 100644 --- a/sh/ea +++ b/sh/ea @@ -1,6 +1,8 @@ -#!/usr/bin/env bash +#!/usr/bin/env false # remote file access (http file-sharing shenanigans) # YES_ZSH +# YES_BASH +# YES_DASH _REMOTE_DOMAIN="https://eaguru.guru" _REMOTE_DIR="t" diff --git a/sh/echo2 b/sh/echo2 index 20c33b7..7e2adc0 100644 --- a/sh/echo2 +++ b/sh/echo2 @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH echo2() { local IFS=" " diff --git a/sh/explore b/sh/explore index 47e70df..bec1421 100644 --- a/sh/explore +++ b/sh/explore @@ -1,7 +1,10 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH explore() { + [ -n "$MSYSTEM" ] || { printf "%s\n" "$0: only for MSYS2" >&2; return 1; } [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } explorer "$(cygpath -w "${1:-.}")" } diff --git a/sh/ff b/sh/ff index c9de645..9c4fc53 100644 --- a/sh/ff +++ b/sh/ff @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH ff() { [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } diff --git a/sh/has b/sh/has index 7ab5694..cbdabe4 100644 --- a/sh/has +++ b/sh/has @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH has() { if [ -n "$ZSH_VERSION" ]; then diff --git a/sh/hex b/sh/hex index a9b3a16..8e5cff2 100755 --- a/sh/hex +++ b/sh/hex @@ -1,5 +1,10 @@ #!/usr/bin/env zsh # YES_ZSH +# YES_BASH +# YES_DASH + +# though technically compatible with other shells, +# extra functionality is through zsh's extended arithmetic functions. hex() { printf "%08X\n" "$(($@))" diff --git a/sh/ify b/sh/ify index 16cf9e9..ffc3783 100644 --- a/sh/ify +++ b/sh/ify @@ -1,5 +1,7 @@ #!/usr/bin/sh # YES_ZSH +# YES_BASH +# YES_DASH ify() { [ $# -ge 2 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } diff --git a/sh/is_empty b/sh/is_empty index 53bde61..ea9407e 100755 --- a/sh/is_empty +++ b/sh/is_empty @@ -1,9 +1,11 @@ #!/usr/bin/env bash # YES_ZSH +# YES_BASH +# NO_DASH is_empty() { - while read -r; do - [ ! -d "$REPLY" ] && [ -s "$REPLY" ] && return 1 + while read -r f; do + [ ! -d "$f" ] && [ -s "$f" ] && return 1 done < <(find ${1:-.}) # TODO: done < <(find ${1:-.} -type f -print -quit) # this needs to skip empty files as well. diff --git a/sh/isup b/sh/isup index c6bab2e..df73677 100755 --- a/sh/isup +++ b/sh/isup @@ -1,5 +1,9 @@ #!/usr/bin/zsh # YES_ZSH +# NO_BASH +# NO_DASH + +# TODO: damned substrings. rewrite to be semi-portable. isup() { local c diff --git a/sh/maybesudo b/sh/maybesudo index 4d2a4c4..c36d711 100644 --- a/sh/maybesudo +++ b/sh/maybesudo @@ -1,10 +1,12 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH maybesudo_() { local name local env_cleanup=0 - while getopts ':AEHKPSVbhiklnsvC:U:g:p:r:t:u:' name; do + while getopts :AEHKPSVbhiklnsvC:U:g:p:r:t:u: name; do case $name in K|V|k) # K: sure kill @@ -122,6 +124,8 @@ maybesudo_() { else # run it in a subshell so it won't affect ours. + # TODO: run through env anyway for consistency. + # (`maybesudo export` should fail) ( "$@"; ) fi diff --git a/sh/minutemaid b/sh/minutemaid index 76588c6..00a9cf5 100755 --- a/sh/minutemaid +++ b/sh/minutemaid @@ -1,17 +1,19 @@ -#!/usr/bin/env zsh +#!/usr/bin/env dash # crontab usage: -#* * * * * minutemaid 9 cd repo && git pull # runs every nine minutes +#* * * * * minutemaid 9 ~/work/do_my_bidding # runs every nine minutes # YES_ZSH +# YES_BASH +# YES_DASH minutemaid() { - local offset=0 opt= - while getopts 'o:h' opt; do - case $opt in + local offset=0 name + while getopts 'o:h' name; do + case $name in o) offset="$OPTARG";; ?) local fd=0 - [ $opt = h ] && fd=0 || fd=2 - echo -E "usage: $0 [-o offset] {interval} [{command} [{args...}]]" >&$fd - [ $opt = h ] && return 0 || return 1;; + [ $name = h ] && fd=1 || fd=2 + printf "%s\n" "usage: $0 [-o offset] {interval} [{command} [{args...}]]" >&$fd + [ $name = h ] && return 0 || return 1;; esac done shift $((OPTIND-1)) @@ -20,13 +22,10 @@ minutemaid() { shift local sec="$(date +%s)" - let min=sec/60+offset - let mod=min%interval + local min="$((sec/60+offset))" + local mod="$((min%interval))" if [ $# -gt 0 ]; then - local cmd="${1}" - shift - - [ $mod -eq 0 ] && "$cmd" "$@" + [ $mod -ne 0 ] || "$@" else [ $mod -eq 0 ] && return 0 || return 1 fi diff --git a/sh/noccom b/sh/noccom index 5f1db86..70f49cc 100644 --- a/sh/noccom +++ b/sh/noccom @@ -2,6 +2,10 @@ # the first expression is taken from this FAQ: # https://perldoc.perl.org/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file%3f # YES_ZSH +# YES_BASH +# YES_DASH + +# TODO: what's the minimum version of perl required for this? noccom() { [ -s ~/opt/local/bin/noccom ] || cat > ~/opt/local/bin/noccom <&2; return 1; } @@ -32,8 +36,8 @@ now() { M=${M#0*} S=${S#0*} - local ms= - let 'ms=(H*60*60+M*60+S)*1000+N/1000000' + local ms + ms=$(((H*60*60+M*60+S)*1000+N/1000000)) ms=$(printf '%08i' $ms) echo "${F}_${ms}" } diff --git a/sh/pacbm b/sh/pacbm index a65fedf..b0f53f6 100755 --- a/sh/pacbm +++ b/sh/pacbm @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH pacbm() { expac -s '%m %n' | awk ' diff --git a/sh/pause b/sh/pause index 78c5e4c..8412604 100755 --- a/sh/pause +++ b/sh/pause @@ -1,11 +1,13 @@ #!/usr/bin/env bash # YES_ZSH +# YES_BASH +# NO_DASH pause() { [ -n "${ZSH_VERSION:-}" ] \ - && read -sk '?Press any key to continue - ' || read -n1 -u 1 -sp 'Press any key to continue - ' + && read -sk '?Press any key to continue +' || read -n1 -u 1 -sp 'Press any key to continue +' } [ "${SOURCING:-0}" -gt 0 ] || pause "$@" diff --git a/sh/pegg b/sh/pegg index d720dc5..2dc14f2 100644 --- a/sh/pegg +++ b/sh/pegg @@ -1,5 +1,9 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH + +# TODO: source pippy here? this may affect dash compatibility. pegg() { [ $# -ge 2 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } diff --git a/sh/pippy b/sh/pippy index e3e9a75..0ec17eb 100644 --- a/sh/pippy +++ b/sh/pippy @@ -1,4 +1,4 @@ -#!/usr/bin/env +#!/usr/bin/env sh # YES_ZSH #. ~/sh/maybesudo diff --git a/sh/pre b/sh/pre index 9c09a59..96c779c 100755 --- a/sh/pre +++ b/sh/pre @@ -1,5 +1,7 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH pre() { if [ -n "${ZSH_VERSION:-}" ]; then diff --git a/sh/psbm b/sh/psbm index 80300cd..d4ff13b 100755 --- a/sh/psbm +++ b/sh/psbm @@ -1,7 +1,10 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH psbm() { + [ -z "$MSYSTEM" ] || { printf "%s\n" "$0: unsupported on MSYS2" >&2; return 1; } ps axco rss,pid,command | awk ' NR>1&&$1>0{t+=$1;printf("%8.2fM %7d %s\n",$1/1024,$2,$3)} END{printf("%8.2fM\n",t/1024)} diff --git a/sh/randir b/sh/randir index 3e42104..deb4cf7 100755 --- a/sh/randir +++ b/sh/randir @@ -1,8 +1,10 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH randir() { - find -maxdepth 1 -type d \( -path '/root' -prune -o -print \) | tail -n+2 | shuf | head -1 + find -maxdepth 1 -type d \( -path '/root' -prune -o -print \) | tail -n+2 | shuf | head -n1 } [ "${SOURCING:-0}" -gt 0 ] || randir "$@" diff --git a/sh/refresh b/sh/refresh index ef66f9b..f158f9d 100644 --- a/sh/refresh +++ b/sh/refresh @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH refresh() { hash -r diff --git a/sh/sc b/sh/sc index 5acd3ce..9ae541f 100755 --- a/sh/sc +++ b/sh/sc @@ -1,6 +1,8 @@ #!/usr/bin/env bash -. ~/.ea # FIXME: don't do this? somehow? +SOURCING=$((SOURCING+1)) +. ~/sh/ea # FIXME: don't do this? somehow? +SOURCING=$((SOURCING-1)) sc_shorten() { REPLY="${1:2:2}${1:5:2}${1:8:2}${1:11:12}" @@ -59,4 +61,4 @@ sc() { return 0 } -sc "$@" +[ "${SOURCING:-0}" -gt 0 ] || sc "$@" diff --git a/sh/scramble b/sh/scramble index 674f1cf..bc721a3 100755 --- a/sh/scramble +++ b/sh/scramble @@ -1,7 +1,10 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH scramble() { + [ $# -eq 0 ] || { printf "%s\n" "$0: does not take arguments" >&2; return 1; } local eggs='s/@\(\w\)\(\w\)\(\w\)/@\3\1@\2/g' sed \ -e 's/\b\w/&@/g' \ diff --git a/sh/scropt b/sh/scropt index 9b5bbc4..4ebdd4e 100755 --- a/sh/scropt +++ b/sh/scropt @@ -1,12 +1,16 @@ #!/usr/bin/env bash # YES_ZSH +#SOURCING=$((SOURCING+1)) +#. ~/sh/now # FIXME: don't do this? +#SOURCING=$((SOURCING-1)) + scropt() { - local now= - which ~/sh/now 2>/dev/null >/dev/null && now=$(~/sh/now) || now=$(date +%F_%T) + local now=$(now) + [ $? -eq 0 ] || return $? local fn="$HOME/play/$now.png" scrot "$fn" -e "optipng -quiet \$f" "$@" - echo "$fn" + [ $? -eq 0 ] && echo "$fn" || return $? } [ "${SOURCING:-0}" -gt 0 ] || scropt "$@" diff --git a/sh/similar b/sh/similar index 7a78368..41c3b21 100755 --- a/sh/similar +++ b/sh/similar @@ -1,7 +1,10 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH similar() { + [ $# -eq 0 ] || { printf "%s\n" "$0: does not take arguments" >&2; return 1; } awk ' { i=0 diff --git a/sh/slit b/sh/slit index d4a6e80..f39001e 100755 --- a/sh/slit +++ b/sh/slit @@ -1,9 +1,14 @@ -#!/usr/bin/env zsh -# from https://github.com/sorin-ionescu/prezto/ +#!/usr/bin/env dash # YES_ZSH +# YES_BASH +# YES_DASH slit() { - awk "{ print ${(j:,:):-\$${^@}} }" + [ $# -gt 0 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } + awk "{ print $(printf ',$%s' $@ | cut -b2-) }" + + # via: https://github.com/sorin-ionescu/prezto/ + #awk "{ print ${(j:,:):-\$${^@}} }" } [ "${SOURCING:-0}" -gt 0 ] || slit "$@" diff --git a/sh/slitt b/sh/slitt index ec63387..2ea45ae 100644 --- a/sh/slitt +++ b/sh/slitt @@ -1,10 +1,17 @@ -#!/usr/bin/env zsh -# via: https://github.com/sorin-ionescu/prezto/ -# this version uses tabs for input/output field separators. +#!/usr/bin/env dash +# this version of slit uses tabs for input/output field separators. # YES_ZSH +# YES_BASH +# YES_DASH slitt() { - awk "BEGIN { FS=\"\\t\"; OFS=\"\\t\" } { print ${(j:,:):-\$${^@}} }" + [ $# -gt 0 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; } + #echo "$@" | awk '{for(i=1;i&2; return 1; } awk "-F${1:- }" ' NF>1{f[$1]=substr($0,length($1)+1+length(FS))}END{for(k in f)print k FS f[k]} ' diff --git a/sh/tpad b/sh/tpad index c5d4ea7..d06d25b 100755 --- a/sh/tpad +++ b/sh/tpad @@ -1,10 +1,11 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH tpad() { - while [ $# -gt 0 ]; do - gm convert -bordercolor "#000000FF" -border 1x1 "$1" "${1/.png/}.tt.png"; - shift + for f; do + gm convert -bordercolor "#000000FF" -border 1x1 "$f" "${f%.png}.tt.png" done } diff --git a/sh/trash b/sh/trash index 380d760..a133229 100644 --- a/sh/trash +++ b/sh/trash @@ -1,5 +1,7 @@ #!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH trash() { [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } diff --git a/sh/trunc b/sh/trunc index 4223079..d94b17a 100755 --- a/sh/trunc +++ b/sh/trunc @@ -1,7 +1,10 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH trunc() { + [ $# -le 1 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } COLUMNS=${COLUMNS:-$(tput cols)} awk -vL=${1:-$COLUMNS} '{e=length>L?"…":"";print substr($0,0,L-(e?1:0)) e}' } diff --git a/sh/unscreen b/sh/unscreen index 0153594..99cdf08 100755 --- a/sh/unscreen +++ b/sh/unscreen @@ -1,5 +1,7 @@ #!/usr/bin/env zsh # YES_ZSH +# NO_BASH +# NO_DASH unscreen() { local pids="$(screen -ls | fgrep "$1" | cut -d. -f1)" diff --git a/sh/wat b/sh/wat index 5ad2bf9..0d978ce 100644 --- a/sh/wat +++ b/sh/wat @@ -1,5 +1,7 @@ #!/usr/bin/env zsh # YES_ZSH +# NO_BASH +# NO_DASH # wat - a better and recursive which/whence # via: https://leahneukirchen.org/dotfiles/tools.html diff --git a/sh/wipe b/sh/wipe index 63a2d5b..f7f7ba4 100644 --- a/sh/wipe +++ b/sh/wipe @@ -1,14 +1,20 @@ -#!/usr/bin/env bash +#!/usr/bin/env sh # YES_ZSH +# YES_BASH +# YES_DASH wipe() { # TODO: rename because wipe(1) already exists. [ $# -le 0 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; } clear clear # twice because mintty is weird - echo "\033[30m\033[107m" - printf "*%.0s" {1..$COLUMNS} - echo "\033[0m\n" + echo $'\033[30m\033[107m' | tr -d '$' + if [ -n "${ZSH_VERSION:-}" ]; then + printf '*%.0s' {1..$COLUMNS} + else + tput cols | awk '{for(i=0;i<$0;i++)printf "*"}' + fi + echo $'\033[0m\n' | tr -d '$' } [ "${SOURCING:-0}" -gt 0 ] || wipe "$@"