From cee85ee94fbcd38b2cd506f51e4308f81c622247 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Fri, 22 Jan 2021 20:22:08 -0500 Subject: Update the shell configuration a bit --- mkshrc | 128 ++++++++++++++++++++++++++++++++-------------------------- shell/aliases | 13 +++--- shell/ash | 2 +- shell/conf | 25 +++++++----- shell/notice | 2 +- shell/update | 37 ++++++++--------- shell/utils | 51 +++++++++++++---------- 7 files changed, 143 insertions(+), 115 deletions(-) diff --git a/mkshrc b/mkshrc index 9e3ea8f..9800ef2 100644 --- a/mkshrc +++ b/mkshrc @@ -1,4 +1,4 @@ -# Copyright 2020 Mitchell Riedstra +# Copyright 2021 Mitchell Riedstra # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -85,27 +85,28 @@ setaliases() { } setcoloraliases() { - if [ `uname` = "Linux" ] ; then + if [ "$(uname)" = "Linux" ] ; then alias ls="ls --color=auto -F"; alias lm='ls -lh --color=force | more'; - elif [ `uname` = "FreeBSD" ] ; then + elif [ "$(uname)" = "FreeBSD" ] ; then alias ls="ls -FG"; alias lm='CLICOLOR_FORCE="1" ls -l | less -r'; fi if echo "color test" | grep -q --color=auto "color test" >/dev/null 2>&1 ; then alias grep="grep --color=auto"; - alias egrep='egrep --color=auto' - alias search='egrep --color=auto -rnI'; + alias egrep='grep -E --color=auto' + alias search='grep -E --color=auto -rnI'; fi - alias diff="$(which diff colordiff 2>/dev/null | tail -n 1)"; + #shellcheck disable=SC2139 + alias diff="$(command -v diff colordiff 2>/dev/null | tail -n 1)"; alias t='tree -CdL' } setsudoaliases() { # Prefer doas if available, as it's likely to be configured by me over sudo - _sudo="$(which doas sudo 2>/dev/null | sed 1q)" + _sudo="$(command -v doas sudo 2>/dev/null | sed 1q)" if [ -x "$_sudo" ] ; then # Package managers # Isn't it amazing how many distros you end up using? @@ -151,23 +152,27 @@ set_go() { export PATH="$_gobin:$PATH" fi } -set_editor() { - if [ -z "$EDITORS" ] ; then - export EDITORS="ed vi vim" - fi - - export EDITOR="$(which $EDITORS 2>/dev/null | tail -n1)" - - export VISUAL="$EDITOR" - - # Set an alias for our editor of choice +_set_editor() { + export EDITOR="$1" + export VISUAL="$1" alias vi="$EDITOR" alias vim="$EDITOR" alias edit="$EDITOR" alias e="$EDITOR" } +set_editor() { + for editor in vim vi ed ; do + unalias "$editor" >/dev/null 2>&1 + e="$(command -v "$editor")" + if [ -n "$e" ] && [ -x "$e" ] ; then + _set_editor "$e" + fi + break + done +} set_pager() { - export PAGER="$(which cat more less 2>/dev/null | tail -n1)" + #shellcheck disable=SC2155 + export PAGER="$(command -v cat more less 2>/dev/null | tail -n1)" } set_lang() { export CHARSET="$1"; @@ -190,9 +195,9 @@ randmacgen() { } # First argument is the interface on which to scan ipv6_local_hosts() { -if ! [ -z $1 ] ; then +if [ -n "$1" ] ; then interface="$1" - ping6 -c 3 ff02::2%$interface \ + ping6 -c 3 "ff02::2%$interface" \ | grep 'bytes from' \ | awk '{print $4}' \ | sort \ @@ -230,12 +235,12 @@ set_my_title() { } _sys_memory() { - ! [ -e $(which vmstat) ] && return - if [ `uname` = "Linux" ] ; then + ! [ -e "$(command -v vmstat)" ] && return + if [ "$(uname)" = "Linux" ] ; then vmstat | tail -n1 | awk '{print $4/1024}' | sed -e's/\..*$//g' - elif [ `uname` = "FreeBSD" ] ; then + elif [ "$(uname)" = "FreeBSD" ] ; then vmstat | tail -n1 | awk '{print $5/1024}' | sed -e's/\..*$//g' - elif [ `uname` = "OpenBSD" ] ; then + elif [ "$(uname)" = "OpenBSD" ] ; then vmstat | sed -n '$p' | awk '{print $4}' | sed 's/M$//' fi } @@ -258,20 +263,24 @@ timestamp() { checkSSHAgent() { ssh_agent_conf="$HOME/.ssh/agent" if [ -e "$ssh_agent_conf" ] ; then - . "$ssh_agent_conf" + #shellcheck disable=SC1090 + . "$ssh_agent_conf" fi + #shellcheck disable=SC2009 if ! ps -eo pid | grep -q "$SSH_AGENT_PID" \ || ! [ -e "$ssh_agent_conf" ] \ || [ -z "$SSH_AGENT_PID" ] ; \ then - ssh-agent -s | grep -v echo > "$ssh_agent_conf" - . "$ssh_agent_conf" + ssh-agent -s | grep -v echo > "$ssh_agent_conf" + #shellcheck disable=SC1090 + . "$ssh_agent_conf" fi } passenv() { _f="$(mktemp)" pass "$@" > "$_f" + #shellcheck disable=SC1090 . "$_f" rm -f "$_f" } @@ -357,7 +366,7 @@ layout="select-layout $layout ';' set-window-option synchronize-panes on ';'" eval _tmux_session -s "\$session" -w "\$working_directory" -n "main" \ -- \ $(_tmux_servers_split_commands $servers) \ - $layout \ + "$layout" \ "\$@" \ } @@ -388,6 +397,7 @@ gpgTmpDir() { rm -rvf "$GNUPGHOME" fi + #shellcheck disable=SC2155 export GNUPGHOME="$(mktemp -d /dev/shm/GPG.XXXXXX)" set +x } @@ -467,7 +477,7 @@ pull() { shift; shift; ;; esac done - CMD="$(which curl wget fetch 2>/dev/null | tail -n1)" + CMD="$(command -v curl wget fetch 2>/dev/null | tail -n1)" case $CMD in *curl) "$CMD" "$url" > "$file" ;; @@ -482,9 +492,9 @@ pulldotfile() { } _make_ssh_dir_if_not_exists() { auth_keys="$HOME/.ssh/authorized_keys" - if ! [ -d $HOME/.ssh ] ; then mkdir $HOME/.ssh ; fi - chmod 700 $HOME/.ssh - chmod 600 $auth_keys + if ! [ -d "$HOME/.ssh" ] ; then mkdir "$HOME/.ssh" ; fi + chmod 700 "$HOME/.ssh" + chmod 600 "$auth_keys" } _pullkeys() { url="$1"; shift @@ -498,7 +508,7 @@ _pullkeys() { ssh-keygen -lf "$auth_keys" || \ mv "${auth_keys}.${_timestamp}" "$auth_keys" - chmod 600 $auth_keys + chmod 600 "$auth_keys" } pullkeys() { file="keys/$1"; shift; @@ -512,20 +522,20 @@ pullkeys_github() { } pulltermcolors() { file="$HOME/bin/terminal-colors" - if ! [ -d $HOME/bin ] ; then - mkdir $HOME/bin + if ! [ -d "$HOME/bin" ] ; then + mkdir "$HOME/bin" fi - pull -u ${UPDATE_URL}/util/terminal-colors -f $file - chmod +x $file + pull -u "${UPDATE_URL}/util/terminal-colors" -f "$file" + chmod +x "$file" } updatetmuxconf() { pulldotfile "tmux/tmux.conf" pulldotfile "tmux/tmux_helper.sh" - if ! [ -e $HOME/.tmux.conf.local ] && [ `id -u` -eq 0 ] ; then + if ! [ -e "$HOME/.tmux.conf.local" ] && [ "$(id -u)" -eq 0 ] ; then # If I ever change the color from green this will have to be updated I guess - grep 'green' $HOME/.tmux.conf | sed -e's/green/red/g' > $HOME/.tmux.conf.local - elif ! [ -e $HOME/.tmux.conf.local ] ; then - touch $HOME/.tmux.conf.local + grep 'green' "$HOME/.tmux.conf" | sed -e's/green/red/g' > "$HOME/.tmux.conf.local" + elif ! [ -e "$HOME/.tmux.conf.local" ] ; then + touch "$HOME/.tmux.conf.local" fi } updategitconf() { @@ -534,28 +544,29 @@ updategitconf() { } updateshell() { pulldotfile "mkshrc" - . $HOME/.mkshrc + #shellcheck disable=SC1090 + . "$HOME/.mkshrc" } updatevimrc() { OLDWD="$(pwd)" + #shellcheck disable=SC2164 cd "$HOME" git clone --recurse-submodules "$VIM_GIT_URL" .vim exit="$?" - cd $HOME/.vim || { echo "Cannot cd to .vim dir" ; return; } + cd "$HOME/.vim" || { echo "Cannot cd to .vim dir" ; return; } if [ "$exit" -gt 0 ] ; then git pull origin master git submodule update fi ./link-vimrc.sh - cd $OLDWD + #shellcheck disable=SC2164 + cd "$OLDWD" } updatevimrc_basic() { pull -u "$VIM_BASIC_URL" -f ~/.vimrc } - - SHELL_CONFIG=".mkshrc" COLOR=0 case "$TERM" in @@ -577,8 +588,9 @@ LOCAL="$HOME/${SHELL_CONFIG}.local" # functions if you do that # Load pre config -if [ -e $PRELOAD ] ; then - . $PRELOAD +if [ -e "$PRELOAD" ] ; then + #shellcheck disable=SC1090 + . "$PRELOAD" fi set_pager @@ -595,20 +607,22 @@ setsudoaliases # Nasty conditional for support of many different shells if - ! [ -z "$KSH_VERSION" ] || - [ $SHELL = "/bin/ksh" ] || - [ $SHELL = "/bin/mksh" ] || - [ $SHELL = "/usr/bin/mksh" ] || - [ $SHELL = "/usr/local/bin/mksh" ] + [ -n "$KSH_VERSION" ] || + [ "$SHELL" = "/bin/ksh" ] || + [ "$SHELL" = "/bin/mksh" ] || + [ "$SHELL" = "/usr/bin/mksh" ] || + [ "$SHELL" = "/usr/local/bin/mksh" ] then export HISTFILE="$HOME/.ksh_history" set -o emacs bind ^L=clear-screen + #shellcheck disable=SC2155 export PS1="$(whoami)@$(hostname) \$PWD \$ " -elif [ $SHELL = "/bin/sh" ] && [ "$(uname)" = "FreeBSD" ] ; then +elif [ "$SHELL" = "/bin/sh" ] && [ "$(uname)" = "FreeBSD" ] ; then set -o emacs + #shellcheck disable=SC2155 export PS1="$(whoami)"'@\h:\w \$ ' -elif ! [ -z "$BASH" ] ; then +elif [ -n "$BASH" ] ; then HISTFILE="$HOME/.bash_history" HISTSIZE=10000 HISTFILESIZE=10000 @@ -624,6 +638,6 @@ if [ $COLOR -eq 1 ] ; then fi # Include local configuration options -if [ -f $LOCAL ] ; then - . $LOCAL +if [ -f "$LOCAL" ] ; then + . "$LOCAL" fi diff --git a/shell/aliases b/shell/aliases index 143385b..a80dd22 100644 --- a/shell/aliases +++ b/shell/aliases @@ -72,27 +72,28 @@ setaliases() { } setcoloraliases() { - if [ `uname` = "Linux" ] ; then + if [ "$(uname)" = "Linux" ] ; then alias ls="ls --color=auto -F"; alias lm='ls -lh --color=force | more'; - elif [ `uname` = "FreeBSD" ] ; then + elif [ "$(uname)" = "FreeBSD" ] ; then alias ls="ls -FG"; alias lm='CLICOLOR_FORCE="1" ls -l | less -r'; fi if echo "color test" | grep -q --color=auto "color test" >/dev/null 2>&1 ; then alias grep="grep --color=auto"; - alias egrep='egrep --color=auto' - alias search='egrep --color=auto -rnI'; + alias egrep='grep -E --color=auto' + alias search='grep -E --color=auto -rnI'; fi - alias diff="$(which diff colordiff 2>/dev/null | tail -n 1)"; + #shellcheck disable=SC2139 + alias diff="$(command -v diff colordiff 2>/dev/null | tail -n 1)"; alias t='tree -CdL' } setsudoaliases() { # Prefer doas if available, as it's likely to be configured by me over sudo - _sudo="$(which doas sudo 2>/dev/null | sed 1q)" + _sudo="$(command -v doas sudo 2>/dev/null | sed 1q)" if [ -x "$_sudo" ] ; then # Package managers # Isn't it amazing how many distros you end up using? diff --git a/shell/ash b/shell/ash index dc8cce0..18c2cf2 100644 --- a/shell/ash +++ b/shell/ash @@ -11,7 +11,7 @@ case "$-" in setaliases export PS1="$ " - if [ $(id -u) -eq 0 ] ; then + if [ "$(id -u)" -eq 0 ] ; then export PS1="# " fi ;; diff --git a/shell/conf b/shell/conf index dd7e6e2..c6b78a4 100644 --- a/shell/conf +++ b/shell/conf @@ -20,8 +20,9 @@ LOCAL="$HOME/${SHELL_CONFIG}.local" # functions if you do that # Load pre config -if [ -e $PRELOAD ] ; then - . $PRELOAD +if [ -e "$PRELOAD" ] ; then + #shellcheck disable=SC1090 + . "$PRELOAD" fi set_pager @@ -38,20 +39,22 @@ setsudoaliases # Nasty conditional for support of many different shells if - ! [ -z "$KSH_VERSION" ] || - [ $SHELL = "/bin/ksh" ] || - [ $SHELL = "/bin/mksh" ] || - [ $SHELL = "/usr/bin/mksh" ] || - [ $SHELL = "/usr/local/bin/mksh" ] + [ -n "$KSH_VERSION" ] || + [ "$SHELL" = "/bin/ksh" ] || + [ "$SHELL" = "/bin/mksh" ] || + [ "$SHELL" = "/usr/bin/mksh" ] || + [ "$SHELL" = "/usr/local/bin/mksh" ] then export HISTFILE="$HOME/.ksh_history" set -o emacs bind ^L=clear-screen + #shellcheck disable=SC2155 export PS1="$(whoami)@$(hostname) \$PWD \$ " -elif [ $SHELL = "/bin/sh" ] && [ "$(uname)" = "FreeBSD" ] ; then +elif [ "$SHELL" = "/bin/sh" ] && [ "$(uname)" = "FreeBSD" ] ; then set -o emacs + #shellcheck disable=SC2155 export PS1="$(whoami)"'@\h:\w \$ ' -elif ! [ -z "$BASH" ] ; then +elif [ -n "$BASH" ] ; then HISTFILE="$HOME/.bash_history" HISTSIZE=10000 HISTFILESIZE=10000 @@ -67,6 +70,6 @@ if [ $COLOR -eq 1 ] ; then fi # Include local configuration options -if [ -f $LOCAL ] ; then - . $LOCAL +if [ -f "$LOCAL" ] ; then + . "$LOCAL" fi diff --git a/shell/notice b/shell/notice index 7f2eab0..f33b575 100644 --- a/shell/notice +++ b/shell/notice @@ -1,4 +1,4 @@ -# Copyright 2020 Mitchell Riedstra +# Copyright 2021 Mitchell Riedstra # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above diff --git a/shell/update b/shell/update index a130285..6d2f764 100644 --- a/shell/update +++ b/shell/update @@ -11,7 +11,7 @@ pull() { shift; shift; ;; esac done - CMD="$(which curl wget fetch 2>/dev/null | tail -n1)" + CMD="$(command -v curl wget fetch 2>/dev/null | tail -n1)" case $CMD in *curl) "$CMD" "$url" > "$file" ;; @@ -26,9 +26,9 @@ pulldotfile() { } _make_ssh_dir_if_not_exists() { auth_keys="$HOME/.ssh/authorized_keys" - if ! [ -d $HOME/.ssh ] ; then mkdir $HOME/.ssh ; fi - chmod 700 $HOME/.ssh - chmod 600 $auth_keys + if ! [ -d "$HOME/.ssh" ] ; then mkdir "$HOME/.ssh" ; fi + chmod 700 "$HOME/.ssh" + chmod 600 "$auth_keys" } _pullkeys() { url="$1"; shift @@ -42,7 +42,7 @@ _pullkeys() { ssh-keygen -lf "$auth_keys" || \ mv "${auth_keys}.${_timestamp}" "$auth_keys" - chmod 600 $auth_keys + chmod 600 "$auth_keys" } pullkeys() { file="keys/$1"; shift; @@ -56,20 +56,20 @@ pullkeys_github() { } pulltermcolors() { file="$HOME/bin/terminal-colors" - if ! [ -d $HOME/bin ] ; then - mkdir $HOME/bin + if ! [ -d "$HOME/bin" ] ; then + mkdir "$HOME/bin" fi - pull -u ${UPDATE_URL}/util/terminal-colors -f $file - chmod +x $file + pull -u "${UPDATE_URL}/util/terminal-colors" -f "$file" + chmod +x "$file" } updatetmuxconf() { pulldotfile "tmux/tmux.conf" pulldotfile "tmux/tmux_helper.sh" - if ! [ -e $HOME/.tmux.conf.local ] && [ `id -u` -eq 0 ] ; then + if ! [ -e "$HOME/.tmux.conf.local" ] && [ "$(id -u)" -eq 0 ] ; then # If I ever change the color from green this will have to be updated I guess - grep 'green' $HOME/.tmux.conf | sed -e's/green/red/g' > $HOME/.tmux.conf.local - elif ! [ -e $HOME/.tmux.conf.local ] ; then - touch $HOME/.tmux.conf.local + grep 'green' "$HOME/.tmux.conf" | sed -e's/green/red/g' > "$HOME/.tmux.conf.local" + elif ! [ -e "$HOME/.tmux.conf.local" ] ; then + touch "$HOME/.tmux.conf.local" fi } updategitconf() { @@ -78,24 +78,25 @@ updategitconf() { } updateshell() { pulldotfile "mkshrc" - . $HOME/.mkshrc + #shellcheck disable=SC1090 + . "$HOME/.mkshrc" } updatevimrc() { OLDWD="$(pwd)" + #shellcheck disable=SC2164 cd "$HOME" git clone --recurse-submodules "$VIM_GIT_URL" .vim exit="$?" - cd $HOME/.vim || { echo "Cannot cd to .vim dir" ; return; } + cd "$HOME/.vim" || { echo "Cannot cd to .vim dir" ; return; } if [ "$exit" -gt 0 ] ; then git pull origin master git submodule update fi ./link-vimrc.sh - cd $OLDWD + #shellcheck disable=SC2164 + cd "$OLDWD" } updatevimrc_basic() { pull -u "$VIM_BASIC_URL" -f ~/.vimrc } - - diff --git a/shell/utils b/shell/utils index 5a74a58..4e60989 100644 --- a/shell/utils +++ b/shell/utils @@ -12,23 +12,27 @@ set_go() { export PATH="$_gobin:$PATH" fi } -set_editor() { - if [ -z "$EDITORS" ] ; then - export EDITORS="ed vi vim" - fi - - export EDITOR="$(which $EDITORS 2>/dev/null | tail -n1)" - - export VISUAL="$EDITOR" - - # Set an alias for our editor of choice +_set_editor() { + export EDITOR="$1" + export VISUAL="$1" alias vi="$EDITOR" alias vim="$EDITOR" alias edit="$EDITOR" alias e="$EDITOR" } +set_editor() { + for editor in vim vi ed ; do + unalias "$editor" >/dev/null 2>&1 + e="$(command -v "$editor")" + if [ -n "$e" ] && [ -x "$e" ] ; then + _set_editor "$e" + fi + break + done +} set_pager() { - export PAGER="$(which cat more less 2>/dev/null | tail -n1)" + #shellcheck disable=SC2155 + export PAGER="$(command -v cat more less 2>/dev/null | tail -n1)" } set_lang() { export CHARSET="$1"; @@ -51,9 +55,9 @@ randmacgen() { } # First argument is the interface on which to scan ipv6_local_hosts() { -if ! [ -z $1 ] ; then +if [ -n "$1" ] ; then interface="$1" - ping6 -c 3 ff02::2%$interface \ + ping6 -c 3 "ff02::2%$interface" \ | grep 'bytes from' \ | awk '{print $4}' \ | sort \ @@ -91,12 +95,12 @@ set_my_title() { } _sys_memory() { - ! [ -e $(which vmstat) ] && return - if [ `uname` = "Linux" ] ; then + ! [ -e "$(command -v vmstat)" ] && return + if [ "$(uname)" = "Linux" ] ; then vmstat | tail -n1 | awk '{print $4/1024}' | sed -e's/\..*$//g' - elif [ `uname` = "FreeBSD" ] ; then + elif [ "$(uname)" = "FreeBSD" ] ; then vmstat | tail -n1 | awk '{print $5/1024}' | sed -e's/\..*$//g' - elif [ `uname` = "OpenBSD" ] ; then + elif [ "$(uname)" = "OpenBSD" ] ; then vmstat | sed -n '$p' | awk '{print $4}' | sed 's/M$//' fi } @@ -119,20 +123,24 @@ timestamp() { checkSSHAgent() { ssh_agent_conf="$HOME/.ssh/agent" if [ -e "$ssh_agent_conf" ] ; then - . "$ssh_agent_conf" + #shellcheck disable=SC1090 + . "$ssh_agent_conf" fi + #shellcheck disable=SC2009 if ! ps -eo pid | grep -q "$SSH_AGENT_PID" \ || ! [ -e "$ssh_agent_conf" ] \ || [ -z "$SSH_AGENT_PID" ] ; \ then - ssh-agent -s | grep -v echo > "$ssh_agent_conf" - . "$ssh_agent_conf" + ssh-agent -s | grep -v echo > "$ssh_agent_conf" + #shellcheck disable=SC1090 + . "$ssh_agent_conf" fi } passenv() { _f="$(mktemp)" pass "$@" > "$_f" + #shellcheck disable=SC1090 . "$_f" rm -f "$_f" } @@ -218,7 +226,7 @@ layout="select-layout $layout ';' set-window-option synchronize-panes on ';'" eval _tmux_session -s "\$session" -w "\$working_directory" -n "main" \ -- \ $(_tmux_servers_split_commands $servers) \ - $layout \ + "$layout" \ "\$@" \ } @@ -249,6 +257,7 @@ gpgTmpDir() { rm -rvf "$GNUPGHOME" fi + #shellcheck disable=SC2155 export GNUPGHOME="$(mktemp -d /dev/shm/GPG.XXXXXX)" set +x } -- cgit v1.2.3