From a2b5f3e2c173284a85401794697ba6925c74b30c Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 30 Nov 2018 14:07:45 +1300 Subject: Use ||/&& short-circuiting only for flow control If we're doing something besides return/exit, it should be a proper `if` condition so that the semantics are clearer. --- bash/bash_logout | 4 +++- bash/bash_profile | 10 +++++--- bash/bashrc | 17 ++++++++++---- bash/bashrc.d/prompt.bash | 58 +++++++++++++++++++++++++++++++---------------- 4 files changed, 61 insertions(+), 28 deletions(-) (limited to 'bash') diff --git a/bash/bash_logout b/bash/bash_logout index 911b4f66..ab3f573d 100644 --- a/bash/bash_logout +++ b/bash/bash_logout @@ -1,2 +1,4 @@ # Clear console if possible when logging out -[ "$SHLVL" = 1 ] && clear_console -q 2>/dev/null +if [ "$SHLVL" = 1 ] ; then + clear_console -q 2>/dev/null +fi diff --git a/bash/bash_profile b/bash/bash_profile index 0376ee57..1f5a633a 100644 --- a/bash/bash_profile +++ b/bash/bash_profile @@ -1,5 +1,7 @@ # Load ~/.profile regardless of shell version -[ -e "$HOME"/.profile ] && . "$HOME"/.profile +if [ -e "$HOME"/.profile ] ; then + . "$HOME"/.profile +fi # If POSIXLY_CORRECT is set after doing that, force the `posix` option on and # don't load the rest of this stuff--so, just ~/.profile and ENV @@ -9,5 +11,7 @@ if [ -n "$POSIXLY_CORRECT" ] ; then fi # If ~/.bashrc exists, source that too; the tests for both interactivity and -# >=2.05a (for features like [[) are in there -[ -f "$HOME"/.bashrc ] && . "$HOME"/.bashrc +# minimum version numbers are in there +if [ -f "$HOME"/.bashrc ] ; then + . "$HOME"/.bashrc +fi diff --git a/bash/bashrc b/bash/bashrc index 06cbb6b3..e8348317 100644 --- a/bash/bashrc +++ b/bash/bashrc @@ -6,8 +6,9 @@ esac # Don't do anything if restricted, not even sourcing the ENV file # Testing $- for "r" doesn't work -# shellcheck disable=SC2128 -[ -n "$BASH_VERSINFO" ] && shopt -q restricted_shell && return +if shopt -q restricted_shell >/dev/null 2>&1 ; then + return +fi # Clear away all aliases; we do this here rather than in the $ENV file shared # between POSIX shells, because ksh relies on aliases to implement certain @@ -16,7 +17,9 @@ unalias -a # If ENV is set, source it to get all the POSIX-compatible interactive stuff; # we should be able to do this even if we're running a truly ancient Bash -[ -n "$ENV" ] && . "$ENV" +if [ -n "$ENV" ] ; then + . "$ENV" +fi # Ensure we're using at least version 3.0. [ -n "$BASH_VERSINFO" ] || return @@ -81,11 +84,15 @@ if ((BASH_VERSINFO[0] >= 4)) ; then # Warn me about stopped jobs when exiting # Available since 4.0, but only set it if >=4.1 due to bug: # - ((BASH_VERSINFO[1] >= 1)) && shopt -s checkjobs + if ((BASH_VERSINFO[1] >= 1)) ; then + shopt -s checkjobs + fi # Expand variables in directory completion # Only available since 4.3 - ((BASH_VERSINFO[1] >= 3)) && shopt -s direxpand + if ((BASH_VERSINFO[1] >= 3)) ; then + shopt -s direxpand + fi fi # Load Bash-specific startup files diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index a6506a60..26e10cd4 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -10,7 +10,9 @@ prompt() { PROMPT_COMMAND='history -a' # If Bash 4.0 is available, trim very long paths in prompt - ((BASH_VERSINFO[0] >= 4)) && PROMPT_DIRTRIM=4 + if ((BASH_VERSINFO[0] >= 4)) ; then + PROMPT_DIRTRIM=4 + fi # Basic prompt shape depends on whether we're in SSH or not PS1= @@ -118,44 +120,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 @@ -186,7 +199,8 @@ prompt() { # Exit if we couldn't get either--or, implicitly, if we don't have # svn(1). - [[ -n $url && -n $root ]] || return + [[ -n $url ]] || return + [[ -n $root ]] || return # Remove the root from the URL to get what's hopefully the branch # name, removing leading slashes and the 'branches' prefix, and any @@ -196,7 +210,7 @@ prompt() { branch=${branch#/} branch=${branch#branches/} branch=${branch%%/*} - [[ -n $branch ]] || branch=unknown + branch=${branch:-unknown} # Parse the output of svn status to determine working copy state local symbol @@ -210,8 +224,12 @@ prompt() { # Add appropriate state flags local state - ((modified)) && state=${state}'!' - ((untracked)) && state=${state}'?' + if ((modified)) ; then + state=${state}'!' + fi + if ((untracked)) ; then + state=${state}'?' + fi # Print the state in brackets with an svn: prefix printf '(svn:%s%s)' \ @@ -230,7 +248,8 @@ prompt() { # Show return status of previous command in angle brackets, if not zero ret) # shellcheck disable=SC2154 - ((ret)) && printf '<%u>' "${ret//\\/\\\\}" + ((ret)) || return + printf '<%u>' "${ret//\\/\\\\}" ;; # Show the count of background jobs in curly brackets, if not zero @@ -239,7 +258,8 @@ prompt() { while read -r ; do ((jobc++)) done < <(jobs -p) - ((jobc)) && printf '{%u}' "${jobc//\\/\\\\}" + ((jobc)) || return + printf '{%u}' "${jobc//\\/\\\\}" ;; # No argument given, print prompt strings and vars -- cgit v1.2.3