aboutsummaryrefslogtreecommitdiff
path: root/sh
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-12-11 14:52:11 +1300
committerTom Ryder <tom@sanctum.geek.nz>2018-12-11 14:52:11 +1300
commit6e8c6c521ae5d60427634be01b82fe61a79f192b (patch)
tree5349fc3e9838c69dbb6fd69f0234ea921ea446ea /sh
parentFactor out zsh ENV hack into one file (diff)
downloaddotfiles-6e8c6c521ae5d60427634be01b82fe61a79f192b.tar.gz
dotfiles-6e8c6c521ae5d60427634be01b82fe61a79f192b.zip
Refactor some conditionals
Diffstat (limited to 'sh')
-rw-r--r--sh/profile4
-rw-r--r--sh/profile.d/downloads.sh6
-rw-r--r--sh/profile.d/options.sh5
-rw-r--r--sh/profile.d/welcome.sh10
-rw-r--r--sh/shinit9
-rw-r--r--sh/shrc4
-rw-r--r--sh/shrc.d/bc.sh3
-rw-r--r--sh/shrc.d/ed.sh6
-rw-r--r--sh/shrc.d/grep.sh20
-rw-r--r--sh/shrc.d/gt.sh4
-rw-r--r--sh/shrc.d/ls.sh36
-rw-r--r--sh/shrc.d/mkcd.sh3
-rw-r--r--sh/shrc.d/path.sh16
-rw-r--r--sh/shrc.d/tree.sh2
-rw-r--r--sh/shrc.d/vr.sh11
15 files changed, 91 insertions, 48 deletions
diff --git a/sh/profile b/sh/profile
index 544ee640..7f16cb32 100644
--- a/sh/profile
+++ b/sh/profile
@@ -1,5 +1,7 @@
# Add ~/.local/bin to PATH if it exists
-[ -d "$HOME"/.local/bin ] && PATH=$HOME/.local/bin:$PATH
+if [ -d "$HOME"/.local/bin ] ; then
+ PATH=$HOME/.local/bin:$PATH
+fi
# Load all supplementary scripts in ~/.profile.d
for sh in "$HOME"/.profile.d/*.sh ; do
diff --git a/sh/profile.d/downloads.sh b/sh/profile.d/downloads.sh
index 865cb859..1a89bc3f 100644
--- a/sh/profile.d/downloads.sh
+++ b/sh/profile.d/downloads.sh
@@ -8,7 +8,7 @@ esac
[ -z "$TMUX" ] || return
# Not if ~/.hushlogin exists
-[ -e "$HOME"/.hushlogin ] && return
+! [ -e "$HOME"/.hushlogin ] || return
# Not if ~/.downloads doesn't
[ -f "$HOME"/.downloads ] || return
@@ -27,5 +27,7 @@ esac
printf 'You have %u unsorted files in %s.\n' "$#" "$dir"
lc=$((lc+1))
done < "$HOME"/.downloads
- [ "$((lc > 0))" -eq 1 ] && printf '\n'
+ if [ "$lc" -gt 0 ] ; then
+ printf '\n'
+ fi
)
diff --git a/sh/profile.d/options.sh b/sh/profile.d/options.sh
index f89967cb..58376fb3 100644
--- a/sh/profile.d/options.sh
+++ b/sh/profile.d/options.sh
@@ -23,8 +23,9 @@ options() {
# Iterate through remaining arguments (desired options), creating files to
# show they're available if found in the help output
for opt do
- command -p grep -q -- '[^[:alnum:]]--'"$opt"'[^[:alnum:]]' help &&
- touch -- "$opt"
+ command -p grep -q -- \
+ '[^[:alnum:]]--'"$opt"'[^[:alnum:]]' help || continue
+ touch -- "$opt"
done
}
diff --git a/sh/profile.d/welcome.sh b/sh/profile.d/welcome.sh
index ede7a05f..cdd41edb 100644
--- a/sh/profile.d/welcome.sh
+++ b/sh/profile.d/welcome.sh
@@ -20,8 +20,10 @@ esac
# Show a fortune
if welcome fortune ; then
- [ -d "$HOME"/.local/share/games/fortunes ] &&
- : "${FORTUNE_PATH:="$HOME"/.local/share/games/fortunes}"
+ if ! [ -n "$FORTUNE_PATH"] &&
+ [ -d "$HOME"/.local/share/games/fortunes ] ; then
+ FORTUNE_PATH=$HOME/.local/share/games/fortunes
+ fi
fortune -s "$FORTUNE_PATH"
printf '\n'
fi
@@ -34,7 +36,9 @@ esac
# Run verse(1) if we haven't seen it already today
if welcome verse ; then
- [ -f "$HOME"/.verse ] && read -r last <"$HOME"/.verse
+ if [ -f "$HOME"/.verse ] ; then
+ read -r last <"$HOME"/.verse
+ fi
now=$(date +%Y%m%d)
if [ "$now" -gt "${last:-0}" ] ; then
verse
diff --git a/sh/shinit b/sh/shinit
index fe770a70..fb72cd14 100644
--- a/sh/shinit
+++ b/sh/shinit
@@ -1,4 +1,7 @@
-# If the shell is interactive, source ~/.shrc
-case $- in *i*)
- [ -f "$HOME"/.shrc ] && . "$HOME"/.shrc ;;
+# If the shell is interactive, and ~/.shrc exists, source it
+case $- in
+ *i*)
+ if [ -f "$HOME"/.shrc ] ; then
+ . "$HOME"/.shrc
+ fi
esac
diff --git a/sh/shrc b/sh/shrc
index 6359dbc3..808e944d 100644
--- a/sh/shrc
+++ b/sh/shrc
@@ -22,5 +22,7 @@ done
unset -v sh
# If ENV_EXT was set and exists, source that too, then clean it away
-[ -e "$ENV_EXT" ] && . "$ENV_EXT"
+if [ -e "$ENV_EXT" ] ; then
+ . "$ENV_EXT"
+fi
unset -v ENV_EXT
diff --git a/sh/shrc.d/bc.sh b/sh/shrc.d/bc.sh
index aee88e09..591b4359 100644
--- a/sh/shrc.d/bc.sh
+++ b/sh/shrc.d/bc.sh
@@ -6,8 +6,9 @@
bc() {
# Add --quiet to stop the annoying welcome banner
- [ -e "$HOME"/.cache/sh/opt/bc/quiet ] &&
+ if [ -e "$HOME"/.cache/sh/opt/bc/quiet ] ; then
set -- --quiet "$@"
+ fi
# Run bc(1) with the concluded arguments
command bc "$@"
diff --git a/sh/shrc.d/ed.sh b/sh/shrc.d/ed.sh
index e6b6eee8..dc8433f6 100644
--- a/sh/shrc.d/ed.sh
+++ b/sh/shrc.d/ed.sh
@@ -12,16 +12,18 @@ ed() {
fi
# Add --verbose to explain errors
- [ -e "$HOME"/.cache/sh/opt/ed/verbose ] &&
+ if [ -e "$HOME"/.cache/sh/opt/ed/verbose ] ; then
set -- --verbose "$@"
+ fi
# Add an asterisk prompt (POSIX feature)
set -- -p\* "$@"
# Run in rlwrap(1) if available
set -- ed "$@"
- command -v rlwrap >/dev/null 2>&1 &&
+ if command -v rlwrap >/dev/null 2>&1 ; then
set -- rlwrap --history-filename=/dev/null "$@"
+ fi
# Run determined command
command "$@"
diff --git a/sh/shrc.d/grep.sh b/sh/shrc.d/grep.sh
index 43797ef5..997babc9 100644
--- a/sh/shrc.d/grep.sh
+++ b/sh/shrc.d/grep.sh
@@ -9,37 +9,43 @@ unset -v GREP_OPTIONS
grep() {
# Add --binary-files=without-match to gracefully skip binary files
- [ -e "$HOME"/.cache/sh/opt/grep/binary-files ] &&
+ if [ -e "$HOME"/.cache/sh/opt/grep/binary-files ] ; then
set -- --binary-files=without-match "$@"
+ fi
# Add --color=auto if the terminal has at least 8 colors
- [ -e "$HOME"/.cache/sh/opt/grep/color ] &&
- [ "$({ tput colors||tput Co||echo 0; } 2>/dev/null)" -ge 8 ] &&
+ if [ -e "$HOME"/.cache/sh/opt/grep/color ] &&
+ [ "$({ tput colors||tput Co||echo 0; } 2>/dev/null)" -ge 8 ] ; then
set -- --color=auto "$@"
+ fi
# Add --devices=skip to gracefully skip devices
- [ -e "$HOME"/.cache/sh/opt/grep/devices ] &&
+ if [ -e "$HOME"/.cache/sh/opt/grep/devices ] ; then
set -- --devices=skip "$@"
+ fi
# Add --directories=skip to gracefully skip directories
- [ -e "$HOME"/.cache/sh/opt/grep/directories ] &&
+ if [ -e "$HOME"/.cache/sh/opt/grep/directories ] ; then
set -- --directories=skip "$@"
+ fi
# Add --exclude to ignore .gitignore and .gitmodules files
- [ -e "$HOME"/.cache/sh/opt/grep/exclude ] &&
+ if [ -e "$HOME"/.cache/sh/opt/grep/exclude ] ; then
set -- \
--exclude=.gitignore \
--exclude=.gitmodules \
"$@"
+ fi
# Add --exclude-dir to ignore version control dot-directories
- [ -e "$HOME"/.cache/sh/opt/grep/exclude-dir ] &&
+ if [ -e "$HOME"/.cache/sh/opt/grep/exclude-dir ] ; then
set -- \
--exclude-dir=.cvs \
--exclude-dir=.git \
--exclude-dir=.hg \
--exclude-dir=.svn \
"$@"
+ fi
# Run grep(1) with the concluded arguments
command grep "$@"
diff --git a/sh/shrc.d/gt.sh b/sh/shrc.d/gt.sh
index 95ab4c2f..7a52571d 100644
--- a/sh/shrc.d/gt.sh
+++ b/sh/shrc.d/gt.sh
@@ -19,7 +19,9 @@ gt() {
done
# If target isn't a directory, chop to its parent
- [ -d "$1" ] || set -- "${1%/*}"
+ if ! [ -d "$1" ] ; then
+ set -- "${1%/*}"
+ fi
# Try to change into the determined directory, or root if empty
command cd -- "${1:-/}"
diff --git a/sh/shrc.d/ls.sh b/sh/shrc.d/ls.sh
index 7e843cc7..1083dfca 100644
--- a/sh/shrc.d/ls.sh
+++ b/sh/shrc.d/ls.sh
@@ -12,26 +12,30 @@ unset -v LS_OPTIONS LS_COLORS
# Define function proper
ls() {
- # -F to show trailing indicators of the filetype
- # -q to replace control chars with '?'
+ # POSIX options:
+ ## -F to show trailing indicators of the filetype
+ ## -q to replace control chars with '?'
set -- -Fq "$@"
-
- # If output is to a terminal, add -x to format entries across, not down
- [ -t 1 ] && set -- -x "$@"
-
- # Add --block-size=K to always show the filesize in kibibytes
- [ -e "$HOME"/.cache/sh/opt/ls/block-size ] &&
+ ## -x to format entries across, not down, if output looks like a terminal
+ if [ -t 1 ] ; then
+ set -- -x "$@"
+ fi
+
+ # GNU options:
+ ## Add --block-size=K to always show the filesize in kibibytes
+ if [ -e "$HOME"/.cache/sh/opt/ls/block-size ] ; then
set -- --block-size=1024 "$@"
-
- # Add --color if the terminal has at least 8 colors
- [ -e "$HOME"/.cache/sh/opt/ls/color ] &&
- [ "$({ tput colors||tput Co||echo 0; } 2>/dev/null)" -ge 8 ] &&
+ fi
+ ## Add --color if the terminal has at least 8 colors
+ if [ -e "$HOME"/.cache/sh/opt/ls/color ] &&
+ [ "$(exec 2>/dev/null;tput colors||tput Co||echo 0)" -ge 8 ] ; then
set -- --color=auto "$@"
-
- # Add --time-style='+%Y-%m-%d %H:%M:%S' to show the date in my preferred
- # (fixed) format
- [ -e "$HOME"/.cache/sh/opt/ls/time-style ] &&
+ fi
+ ## Add --time-style='+%Y-%m-%d %H:%M:%S' to show the date in my preferred
+ ## (fixed) format
+ if [ -e "$HOME"/.cache/sh/opt/ls/time-style ] ; then
set -- --time-style='+%Y-%m-%d %H:%M:%S' "$@"
+ fi
# If the operating system is FreeBSD, there are some specific options we
# can add that might mean different things to e.g. GNU ls(1)
diff --git a/sh/shrc.d/mkcd.sh b/sh/shrc.d/mkcd.sh
index c59a8c54..cd882b51 100644
--- a/sh/shrc.d/mkcd.sh
+++ b/sh/shrc.d/mkcd.sh
@@ -1,4 +1,5 @@
# Create a directory and change into it
mkcd() {
- mkdir -p -- "$1" && command cd -- "$1"
+ command -p mkdir -p -- "$1" || return
+ command cd -- "$1"
}
diff --git a/sh/shrc.d/path.sh b/sh/shrc.d/path.sh
index 70947515..a854e148 100644
--- a/sh/shrc.d/path.sh
+++ b/sh/shrc.d/path.sh
@@ -34,7 +34,9 @@ path() {
# Add a directory at the start of $PATH
insert)
- [ "$#" -eq 2 ] || set -- "$1" "$PWD"
+ if ! [ "$#" -eq 2 ] ; then
+ set -- "$1" "$PWD"
+ fi
path _argcheck "$@" || return
if path check "$2" ; then
printf >&2 'path(): %s: %s already in PATH\n' "$@"
@@ -45,7 +47,9 @@ path() {
# Add a directory to the end of $PATH
append)
- [ "$#" -eq 2 ] || set -- "$1" "$PWD"
+ if ! [ "$#" -eq 2 ] ; then
+ set -- "$1" "$PWD"
+ fi
path _argcheck "$@" || return
if path check "$2" ; then
printf >&2 'path(): %s: %s already in PATH\n' "$@"
@@ -56,7 +60,9 @@ path() {
# Remove a directory from $PATH
remove)
- [ "$#" -eq 2 ] || set -- "$1" "$PWD"
+ if ! [ "$#" -eq 2 ] ; then
+ set -- "$1" "$PWD"
+ fi
path _argcheck "$@" || return
if ! path check "$2" ; then
printf >&2 'path(): %s: %s not in PATH\n' "$@"
@@ -108,7 +114,9 @@ path() {
# Check whether a directory is in PATH
check)
path _argcheck "$@" || return
- [ "$#" -eq 2 ] || set -- "$1" "$PWD"
+ if ! [ "$#" -eq 2 ] ; then
+ set -- "$1" "$PWD"
+ fi
case :$PATH: in
*:"$2":*) return 0 ;;
esac
diff --git a/sh/shrc.d/tree.sh b/sh/shrc.d/tree.sh
index d462f3e1..a7e5bef3 100644
--- a/sh/shrc.d/tree.sh
+++ b/sh/shrc.d/tree.sh
@@ -22,7 +22,7 @@ tree() {
[ -t 1 ] || exit
# Not if output terminal doesn't have at least 8 colors
- [ "$({ tput colors||tput Co||echo 0; } 2>/dev/null)" -ge 8 ]
+ [ "$(exec 2>/dev/null;tput colors||tput Co||echo 0)" -ge 8 ]
) ; then
set -- -C "$@"
diff --git a/sh/shrc.d/vr.sh b/sh/shrc.d/vr.sh
index 8b35357c..c7057ec2 100644
--- a/sh/shrc.d/vr.sh
+++ b/sh/shrc.d/vr.sh
@@ -11,9 +11,14 @@ vr() {
exit 2
fi
- # Get path from first argument, strip trailing slash
+ # Get path from first argument
path=${1:-"$PWD"}
- [ "$path" = / ] || path=${path%/}
+
+ # Strip a trailing slash
+ case $path in
+ (/) ;;
+ (*) path=${path%/} ;;
+ esac
# Step into the directory
cd -- "$path" || exit
@@ -34,7 +39,7 @@ vr() {
# that is the root (bad)
while svn info >/dev/null 2>&1 ; do
root=$PWD
- [ "$root" = / ] && break
+ ! [ "$root" = / ] || break
cd .. || exit
done
if [ -n "$root" ] ; then