NAME
which - locate a command
SYNOPSIS
which [-a] filename ...
DESCRIPTION
which returns the pathnames of the files which wouldbe executed in the current environment, had itsarguments been given as commands in a strictlyPOSIX-conformant shell. It does this by searchingthe PATH for executable files matching the namesof the arguments.
OPTIONS
-a print all matching pathnames of each argument
EXIT STATUS
0 if all specified commands arefound and executable
1 if one or more specified commands is nonexistentor not executable
2 if an invalid option is specified
为了模仿Bash的type -P cmd,我们可以使用符合POSIX的env -i type cmd 1>/dev/null 2>&1。
man env# "The option '-i' causes env to completely ignore the environment it inherits."# In other words, there are no aliases or functions to be looked up by the type command.
ls() { echo 'Hello, world!'; }
lstype lsenv -i type ls
cmd=lscmd=lsxenv -i type $cmd 1>/dev/null 2>&1 || { echo "$cmd not found"; exit 1; }
# First check if the time program existstimeProg=`which time`if [ "$timeProg" = "" ]thenecho "The time program does not exist on this system."exit 1fi
# Invoke the time program$timeProg --quiet -o result.txt -f "%S %U + p" du -sk ~echo "Total CPU time: `dc -f result.txt` seconds"rm result.txt
GIT=/usr/bin/git # STORE THE RELATIVE PATH# GIT=$(which git) # USE THIS COMMAND TO SEARCH FOR THE RELATIVE PATH
if [[ ! -e $GIT ]]; then # CHECK IF THE FILE EXISTSecho "PROGRAM DOES NOT EXIST."exit 1 # EXIT THE PROGRAM IF IT DOES NOTfi
# DO SOMETHING ...
exit 0 # EXIT THE PROGRAM IF IT DOES
#!/bin/bash
# Commands found in the hash table are checked for existence before being# executed and non-existence forces a normal PATH search.shopt -s checkhash
function exists() {local mycomm=$1; shift || return 1
hash $mycomm 2>/dev/null || \printf "\xe2\x9c\x98 [ABRT]: $mycomm: command does not exist\n"; return 1;}readonly -f exists
exists notacmdexists bashhashbash -c 'printf "Fin.\n"'
结果
✘ [ABRT]: notacmd: command does not existhits command0 /usr/bin/bashFin.
#!/usr/bin/env bashset -x
# if local program 'foo' returns 1 (doesn't exist) then...if ! type -P foo; thenecho 'crap, no foo'elseecho 'sweet, we have foo!'fi
#!/bin/bashecho "hello world"exit 1 # throw some error code
示例:
# outputs something bad... and exitsbash foo.sh $? -eq 0 || echo "something bad happened. not installed" ; exit 1
# does NOT outputs nothing nor exits because dotnet is installed on my machinedotnet --version $? -eq 0 || echo "something bad happened. not installed" ; exit 1
THE ZSH/PARAMETER MODULEThe zsh/parameter module gives access to some of the internal hash ta‐bles used by the shell by defining some special parameters.
[...]
commandsThis array gives access to the command hash table. The keys arethe names of external commands, the values are the pathnames ofthe files that would be executed when the command would be in‐voked. Setting a key in this array defines a new entry in thistable in the same way as with the hash builtin. Unsetting a keyas in `unset "commands[foo]"' removes the entry for the givenkey from the command hash table.
虽然它是一个可加载的模块,但它似乎是默认加载的,只要zsh不与--emulate一起使用。
例子:
martin@martin ~ % echo $commands[zsh]/usr/bin/zsh
要快速检查某个命令是否可用,只需检查哈希中是否存在键:
if (( ${+commands[zsh]} ))thenecho "zsh is available"fi
-a means Names of alias-b means Names of shell builtins-c means Names of all commands-d means Names of directory-e means Names of exported shell variables-f means Names of file and functions-g means Names of groups-j means Names of job-k means Names of Shell reserved words-s means Names of service-u means Names of userAlias names-v means Names of shell variables