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 +++++++++++++---------- ksh/kshrc.d/prompt.ksh | 40 +++++++++++++++++++++++++++------------- ksh/shrc.d/ksh.sh | 4 +++- zsh/zshrc.d/prompt.zsh | 39 ++++++++++++++++++++++++++------------- 13 files changed, 103 insertions(+), 50 deletions(-) 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 diff --git a/ksh/kshrc.d/prompt.ksh b/ksh/kshrc.d/prompt.ksh index c5f3ee1b..99e6238d 100644 --- a/ksh/kshrc.d/prompt.ksh +++ b/ksh/kshrc.d/prompt.ksh @@ -117,27 +117,34 @@ function prompt { # Check various files in .git to flag processes typeset proc - [[ -d .git/rebase-merge || -d .git/rebase-apply ]] && + if [[ -d .git/rebase-merge || -d .git/rebase-apply ]] ; then proc=${proc:+"$proc",}'REBASE' - [[ -f .git/MERGE_HEAD ]] && + fi + if [[ -f .git/MERGE_HEAD ]] ; then proc=${proc:+"$proc",}'MERGE' - [[ -f .git/CHERRY_PICK_HEAD ]] && + fi + if [[ -f .git/CHERRY_PICK_HEAD ]] ; then proc=${proc:+"$proc",}'PICK' - [[ -f .git/REVERT_HEAD ]] && + fi + if [[ -f .git/REVERT_HEAD ]] ; then proc=${proc:+"$proc",}'REVERT' - [[ -f .git/BISECT_LOG ]] && + fi + if [[ -f .git/BISECT_LOG ]] ; then proc=${proc:+"$proc",}'BISECT' + fi # Collect symbols representing repository state typeset state # Upstream HEAD has commits after local HEAD; we're "behind" - (($(git rev-list --count 'HEAD..@{u}'))) && + if (($(git rev-list --count 'HEAD..@{u}'))) ; then state=${state}'<' + fi # Local HEAD has commits after upstream HEAD; we're "ahead" - (($(git rev-list --count '@{u}..HEAD'))) && + if (($(git rev-list --count '@{u}..HEAD'))) ; then state=${state}'>' + fi # Tracked files are modified if ! git diff-files --no-ext-diff --quiet ; then @@ -164,17 +171,20 @@ function prompt { fi # Changes are staged - git diff-index --cached --no-ext-diff --quiet HEAD || + if ! git diff-index --cached --no-ext-diff --quiet HEAD ; then state=${state}'+' + fi # There are some untracked and unignored files - git ls-files --directory --error-unmatch --exclude-standard \ - --no-empty-directory --others -- ':/*' && + if git ls-files --directory --error-unmatch --exclude-standard \ + --no-empty-directory --others -- ':/*' ; then state=${state}'?' + fi # There are stashed changes - git rev-parse --quiet --verify refs/stash && + if git rev-parse --quiet --verify refs/stash ; then state=${state}'^' + fi } >/dev/null 2>&1 @@ -214,13 +224,17 @@ function prompt { # Show return status of previous command in angle brackets, if not zero ret) # shellcheck disable=SC2154 - ((ret)) && printf '<%u>' "$ret" + if ((ret)) ; then + printf '<%u>' "$ret" + fi ;; # Show the count of background jobs in curly brackets, if not zero job) # shellcheck disable=SC2154 - ((jobc)) && printf '{%u}' "$jobc" + if ((jobc)) ; then + printf '{%u}' "$jobc" + fi ;; # Print error diff --git a/ksh/shrc.d/ksh.sh b/ksh/shrc.d/ksh.sh index 5ad14b9c..9e032756 100644 --- a/ksh/shrc.d/ksh.sh +++ b/ksh/shrc.d/ksh.sh @@ -29,4 +29,6 @@ if [ -z "$KSH_VERSION" ] ; then fi # If ENV_EXT isn't already set, set it -[ -n "$ENV_EXT" ] || ENV_EXT=$HOME/.kshrc +if [ -z "$ENV_EXT" ] ; then + ENV_EXT=$HOME/.kshrc +fi diff --git a/zsh/zshrc.d/prompt.zsh b/zsh/zshrc.d/prompt.zsh index b612c704..689866e3 100644 --- a/zsh/zshrc.d/prompt.zsh +++ b/zsh/zshrc.d/prompt.zsh @@ -81,44 +81,55 @@ prompt() { # Check various files in .git to flag processes local proc - [[ -d .git/rebase-merge || -d .git/rebase-apply ]] && + if [[ -d .git/rebase-merge || -d .git/rebase-apply ]] ; then proc=${proc:+"$proc",}'REBASE' - [[ -f .git/MERGE_HEAD ]] && + fi + if [[ -f .git/MERGE_HEAD ]] ; then proc=${proc:+"$proc",}'MERGE' - [[ -f .git/CHERRY_PICK_HEAD ]] && + fi + if [[ -f .git/CHERRY_PICK_HEAD ]] ; then proc=${proc:+"$proc",}'PICK' - [[ -f .git/REVERT_HEAD ]] && + fi + if [[ -f .git/REVERT_HEAD ]] ; then proc=${proc:+"$proc",}'REVERT' - [[ -f .git/BISECT_LOG ]] && + fi + if [[ -f .git/BISECT_LOG ]] ; then proc=${proc:+"$proc",}'BISECT' + fi # Collect symbols representing repository state local state # Upstream HEAD has commits after local HEAD; we're "behind" - (($(git rev-list --count 'HEAD..@{u}'))) && + if (($(git rev-list --count 'HEAD..@{u}'))) ; then state=${state}'<' + fi # Local HEAD has commits after upstream HEAD; we're "ahead" - (($(git rev-list --count '@{u}..HEAD'))) && + if (($(git rev-list --count '@{u}..HEAD'))) ; then state=${state}'>' + fi # Tracked files are modified - git diff-files --no-ext-diff --quiet || + if ! git diff-files --no-ext-diff --quiet ; then state=${state}'!' + fi # Changes are staged - git diff-index --cached --no-ext-diff --quiet HEAD || + if ! git diff-index --cached --no-ext-diff --quiet HEAD ; then state=${state}'+' + fi # There are some untracked and unignored files - git ls-files --directory --error-unmatch --exclude-standard \ - --no-empty-directory --others -- ':/*' && + if git ls-files --directory --error-unmatch --exclude-standard \ + --no-empty-directory --others -- ':/*' ; then state=${state}'?' + fi # There are stashed changes - git rev-parse --quiet --verify refs/stash && + if git rev-parse --quiet --verify refs/stash ; then state=${state}'^' + fi } >/dev/null 2>&1 @@ -154,7 +165,9 @@ prompt() { branch=${branch#/} branch=${branch#branches/} branch=${branch%%/*} - [[ -n $branch ]] || branch=unknown + if [[ -z $branch ]] ; then + branch=unknown + fi # Parse the output of svn status to determine working copy state local symbol -- cgit v1.2.3