Skip to content

Commit 8c69771

Browse files
author
Noah Gorny
authored
Merge pull request #1938 from gaelicWizard/_command_exists
Use `_command_exists` everywhere
2 parents 4700d7b + fb6e05d commit 8c69771

36 files changed

+163
-96
lines changed

aliases/available/apt.aliases.bash

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
# shellcheck shell=bash
22
#
33
# -binaryanomaly
44

@@ -8,7 +8,8 @@ about-alias 'Apt and dpkg aliases for Ubuntu and Debian distros.'
88
# set apt aliases
99
function _set_pkg_aliases()
1010
{
11-
if [ -x $(which apt) ]; then
11+
if _command_exists apt
12+
then
1213
alias apts='apt-cache search'
1314
alias aptshow='apt-cache show'
1415
alias aptinst='sudo apt-get install -V'

aliases/available/curl.aliases.bash

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ about-alias 'Curl aliases for convenience.'
66
# set apt aliases
77
function _set_pkg_aliases()
88
{
9-
if [ -x $(which curl) ]; then
9+
if _command_exists curl
10+
then
1011
# follow redirects
1112
alias cl='curl -L'
1213
# follow redirects, download as original name

aliases/available/general.aliases.bash

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ then
3030
alias grep='grep --color=auto'
3131
fi
3232

33-
if which gshuf &> /dev/null
33+
if _command_exists gshuf
3434
then
3535
alias shuf=gshuf
3636
fi
@@ -65,7 +65,7 @@ alias -- -='cd -' # Go back
6565
alias h='history'
6666

6767
# Tree
68-
if [ ! -x "$(which tree 2>/dev/null)" ]
68+
if ! _command_exists tree
6969
then
7070
alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
7171
fi

aliases/available/vim.aliases.bash

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
cite 'about-alias'
33
about-alias 'vim abbreviations'
44

5-
VIM=$(command -v vim)
6-
GVIM=$(command -v gvim)
7-
MVIM=$(command -v mvim)
8-
9-
if [[ -n $VIM ]]; then
5+
if _command_exists vim; then
106
alias v='$VIM'
117
# open the vim help in fullscreen incorporated from
128
# https://stackoverflow.com/a/4687513
@@ -17,9 +13,9 @@ fi
1713
# http://stackoverflow.com/questions/936501/let-gvim-always-run-a-single-instancek
1814
case $OSTYPE in
1915
darwin*)
20-
[[ -n $MVIM ]] && function mvimt { command mvim --remote-tab-silent "$@" || command mvim "$@"; }
16+
_command_exists mvim && function mvimt { command mvim --remote-tab-silent "$@" || command mvim "$@"; }
2117
;;
2218
*)
23-
[[ -n $GVIM ]] && function gvimt { command gvim --remote-tab-silent "$@" || command gvim "$@"; }
19+
_command_exists gvim && function gvimt { command gvim --remote-tab-silent "$@" || command gvim "$@"; }
2420
;;
2521
esac

bash_it.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ if [ -e "$HOME/.jekyllconfig" ]; then
145145
fi
146146

147147
# BASH_IT_RELOAD_LEGACY is set.
148-
if ! command -v reload &> /dev/null && [ -n "${BASH_IT_RELOAD_LEGACY:-}" ]; then
148+
if ! _command_exists reload && [[ -n "${BASH_IT_RELOAD_LEGACY:-}" ]]; then
149149
case $OSTYPE in
150150
darwin*)
151151
alias reload='source ~/.bash_profile'

clean_files.txt

+10
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,25 @@ completion/available/wpscan.completion.bash
7777
# plugins
7878
#
7979
plugins/available/alias-completion.plugin.bash
80+
plugins/available/autojump.plugin.bash
8081
plugins/available/basher.plugin.bash
8182
plugins/available/cmd-returned-notify.plugin.bash
83+
plugins/available/direnv.plugin.bash
8284
plugins/available/docker-machine.plugin.bash
8385
plugins/available/git.plugin.bash
8486
plugins/available/go.plugin.bash
8587
plugins/available/goenv.plugin.bash
8688
plugins/available/history-search.plugin.bash
8789
plugins/available/history-substring-search.plugin.bash
8890
plugins/available/history.plugin.bash
91+
plugins/available/hub.plugin.bash
92+
plugins/available/jump.plugin.bash
93+
plugins/available/node.plugin.bash
94+
plugins/available/nodenv.plugin.bash
95+
plugins/available/plenv.plugin.bash
96+
plugins/available/pyenv.plugin.bash
97+
plugins/available/rbenv.plugin.bash
98+
plugins/available/textmate.plugin.bash
8999
plugins/available/xterm.plugin.bash
90100

91101
# tests
+6-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
[[ -x "$(which aws_completer)" ]] && complete -C "$(which aws_completer)" aws
1+
# shellcheck shell=bash
2+
3+
if _command_exists aws_completer
4+
then
5+
complete -C "$(command -v aws_completer)" aws
6+
fi

completion/available/consul.completion.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ cite "about-completion"
33
about-completion "Hashicorp consul completion"
44

55
if _command_exists consul; then
6-
complete -C "$(which consul)" consul
6+
complete -C "$(command -v consul)" consul
77
fi

completion/available/docker-compose.completion.bash

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ _docker_compose() {
676676
done
677677

678678
local completions_func=_docker_compose_${command//-/_}
679-
declare -F $completions_func >/dev/null && $completions_func
679+
_is_function $completions_func && $completions_func
680680

681681
eval "$previous_extglob_setting"
682682
return 0

completion/available/git_flow.completion.bash

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ __git_flow_list_hotfixes ()
172172
}
173173

174174
# temporarily wrap __git_find_on_cmdline() for backwards compatibility
175-
if [ -z "`type -t __git_find_subcommand`" ]; then
175+
if ! _command_exists __git_find_subcommand
176+
then
176177
alias __git_find_subcommand=__git_find_on_cmdline
177178
fi

completion/available/git_flow_avh.completion.bash

+2-1
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ __git_flow_list_branches ()
505505
}
506506

507507
# alias __git_find_on_cmdline for backwards compatibility
508-
if [ -z "`type -t __git_find_on_cmdline`" ]; then
508+
if ! _command_exists __git_find_on_cmdline
509+
then
509510
alias __git_find_on_cmdline=__git_find_subcommand
510511
fi

completion/available/gradle.completion.bash

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ __gradle-set-cache-name() {
5858

5959
__gradle-set-files-checksum() {
6060
# Cache MD5 sum of all Gradle scripts and modified timestamps
61-
if builtin command -v md5 > /dev/null; then
61+
if _command_exists md5; then
6262
gradle_files_checksum=$(md5 -q -s "$(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null)")
63-
elif builtin command -v md5sum > /dev/null; then
63+
elif _command_exists md5sum; then
6464
gradle_files_checksum=$(cat "$cache_dir/$cache_name" | xargs ls -o 2>/dev/null | md5sum | awk '{print $1}')
6565
else
6666
echo "Cannot generate completions as neither md5 nor md5sum exist on \$PATH"

completion/available/hub.completion.bash

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424

2525
# If there is no git tab completion, but we have the _completion loader try to load it
26-
if ! declare -F _git > /dev/null && declare -F _completion_loader > /dev/null; then
26+
if ! _is_function _git && _is_function _completion_loader; then
2727
_completion_loader git
2828
fi
2929

3030
# Check that git tab completion is available and we haven't already set up completion
31-
if declare -F _git > /dev/null && ! declare -F __git_list_all_commands_without_hub > /dev/null; then
31+
if _is_function _git && ! _is_function __git_list_all_commands_without_hub; then
3232
# Duplicate and rename the 'list_all_commands' function
3333
eval "$(declare -f __git_list_all_commands | \
3434
sed 's/__git_list_all_commands/__git_list_all_commands_without_hub/')"
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/bash
22

3-
if command -v laravel > /dev/null; then
4-
__laravel_completion() {
3+
if _command_exists laravel
4+
then
5+
function __laravel_completion()
6+
{
57
local OPTS=("-h --help -q --quiet --ansi --no-ansi -n --no-interaction -v -vv -vvv --verbose help list new")
68
COMPREPLY=()
79
for _opt_ in ${OPTS[@]}; do
810
if [[ "$_opt_" == "$2"* ]]; then
911
COMPREPLY+=("$_opt_")
1012
fi
1113
done
12-
}
14+
}
1315

1416
complete -F __laravel_completion laravel
1517
fi
+6-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
[[ -x "$(which pew)" ]] && source "$(pew shell_config)"
1+
# shellcheck shell=bash
2+
3+
if _command_exists pew
4+
then
5+
source "$(pew shell_config)"
6+
fi

completion/available/sqlmap.completion.bash

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
#!/bin/bash
1+
# shellcheck shell=bash
22

33
# ---------------------------------------------------------------------------+
44
# |
5-
# Thanks to Alexander Korznikov |
5+
# Thanks to Alexander Korznikov |
66
# http://www.korznikov.com/2014/12/bash-tab-completion-for-awesome-tool.html |
77
# |
88
# ---------------------------------------------------------------------------+
99

10-
if command -v sqlmap > /dev/null; then
10+
if _command_exists sqlmap
11+
then
1112

12-
_sqlmap()
13+
function _sqlmap()
1314
{
1415
local cur prev
1516

1617
COMPREPLY=()
17-
cur=$(_get_cword)
18-
prev=$(_get_pword)
18+
cur="$(_get_cword)"
19+
prev="$(_get_pword)"
1920

2021
case $prev in
2122

@@ -143,7 +144,7 @@ if command -v sqlmap > /dev/null; then
143144
--mobile --page-rank --purge-output --smart \
144145
--sqlmap-shell --wizard '
145146
COMPREPLY=( $( \
146-
(while read -d ' ' i; do
147+
(while read -d ' ' i; do
147148
[[ -z "$i" || "${onlyonce/ ${i%% *} / }" == "$onlyonce" ]] &&
148149
continue
149150
# flatten array with spaces on either side,
@@ -152,7 +153,7 @@ if command -v sqlmap > /dev/null; then
152153
COMPREPLY=" ${COMPREPLY[@]} "
153154
# remove word from list of completions
154155
COMPREPLY=( ${COMPREPLY/ ${i%% *} / } )
155-
done
156+
done
156157
printf '%s ' "${COMPREPLY[@]}") <<<"${COMP_WORDS[@]}"
157158
) )
158159

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
if [[ -x "$(which travis)" ]]; then
2-
__TRAVIS_COMPLETION_SCRIPT="${TRAVIS_CONFIG_PATH:-${HOME}/.travis}/travis.sh"
3-
[[ -f "${__TRAVIS_COMPLETION_SCRIPT}" ]] && source "${__TRAVIS_COMPLETION_SCRIPT}"
1+
# shellcheck shell=bash
2+
3+
if _command_exists travis
4+
then
5+
if [[ -s "${__TRAVIS_COMPLETION_SCRIPT:=${TRAVIS_CONFIG_PATH:-${HOME}/.travis}/travis.sh}" ]]
6+
then
7+
source "${__TRAVIS_COMPLETION_SCRIPT}"
8+
fi
49
unset __TRAVIS_COMPLETION_SCRIPT
510
fi

completion/available/wpscan.completion.bash

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# shellcheck shell=bash
22

3-
if command -v wpscan > /dev/null; then
4-
__wpscan_completion() {
3+
if _command_exists wpscan; then
4+
function __wpscan_completion() {
55
local OPTS=("--help --hh --version --url --ignore-main-redirect --verbose --output --format --detection-mode --scope --headers --user-agent --vhost --random-user-agent --user-agents-list --http-auth --max-threads --throttle --request-timeout --connect-timeout --disable-tlc-checks --proxy --proxy-auth --cookie-string --cookie-jar --cache-ttl --clear-cache --server --cache-dir --update --no-update --wp-content-dir --wp-plugins-dir --wp-version-detection --main-theme-detection --enumerate --exclude-content-based --plugins-list --plugins-detection --plugins-version-all --plugins-version-detection --themes-list --themes-detection --themes-version-all --themes-version-detection --timthumbs-list --timthumbs-detection --config-backups-list --config-backups-detection --db-exports-list --db-exports-detection --medias-detection --users-list --users-detection --passwords --usernames --multicall-max-passwords --password-attack --stealthy")
66
COMPREPLY=()
77
for _opt_ in "${OPTS[@]}"; do

lib/helpers.bash

100644100755
+14-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ function _command_exists ()
2323
_example '$ _command_exists ls && echo exists'
2424
_group 'lib'
2525
local msg="${2:-Command '$1' does not exist!}"
26-
type "$1" &> /dev/null || (_log_warning "$msg" && return 1) ;
26+
if type -t "$1" &> /dev/null
27+
then
28+
return 0
29+
else
30+
_log_warning "$msg"
31+
return 1
32+
fi
2733
}
2834

2935
function _binary_exists ()
@@ -34,7 +40,13 @@ function _binary_exists ()
3440
_example '$ _binary_exists ls && echo exists'
3541
_group 'lib'
3642
local msg="${2:-Binary '$1' does not exist!}"
37-
type -P "$1" &> /dev/null || (_log_warning "$msg" && return 1) ;
43+
if type -P "$1" &> /dev/null
44+
then
45+
return 0
46+
else
47+
_log_warning "$msg"
48+
return 1
49+
fi
3850
}
3951

4052
function _completion_exists ()
+8-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'Autojump configuration, see https://github.com/wting/autojump for more details'
34

45
# Only supports the Homebrew variant, Debian and Arch at the moment.
56
# Feel free to provide a PR to support other install locations
67
if _bash_it_homebrew_check && [[ -s "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh" ]]; then
7-
. "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh"
8-
elif command -v dpkg &>/dev/null && dpkg -s autojump &>/dev/null ; then
9-
. "$(dpkg-query -S autojump.sh | cut -d' ' -f2)"
10-
elif command -v pacman &>/dev/null && pacman -Q autojump &>/dev/null ; then
11-
. "$(pacman -Ql autojump | grep autojump.sh | cut -d' ' -f2)"
8+
source "${BASH_IT_HOMEBREW_PREFIX}/etc/profile.d/autojump.sh"
9+
elif _command_exists dpkg && dpkg -s autojump &> /dev/null; then
10+
# shellcheck disable=SC1090
11+
source "$(dpkg-query -S autojump.sh | cut -d' ' -f2)"
12+
elif _command_exists pacman && pacman -Q autojump &> /dev/null; then
13+
# shellcheck disable=SC1090
14+
source "$(pacman -Ql autojump | grep autojump.sh | cut -d' ' -f2)"
1215
fi

plugins/available/base.plugin.bash

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ function ips ()
55
{
66
about 'display all ip addresses for this host'
77
group 'base'
8-
if command -v ifconfig &>/dev/null
8+
if _command_exists ifconfig
99
then
1010
ifconfig | awk '/inet /{ gsub(/addr:/, ""); print $2 }'
11-
elif command -v ip &>/dev/null
11+
elif _command_exists ip
1212
then
1313
ip addr | grep -oP 'inet \K[\d.]+'
1414
else
@@ -70,7 +70,7 @@ function passgen ()
7070

7171
# Create alias pass to passgen when pass isn't installed or
7272
# BASH_IT_LEGACY_PASS is true.
73-
if ! command -v pass &>/dev/null || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]
73+
if ! _command_exists pass || [[ "${BASH_IT_LEGACY_PASS:-}" = true ]]
7474
then
7575
alias pass=passgen
7676
fi
@@ -81,7 +81,7 @@ function pmdown ()
8181
param '1: markdown file'
8282
example '$ pmdown README.md'
8383
group 'base'
84-
if command -v markdown &>/dev/null
84+
if _command_exists markdown
8585
then
8686
markdown $1 | browser
8787
else

plugins/available/direnv.plugin.bash

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'load direnv, if you are using it: https://direnv.net/'
34

4-
[ -x "$(which direnv)" ] && eval "$(direnv hook bash)"
5+
if _command_exists direnv; then
6+
eval "$(direnv hook bash)"
7+
fi

plugins/available/hub.plugin.bash

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
# shellcheck shell=bash
12
cite about-plugin
23
about-plugin 'load hub, if you are using it'
34

4-
command -v hub &> /dev/null && eval "$(hub alias -s)"
5+
if _command_exists hub; then
6+
eval "$(hub alias -s)"
7+
fi

plugins/available/jump.plugin.bash

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
# shellcheck shell=bash
2+
# shellcheck disable=SC2016
13
cite about-plugin
24
about-plugin 'initialize jump (see https://github.com/gsamokovarov/jump). Add `export JUMP_OPTS=("--bind=z")` to change keybinding'
35

4-
__init_jump() {
5-
command -v jump &> /dev/null || return
6-
eval "$(jump shell bash "${JUMP_OPTS[@]}")"
6+
function __init_jump() {
7+
if _command_exists jump; then
8+
eval "$(jump shell bash "${JUMP_OPTS[@]}")"
9+
fi
710
}
811

912
__init_jump

0 commit comments

Comments
 (0)