From f5ed4cd090b321064039441e40fe46a6995abf9a Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 31 Jul 2013 17:26:30 +1200 Subject: Use standard structure for if/for in shell Mostly for clarity reasons; using this syntax: if [ condition ]; then commands fi As opposed to: if [ condition ] then commands fi Or: [ condition ] && command --- bash/bash_logout | 7 +++--- bash/bash_profile | 8 +++++-- bash/bashrc | 14 ++++++------ bash/bashrc.d/grep.bash | 15 +++++++------ bash/bashrc.d/ls.bash | 5 +++-- bash/bashrc.d/prompt.bash | 55 +++++++++++++++++++++++++++++++---------------- sh/profile | 10 ++++----- sh/profile.d/browser.sh | 3 +-- sh/profile.d/keychain.sh | 10 +++++---- 9 files changed, 78 insertions(+), 49 deletions(-) diff --git a/bash/bash_logout b/bash/bash_logout index 15168ec8..c9b0ec6f 100644 --- a/bash/bash_logout +++ b/bash/bash_logout @@ -1,5 +1,6 @@ # Clear console if possible when logging out -[[ "$SHLVL" = 1 ]] \ - && command -v clear_console &>/dev/null \ - && clear_console -q +if [[ "$SHLVL" = 1 ]]; then + command -v clear_console &>/dev/null \ + && clear_console -q +fi diff --git a/bash/bash_profile b/bash/bash_profile index aa87667d..3b391209 100644 --- a/bash/bash_profile +++ b/bash/bash_profile @@ -1,6 +1,10 @@ # Source Bourne shell profile if it exists -[[ -r "$HOME/.profile" ]] && source $HOME/.profile +if [[ -r "$HOME/.profile" ]]; then + source $HOME/.profile +fi # Source interactive Bash config if it exists -[[ -r "$HOME/.bashrc" ]] && source $HOME/.bashrc +if [[ -r "$HOME/.bashrc" ]]; then + source $HOME/.bashrc +fi diff --git a/bash/bashrc b/bash/bashrc index 5e37d3f4..280a47ca 100644 --- a/bash/bashrc +++ b/bash/bashrc @@ -1,5 +1,7 @@ # Don't do anything if not running interactively -[[ -z "$PS1" ]] && return +if [[ -z "$PS1" ]]; then + return +fi # Keep plenty of history HISTFILESIZE=1000000 @@ -21,13 +23,13 @@ setterm -bfreq 0 &>/dev/null stty -ixon &>/dev/null # Use completion, if available -[[ -r /etc/bash_completion ]] && source /etc/bash_completion +if [[ -r /etc/bash_completion ]]; then + source /etc/bash_completion +fi # Load any supplementary scripts -if [[ -d "$HOME/.bashrc.d" ]] -then - for file in $HOME/.bashrc.d/* - do +if [[ -d "$HOME/.bashrc.d" ]]; then + for file in $HOME/.bashrc.d/*; do source $file done fi diff --git a/bash/bashrc.d/grep.bash b/bash/bashrc.d/grep.bash index f2586562..62acdb01 100644 --- a/bash/bashrc.d/grep.bash +++ b/bash/bashrc.d/grep.bash @@ -2,12 +2,15 @@ __grepopts() { local grepopts='-I' local grephelp="$(grep --help 2>/dev/null)" - [[ "$grephelp" == *--color* ]] \ - && grepopts="${grepopts} --color=auto" - [[ "$grephelp" == *--exclude* ]] \ - && grepopts="${grepopts} --exclude=.git{,ignore,modules}" - [[ "$grephelp" == *--exclude-dir* ]] \ - && grepopts="${grepopts} --exclude-dir=.{cvs,git,hg,svn}" + if [[ "$grephelp" == *--color* ]]; then + grepopts="${grepopts} --color=auto" + fi + if [[ "$grephelp" == *--exclude* ]]; then + grepopts="${grepopts} --exclude=.git{,ignore,modules}" + fi + if [[ "$grephelp" == *--exclude-dir* ]]; then + grepopts="${grepopts} --exclude-dir=.{cvs,git,hg,svn}" + fi printf '%s' "$grepopts" } diff --git a/bash/bashrc.d/ls.bash b/bash/bashrc.d/ls.bash index 076f34df..c4be33c2 100644 --- a/bash/bashrc.d/ls.bash +++ b/bash/bashrc.d/ls.bash @@ -2,8 +2,9 @@ __lsopts() { local lsopts= local lshelp="$(ls --help 2>/dev/null)" - [[ "$lshelp" == *--color* ]] \ - && lsopts="${lsopts} --color=auto" + if [[ "$lshelp" == *--color* ]]; then + lsopts="${lsopts} --color=auto" + fi printf '%s' "$lsopts" } diff --git a/bash/bashrc.d/prompt.bash b/bash/bashrc.d/prompt.bash index 3243dd18..0d477cc7 100644 --- a/bash/bashrc.d/prompt.bash +++ b/bash/bashrc.d/prompt.bash @@ -29,37 +29,48 @@ prompt() { # Git prompt function git) - $(git rev-parse --is-inside-git-dir 2>/dev/null) \ - && return 1 - $(git rev-parse --is-inside-work-tree 2>/dev/null) \ - || return 1 + if $(git rev-parse --is-inside-git-dir 2>/dev/null); then + return 1 + fi + if ! $(git rev-parse --is-inside-work-tree 2>/dev/null); then + return 1 + fi git status &>/dev/null branch=$(git symbolic-ref --quiet HEAD 2>/dev/null) \ || branch=$(git rev-parse --short HEAD 2>/dev/null) \ || branch='unknown' branch=${branch##*/} - git diff --quiet --ignore-submodules --cached \ - || state=${state}+ - git diff-files --quiet --ignore-submodules -- \ - || state=${state}! - $(git rev-parse --verify refs/stash &>/dev/null) \ - && state=${state}^ - [ -n "$(git ls-files --others --exclude-standard)" ] \ - && state=${state}? + if ! git diff --quiet --ignore-submodules --cached; then + state=${state}+ + fi + if ! git diff-files --quiet --ignore-submodules --; then + state=${state}! + fi + if $(git rev-parse --verify refs/stash &>/dev/null); then + state=${state}^ + fi + if [ -n "$(git ls-files --others --exclude-standard)" ]; then + state=${state}? + fi printf '(git:%s)' "${branch:-unknown}${state}" ;; # Mercurial prompt function hg) - hg branch &>/dev/null || return 1 - branch="$(hg branch 2>/dev/null)" - [[ -n "$(hg status 2>/dev/null)" ]] && state="!" + if ! branch="$(hg branch 2>/dev/null)"; then + return 1 + fi + if [[ -n "$(hg status 2>/dev/null)" ]]; then + state="!" + fi printf '(hg:%s)' "${branch:-unknown}${state}" ;; # Subversion prompt function svn) - svn info &>/dev/null || return 1 + if ! svn info &>/dev/null; then + return 1 + fi url="$(svn info 2>/dev/null | \ awk -F': ' '$1 == "URL" {print $2}')" root="$(svn info 2>/dev/null | \ @@ -68,7 +79,9 @@ prompt() { branch=${branch#/} branch=${branch#branches/} branch=${branch%%/*} - [[ -n "$(svn status 2>/dev/null)" ]] && state="!" + if [[ -n "$(svn status 2>/dev/null)" ]]; then + state="!" + fi printf '(svn:%s)' "${branch:-unknown}${state}" ;; @@ -79,12 +92,16 @@ prompt() { # Return status prompt function return) - [[ $ret -ne 0 ]] && printf '<%d>' ${ret} + if [[ $ret -ne 0 ]]; then + printf '<%d>' ${ret} + fi ;; # Job count prompt function jobs) - [[ -n "$(jobs)" ]] && printf '{%d}' $(jobs | sed -n '$=') + if [[ -n "$(jobs)" ]]; then + printf '{%d}' $(jobs | sed -n '$=') + fi ;; esac } diff --git a/sh/profile b/sh/profile index d5a2fec8..0a1e12a3 100644 --- a/sh/profile +++ b/sh/profile @@ -6,14 +6,14 @@ export VISUAL="$EDITOR" export PAGER='less' # Add ~/.local/bin to PATH -[ -d "$HOME/.local/bin" ] && PATH="$HOME/.local/bin:$PATH" +if [ -d "$HOME/.local/bin" ]; then + PATH="$HOME/.local/bin:$PATH" +fi export PATH # Load any supplementary scripts -if [ -d "$HOME/.profile.d" ] -then - for file in $HOME/.profile.d/* - do +if [ -d "$HOME/.profile.d" ]; then + for file in $HOME/.profile.d/*; do . $file done fi diff --git a/sh/profile.d/browser.sh b/sh/profile.d/browser.sh index 2b060b06..094d2095 100644 --- a/sh/profile.d/browser.sh +++ b/sh/profile.d/browser.sh @@ -1,6 +1,5 @@ # Browser -if [ -n "$DISPLAY" ] -then +if [ -n "$DISPLAY" ]; then BROWSER='firefox' else BROWSER='lynx' diff --git a/sh/profile.d/keychain.sh b/sh/profile.d/keychain.sh index a53edb65..3b7b5456 100644 --- a/sh/profile.d/keychain.sh +++ b/sh/profile.d/keychain.sh @@ -1,6 +1,8 @@ # Keychain -command -v ssh-askpass >/dev/null 2>&1 \ - && export SSH_ASKPASS=`which ssh-askpass` -command -v keychain >/dev/null 2>&1 \ - && eval `keychain --eval --ignore-missing --quiet id_dsa id_rsa id_ecsda` +if command -v ssh-askpass >/dev/null; then + export SSH_ASKPASS=`which ssh-askpass` +fi +if command -v keychain >/dev/null; then + eval `keychain --eval --ignore-missing --quiet id_dsa id_rsa id_ecsda` +fi -- cgit v1.2.3