Comment puis-je vérifier si un programme existe depuis un script Bash

Mots clés : bashbash

meilleur 5 Réponses Comment puis-je vérifier si un programme existe depuis un script Bash

vote vote

98

command -v <the_command> 
if ! command -v <the_command> &> /dev/null then     echo "<the_command> could not be found"     exit fi 
hash <the_command> # For regular commands. Or... type <the_command> # To check built-ins and keywords 
$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; } $ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; } $ hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; } 
gnudate() {     if hash gdate 2>/dev/null; then         gdate "$@"     else         date "$@"     fi } 
checkBin <the_command> || errorMessage "This tool requires <the_command>. Install it please, and then run this tool again." 
vote vote

86

[ -x "$(command -v foo)" ] 
if ! [ -x "$(command -v git)" ]; then   echo 'Error: git is not installed.' >&2   exit 1 fi 
vote vote

75

$ command -v foo >/dev/null 2>&1 || { echo "I require foo but it's not installed.  Aborting." >&2; exit 1; } 
vote vote

68

command_exists () {     type "$1" &> /dev/null ; } 
if command_exists mvim ; then     export VISUAL="mvim --nofork" fi 
vote vote

52

if which programname >/dev/null; then     echo exists else     echo does not exist fi 
if [ -x /path/to/programname ]; then     echo exists else     echo does not exist fi 

Questions similaires