From 0c84989ca0b97ca1d075e7a304a407553e34381d Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 31 Dec 2018 14:19:57 +1300 Subject: Clarify control flow in shell scripts --- bin/bcq.sh | 4 +++- bin/eds.sh | 5 ++++- bin/fnp.sh | 8 ++++++-- bin/gms.sh | 7 +++++-- bin/grc.sh | 2 +- bin/osc.sh | 8 ++++++-- bin/plmu.sh | 8 ++++++-- bin/td.sh | 2 +- bin/umake.sh | 3 ++- bin/xgo.sh | 23 +++++++++++++---------- 10 files changed, 47 insertions(+), 23 deletions(-) (limited to 'bin') diff --git a/bin/bcq.sh b/bin/bcq.sh index a6c0fe60..1f4f3f9e 100644 --- a/bin/bcq.sh +++ b/bin/bcq.sh @@ -1,3 +1,5 @@ # Fire up bc(1), hushing it if it looks like GNU -[ -e "$HOME"/.cache/sh/opt/bc/quiet ] && set -- --quiet "$@" +if [ -e "$HOME"/.cache/sh/opt/bc/quiet ] ; then + set -- --quiet "$@" +fi exec bc "$@" diff --git a/bin/eds.sh b/bin/eds.sh index 7e719e9d..63e1a772 100644 --- a/bin/eds.sh +++ b/bin/eds.sh @@ -23,7 +23,10 @@ esac # Prepend the path to each of the names given if they don't look like options for arg do - [ -n "$reset" ] || set -- && reset=1 + if [ -z "$reset" ] ; then + set -- + reset=1 + fi case $arg in --) optend=1 diff --git a/bin/fnp.sh b/bin/fnp.sh index bc0c7e21..c5beddc6 100644 --- a/bin/fnp.sh +++ b/bin/fnp.sh @@ -1,7 +1,9 @@ # Print input, but include filenames as headings # Assume stdin if no options given -[ "$#" -gt 0 ] || set -- - +if [ "$#" -eq 0 ] ; then + set -- - +fi # Iterate through arguments for arg do @@ -13,7 +15,9 @@ for arg do *) fn=$arg ;; esac - [ -n "$tail" ] && printf '\n' + if [ -n "$tail" ] ; then + printf '\n' + fi tail=1 # Form the underline; is there a nicer way to do this in POSIX sh? diff --git a/bin/gms.sh b/bin/gms.sh index b77da6fa..c33c747e 100644 --- a/bin/gms.sh +++ b/bin/gms.sh @@ -3,7 +3,9 @@ # Trap to remove whatever's set in lockdir if we're killed lockdir= cleanup() { - [ -n "$lockdir" ] && rm -fr -- "$lockdir" + if [ -n "$lockdir" ] ; then + rm -fr -- "$lockdir" + fi if [ "$1" != EXIT ] ; then trap - "$1" kill "-$1" "$$" @@ -23,7 +25,8 @@ for rcfile in "${GETMAIL:-"$HOME"/.getmail}"/getmailrc.* ; do ( lockdir=${TMPDIR:-/tmp}/getmail.$uid.${rcfile##*/}.lock mkdir -m 0700 -- "$lockdir" 2>/dev/null || exit try -n 3 -s 15 getmail --rcfile "$rcfile" "$@" - rm -fr -- "$lockdir" && lockdir= + rm -fr -- "$lockdir" + lockdir= ) & done # Wait for all of the enqueued tasks to finish diff --git a/bin/grc.sh b/bin/grc.sh index 184baf8e..bfcb648d 100644 --- a/bin/grc.sh +++ b/bin/grc.sh @@ -12,4 +12,4 @@ fi # Exit 0 if the first command gives any output (added files) or the second one # exits 1 (inverted; differences in tracked files) [ -n "$(git ls-files --others --exclude-standard)" ] || -! git diff-index --quiet HEAD + ! git diff-index --quiet HEAD diff --git a/bin/osc.sh b/bin/osc.sh index 86923f12..5def12ff 100644 --- a/bin/osc.sh +++ b/bin/osc.sh @@ -57,8 +57,12 @@ set -- "$@" -connect "$host":"$serv" td='' fil='' cleanup() { trap - EXIT "$1" - [ -n "$fil" ] && kill -TERM "$fil" - [ -n "$td" ] && rm -fr -- "$td" + if [ -n "$fil" ] ; then + kill -TERM "$fil" + fi + if [ -n "$td" ] ; then + rm -fr -- "$td" + fi if [ "$1" != EXIT ] ; then kill -"$1" "$$" fi diff --git a/bin/plmu.sh b/bin/plmu.sh index 5c599828..3f237ae2 100644 --- a/bin/plmu.sh +++ b/bin/plmu.sh @@ -1,8 +1,12 @@ # Upgrade plenv modules with cpanm(1) # Set up exceptions file if it exists -ef=$HOME/.plenv/non-cpanm-modules -[ -e "$ef" ] || ef=/dev/null +def="$HOME"/.plenv/non-cpanm-modules +if [ -e "$def" ] ; then + ef=$def +else + ef=/dev/null +fi # Check that exceptions file is sorted if ! LC_COLLATE=C sort -c -- "$ef" ; then diff --git a/bin/td.sh b/bin/td.sh index fb5610c5..ef0be618 100644 --- a/bin/td.sh +++ b/bin/td.sh @@ -28,4 +28,4 @@ git add -- "$file" # If there are changes to commit, commit them git diff-index --quiet HEAD 2>/dev/null || -git commit --message 'Changed by td(1df)' --quiet + git commit --message 'Changed by td(1df)' --quiet diff --git a/bin/umake.sh b/bin/umake.sh index 21073328..3c381e09 100644 --- a/bin/umake.sh +++ b/bin/umake.sh @@ -2,7 +2,8 @@ # any given args while [ "$PWD" != / ] ; do for mf in makefile Makefile ; do - [ -f "$mf" ] && exec make "$@" + [ -f "$mf" ] || continue + exec make "$@" done cd .. || exit done diff --git a/bin/xgo.sh b/bin/xgo.sh index 1b9f83da..6d6586ef 100644 --- a/bin/xgo.sh +++ b/bin/xgo.sh @@ -15,7 +15,8 @@ for url do ( # If this is a GitHub or GitLab link, swap "blob" for "raw" to get the # actual file (*://github.com/*/blob/*|*://gitlab.com/*/blob/*) - url=$(printf '%s\n' "$url" | sed 's_/blob/_/raw/_') + url=$(printf '%s\n' "$url" | + sed 's_/blob/_/raw/_') ;; # Dig out the plain text for pastebin.com links @@ -38,7 +39,7 @@ for url do ( # mpv(1) (*[/.]youtube.com/watch*[?\&]t=) ;; (*[/.]youtube.com/watch*) - mpv -- "$url" && exit + exec mpv -- "$url" ;; esac @@ -54,30 +55,32 @@ for url do ( ( cd -- "$HOME"/Downloads || exit curl -O -- "$url" || exit - xpdf -- "${url##*/}" - ) && exit + exec xpdf -- "${url##*/}" + ) ;; # Open audio and video in mpv(1); force a window even for audio so I # can control it (audio/*|video/*) - mpv --force-window -- "$url" && exit + exec mpv --force-window -- "$url" ;; # If the MIME type is an image that is not a GIF, load it in feh(1) (image/gif) ;; (image/*) - curl -- "$url" | feh - && exit + exec curl -- "$url" | feh - ;; # Open plain text in a terminal view(1) (text/plain) # shellcheck disable=SC2016 - urxvt -e sh -c 'curl -- "$1" | view -' _ "$url" && exit + exec urxvt -e sh -c 'curl -- "$1" | view -' _ "$url" ;; - esac - # Otherwise, just pass it to br(1df) - br "$url" + # Otherwise, just pass it to br(1df) + (*) + exec br "$url" + ;; + esac ) & done -- cgit v1.2.3