Script+Shell: fixed and standardize PATH checking

We redirect stderr to NULL since POSIX says it may be used (no modern shell
seems to be using it).

If more than 1 program is checked for availability, we use a function.

Emacs snippet make it uniform.

Error message is output on stderr and is always the same.
master
Pierre Neidhardt 2014-01-03 19:13:07 +01:00
parent 9cb4840fca
commit 9233dad745
20 changed files with 85 additions and 97 deletions

View File

@ -56,7 +56,7 @@ fi
unset TEXDIR
## Make 'less' more friendly for non-text input files, see lesspipe(1).
command -v lesspipe >/dev/null && eval "$(lesspipe)"
command -v lesspipe >/dev/null 2>&1 && eval "$(lesspipe)"
## Manpage.
export MANPAGER="less -s"
@ -94,12 +94,12 @@ fi
## Default text editor
EDITOR="nano"
command -v vim >/dev/null && EDITOR="vim"
command -v emacs >/dev/null && EDITOR="emacs"
command -v vim >/dev/null 2>&1 && EDITOR="vim"
command -v emacs >/dev/null 2>&1 && EDITOR="emacs"
GIT_EDITOR="$EDITOR"
## 'em' is a script for emacsclient. See 'homeinit'.
if command -v em >/dev/null; then
## 'em' is a script for emacsclient. See '.scripts/em'.
if command -v em >/dev/null 2>&1; then
EDITOR='em'
GIT_EDITOR='emc'
fi
@ -108,14 +108,12 @@ export EDITOR
export GIT_EDITOR
## Internet Browser
command -v luakit >/dev/null && export BROWSER="luakit"
command -v dwb >/dev/null && export BROWSER="dwb"
command -v luakit >/dev/null 2>&1 && export BROWSER="luakit"
command -v dwb >/dev/null 2>&1 && export BROWSER="dwb"
## SSH-Agent
## WARNING: this is insecure on machines where someone else has root access.
if command -v ssh-agent >/dev/null; then
eval "$(ssh-agent)"
fi
command -v ssh-agent >/dev/null 2>&1 && eval "$(ssh-agent)"
## Kill ssh-agent on session end.
trap 'test -n "$SSH_AGENT_PID" && eval $(ssh-agent -k)' 0

View File

@ -48,9 +48,9 @@ if ! $OPT_PROCESS; then
exit
fi
if ! command -v ffmpeg >/dev/null; then
echo "ffmpeg required."
exit
if ! command -v ffmpeg >/dev/null 2>&1; then
echo >&2 "'ffmpeg' not found in PATH. Exiting."
exit 1
fi
_transcode ()

View File

@ -63,8 +63,8 @@ fi
## We need 'realpath' since we need to work on the real path for the input
## files/folders and the archive location. Otherwise we could have troubles with
## circular symlinks and so on.
if ! command -v realpath >/dev/null; then
echo >&2 "'realpath' is required."
if ! command -v realpath >/dev/null 2>&1; then
echo >&2 "'realpath' not found in PATH. Exiting."
exit 1
fi

View File

@ -19,7 +19,7 @@ fi
AGENT="curl -s"
if ! command -v curl >/dev/null; then
if ! command -v curl >/dev/null 2>&1; then
AGENT="wget -q -O - "
fi

View File

@ -39,14 +39,11 @@ if [ $# -eq 0 ]; then
exit 1
fi
if ! command -v readcd >/dev/null; then
echo >&2 "readcd required."
exit
if ! command -v readcd >/dev/null 2>&1; then
echo >&2 "'readcd' not found in PATH. Exiting."
exit 1
fi
## Fast?
readcd dev=$DRIVE f="$1.iso" -v retries=5 timeout=30 -noerror

View File

@ -5,13 +5,13 @@ TERMCMD=xterm
case ${DWB_SCHEME} in
mailto)
! command -v xclip >/dev/null && exit
! command -v xclip >/dev/null 2>&1 && exit
echo "${DWB_URI}" | sed -e 's/mailto://' -e 's/?/ /' | xclip -i ;;
ftp)
! command -v $TERMCMD >/dev/null && exit
! command -v lftp >/dev/null && exit
! command -v $TERMCMD >/dev/null 2>&1 && exit
! command -v lftp >/dev/null 2>&1 && exit
$TERMCMD -e "lftp ${DWB_URI}";;
*)
! command -v xdg-open >/dev/null && exit
! command -v xdg-open >/dev/null 2>&1 && exit
xdg-open "${DWB_URI}";;
esac

View File

@ -55,7 +55,7 @@ if [ $# -eq 0 ]; then
exit 1
fi
command -v atool >/dev/null && echo "You should use atool instead." && echo
command -v atool >/dev/null 2>&1 && echo "You should use atool instead." && echo
_extract()
{

View File

@ -43,13 +43,11 @@ if [ $# -eq 0 ]; then
exit 1
fi
if ! command -v git >/dev/null; then
echo >&2 "git required."
exit
if ! command -v git >/dev/null 2>&1; then
echo >&2 "'git' not found in PATH. Exiting."
exit 1
fi
for i; do
git clone git@$DOMAIN:/$USER/$i
done

View File

@ -52,7 +52,7 @@ fi
## Clean trash if possible.
echo
echo "==> Emptying trash."
if ! command -v trash-empty >/dev/null; then
if ! command -v trash-empty >/dev/null 2>&1; then
echo ":: 'trash-empty' not installed, you have to clean other drives manually."
rm -rvf "$HOME/.local/share/Trash"
else

View File

@ -33,7 +33,7 @@ if [ -d "$SOURCEDIR/contacts" ]; then
echo
fi
if command -v mutt >/dev/null; then
if command -v mutt >/dev/null 2>&1; then
echo "==> Mutt"
[ -d "$SOURCEDIR" ] && ln -snf "$SOURCEDIR/mails" "$HOME/.mutt.d"
mkdir -p "$HOME/.cache/mutt/hcache"
@ -46,7 +46,7 @@ if [ -d "$SOURCEDIR/todo" ]; then
echo
fi
if command -v rtorrent >/dev/null; then
if command -v rtorrent >/dev/null 2>&1; then
echo "==> rtorrent"
## We need the evaluation of the result, otherwise the variables will not
## get expanded, should it be '~' or "$HOME".
@ -55,7 +55,7 @@ if command -v rtorrent >/dev/null; then
fi
## Luakit -- Install the adblock modules
if command -v luakit >/dev/null; then
if command -v luakit >/dev/null 2>&1; then
echo "==> Luakit"
git clone https://github.com/Plaque-fcc/luakit-adblock/ "$HOME/luakit-adblock"
cp -fv ~/luakit-adblock/*.lua "$XDG_CONFIG_HOME/luakit"
@ -72,7 +72,7 @@ if command -v luakit >/dev/null; then
fi
## dwb -- Install extensions.
if command -v dwb >/dev/null && [ ! -f "$XDG_DATA_HOME/dwb/extensions/adblock_subscriptions" ] ; then
if command -v dwb >/dev/null 2>&1 && [ ! -f "$XDG_DATA_HOME/dwb/extensions/adblock_subscriptions" ] ; then
echo "==> dwb"
dwbem -Ni adblock_subscriptions
dwbem -Ni navtools
@ -97,7 +97,7 @@ if [ -d "$SOURCEDIR" ]; then
fi
## News
if command -v newsbeuter >/dev/null; then
if command -v newsbeuter >/dev/null 2>&1; then
echo "==> Newsbeuter"
mkdir -p "$XDG_DATA_HOME/newsbeuter"
[ -d "$SOURCEDIR" ] && ln -sf "$SOURCEDIR/news/urls" "$XDG_CONFIG_HOME/newsbeuter/"

View File

@ -34,7 +34,7 @@ _mount ()
fi
cdemu load $DEVICE "$1"
if [ $? -ne 0 ] && command -v fuseiso >/dev/null; then
if [ $? -ne 0 ] && command -v fuseiso >/dev/null 2>&1; then
echo "Falling back to fuseiso."
# LIST="$(mount | awk -v root="$FUSEROOT" '$1 == "fuseiso" && $3 ~ root {print $3}')"
@ -83,16 +83,6 @@ _umount ()
fi
}
if ! command -v udiskie >/dev/null; then
echo "udiskie required."
exit
fi
if ! command -v cdemu >/dev/null; then
echo "cdemu required."
exit
fi
_printhelp ()
{
cat<<EOF
@ -134,13 +124,18 @@ if [ $# -eq 0 ]; then
exit 1
fi
if ! command -v realpath >/dev/null; then
echo >&2 "'realpath' is required."
exit 1
fi
check()
{
for i ; do
if ! command -v $i >/dev/null 2>&1; then
echo >&2 "'$i' not found in PATH. Exiting."
exit 1
fi
done
}
## realpath is needed to check if image was already mounted or not.
check udiskie cdemu realpath
if $OPT_UNMOUNT; then
for i ; do
_umount "$(realpath "$i")"

View File

@ -27,7 +27,7 @@ Usage: ${1##*/} [OPTIONS]
Connect to a wireless network.
-c: Decrypt $WSFILE with ccrypt.
-c: Decrypt $WSFILE with ccrypt's ccat command'.
-d: Use DHCP.
-h: Show this help.
-t ITER: Timeout (default: $TIMEOUT_LIMIT).
@ -73,10 +73,11 @@ if [ $(id -u) -ne 0 ]; then
fi
if [ $USE_CRYPT -eq 1 ]; then
if ! command -v ccrypt >/dev/null; then
echo "You need to have 'ccrypt' installed."
exit
if ! command -v ccat >/dev/null 2>&1; then
echo >&2 "'ccat' not found in PATH. Exiting."
exit 1
fi
WPA_SUPPLICANT_CONF="$(ccat "$WSFILE")"
if [ $? -ne 0 ]; then
echo "Failed to decrypt wpa_supplicant config from $WSFILE."

View File

@ -12,9 +12,9 @@ EOF
fi
## Network operations
if ! command -v nmap >/dev/null; then
echo "nmap required."
exit
if ! command -v nmap >/dev/null 2>&1; then
echo >&2 "'nmap' not found in PATH. Exiting."
exit 1
fi
echo "$1"

View File

@ -47,7 +47,10 @@ if [ $# -eq 0 ]; then
exit 1
fi
! command -v pacman >/dev/null && echo "You need pacman to run this script." && exit 1
if ! command -v pacman >/dev/null 2>&1; then
echo >&2 "'pacman' not found in PATH. Exiting."
exit 1
fi
PKGLIST="$(mktemp)"
FILE="$(mktemp)"

View File

@ -87,7 +87,7 @@ done
shift $(($OPTIND - 1))
## If expac is available, use it since it is much faster.
if ! command -v expac >/dev/null; then
if ! command -v expac >/dev/null 2>&1; then
expac -HK -$cmd '%6m %n' "$@" | awk -v sum=$opt_total '{sub(/[\.,]00/,""); if(sum) total+=$1; printf("%6s %s %s\n",$1,$2,$3)} END {if(sum) print total, "KiB TOTAL"}' | ($opt_sort || cat)
exit
fi
@ -109,7 +109,7 @@ if [ $# -eq 0 ]; then
exit 1
fi
if ! command -v pacman >/dev/null; then
if ! command -v pacman >/dev/null 2>&1; then
echo "You need the pacman package manager." >&2
exit 1
fi

View File

@ -8,7 +8,7 @@ PKG_ROOT="$HOME/.pkg"
mkdir -p "$PKG_ROOT"
## Arch Linux
if command -v pacman >/dev/null; then
if command -v pacman >/dev/null 2>&1; then
## Note: the "-n" option was added between 2012 and 2013.
pacman -Qnq | sort >"$PKG_ROOT/arch-official-${HOST}"
pacman -Qmq | sort >"$PKG_ROOT/arch-aur-${HOST}"

View File

@ -18,10 +18,9 @@ if [ $# -eq 0 ] || [ "$1" = "-h" ]; then
exit
fi
if ! command -v indent >/dev/null; then
echo "Please install 'indent'."
exit
if ! command -v indent >/dev/null 2>&1; then
echo >&2 "'indent' not found in PATH. Exiting."
exit 1
fi
CMD="indent -i4 -ppi4 -bli0 -cli4 -nut"

View File

@ -1,10 +1,10 @@
#!/bin/sh
## This is a compatibility wrapper for 'realpath'.
if command -v greadlink >/dev/null; then
if command -v greadlink >/dev/null 2>&1; then
## On BSD systems, we might have greadlink
greadlink -f "$@"
elif command -v readlink >/dev/null && [ "$(uname)" = "Linux" ]; then
elif command -v readlink >/dev/null 2>&1 && [ "$(uname)" = "Linux" ]; then
readlink -f "$@"
else
## Dirty fallback

View File

@ -48,10 +48,10 @@ alias grep='grep --color=auto'
alias nox='find . -type f -exec chmod a-x {} +'
## Get your external IP.
command -v curl >/dev/null && alias myip='curl ifconfig.me'
command -v curl >/dev/null 2>&1 && alias myip='curl ifconfig.me'
## Network discovery.
command -v nmap >/dev/null && alias network-discover='nmap -sP "192.168.0.*"'
command -v nmap >/dev/null 2>&1 && alias network-discover='nmap -sP "192.168.0.*"'
## Prevents accidentally clobbering files.
alias mkdir='mkdir -p'
@ -61,7 +61,7 @@ alias cls='printf "\ec"'
## Shutdown / Reboot.
## If you do not want to get prompted for your password, configure sudo properly.
if command -v systemctl >/dev/null; then
if command -v systemctl >/dev/null 2>&1; then
alias halt='systemctl poweroff'
else
alias halt='sudo halt -p'
@ -69,10 +69,10 @@ fi
## Start X and lock current terminal (press ENTER when back in terminal to
## prompt for password). Useless if X is running on the same TTY.
command -v vlock >/dev/null && alias sx='startx & vlock'
command -v vlock >/dev/null 2>&1 && alias sx='startx & vlock'
## Zathura -- Fork to background.
command -v zathura >/dev/null && alias zat='zathura --fork'
command -v zathura >/dev/null 2>&1 && alias zat='zathura --fork'
## Xrandr
alias xmorphnone='xrandr --output $(xrandr | grep "\<connected\>" | cut -f1 -d" ") --transform none'
@ -83,10 +83,10 @@ alias xmorphzi='xrandr --output $(xrandr | grep "\<connected\>" | cut -f1 -d" ")
alias xres='xrandr --output $(xrandr | awk '"'"'NR==2 {print $1;exit}'"'"') --auto && xrandr -s $(xrandr | awk '"'"'/^ / {print $1;exit}'"'"')'
## Udiskie
command -v udiskie-umount >/dev/null && alias uua='udiskie-umount -a'
command -v udiskie-umount >/dev/null 2>&1 && alias uua='udiskie-umount -a'
## Emacs
command -v em >/dev/null && alias emacs-reload="emacsclient -e '(kill-emacs)' >/dev/null 2>&1; emacs --daemon"
command -v em >/dev/null 2>&1 && alias emacs-reload="emacsclient -e '(kill-emacs)' >/dev/null 2>&1 2>&1; emacs --daemon"
##==============================================================================
## Zsh power aliases
@ -105,19 +105,16 @@ command -v em >/dev/null && alias emacs-reload="emacsclient -e '(kill-emacs)' >/
##==============================================================================
## Pacman aliases
##==============================================================================
if command -v "pacman" >/dev/null; then
PACMAN_FRONTEND='pacman'
## The eval trick lets us define the alias after the variable has been expanded.
eval 'alias pc="sudo $PACMAN_FRONTEND -Sc"'
eval 'alias pi="sudo $PACMAN_FRONTEND -S --needed"'
eval 'alias pqi="$PACMAN_FRONTEND -Qi"'
eval 'alias pqo="$PACMAN_FRONTEND -Qo"'
eval 'alias pqs="$PACMAN_FRONTEND -Qs"'
eval 'alias pr="sudo $PACMAN_FRONTEND -Rs"'
eval 'alias psi="$PACMAN_FRONTEND -Si"'
eval 'alias pss="$PACMAN_FRONTEND -Ss"'
eval 'alias pu="sudo $PACMAN_FRONTEND -Syu"'
if command -v "pacman" >/dev/null 2>&1; then
alias pc='sudo pacman -Sc'
alias pi='sudo pacman -S --needed'
alias pqi='pacman -Qi'
alias pqo='pacman -Qo'
alias pqs='pacman -Qs'
alias pr='sudo pacman -Rs'
alias psi='pacman -Si'
alias pss='pacman -Ss'
alias pu='sudo pacman -Syu'
fi
##==============================================================================

View File

@ -45,7 +45,7 @@ search()
## Vim-only: search the vim reference manual for a keyword.
## Usage: :h <keyword>
if command -v vim >/dev/null; then
if command -v vim >/dev/null 2>&1; then
:h()
{
vim --cmd ":silent help $@" --cmd "only";
@ -100,7 +100,7 @@ termsupport()
done | sort -nk2
}
if command -v highlight >/dev/null ; then
if command -v highlight >/dev/null 2>&1; then
hlt-viewer ()
{
local SAMPLE DUMMY
@ -163,12 +163,12 @@ echokeymap ()
typex()
{
local xpath
if [ $# -ne 1 ] || ! command -v "$1" >/dev/null; then
if [ $# -ne 1 ] || ! command -v "$1" >/dev/null 2>&1; then
echo "Provide an existing command as parameter."
return
fi
if [ -z "$(command -v "$1" | grep ^/)" ]; then
if [ -z "$(command -v "$1" 2>/dev/null | grep ^/)" ]; then
type "$1"
return
fi
@ -276,7 +276,7 @@ xmultioff ()
## Pacman functions
##==============================================================================
if command -v pacman >/dev/null; then
if command -v pacman >/dev/null 2>&1; then
## Get detailed file list for the specified packages, omitting folders.
pql ()
{