How can I check if a program exists from a Bash script?

后端 未结 30 1960
有刺的猬
有刺的猬 2020-11-21 07:21

How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?

It seems like it should be easy, but it\

相关标签:
30条回答
  • 2020-11-21 07:50

    For those interested, none of the methodologies in previous answers work if you wish to detect an installed library. I imagine you are left either with physically checking the path (potentially for header files and such), or something like this (if you are on a Debian-based distribution):

    dpkg --status libdb-dev | grep -q not-installed
    
    if [ $? -eq 0 ]; then
        apt-get install libdb-dev
    fi
    

    As you can see from the above, a "0" answer from the query means the package is not installed. This is a function of "grep" - a "0" means a match was found, a "1" means no match was found.

    0 讨论(0)
  • The following is a portable way to check whether a command exists in $PATH and is executable:

    [ -x "$(command -v foo)" ]
    

    Example:

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

    The executable check is needed because bash returns a non-executable file if no executable file with that name is found in $PATH.

    Also note that if a non-executable file with the same name as the executable exists earlier in $PATH, dash returns the former, even though the latter would be executed. This is a bug and is in violation of the POSIX standard. [Bug report] [Standard]

    In addition, this will fail if the command you are looking for has been defined as an alias.

    0 讨论(0)
  • 2020-11-21 07:52

    Try using:

    test -x filename
    

    or

    [ -x filename ]
    

    From the Bash manpage under Conditional Expressions:

     -x file
              True if file exists and is executable.
    
    0 讨论(0)
  • 2020-11-21 07:52

    I never did get the previous answers to work on the box I have access to. For one, type has been installed (doing what more does). So the builtin directive is needed. This command works for me:

    if [ `builtin type -p vim` ]; then echo "TRUE"; else echo "FALSE"; fi
    
    0 讨论(0)
  • 2020-11-21 07:52

    There are a ton of options here, but I was surprised no quick one-liners. This is what I used at the beginning of my scripts:

    [[ "$(command -v mvn)" ]] || { echo "mvn is not installed" 1>&2 ; exit 1; }
    [[ "$(command -v java)" ]] || { echo "java is not installed" 1>&2 ; exit 1; }
    

    This is based on the selected answer here and another source.

    0 讨论(0)
  • 2020-11-21 07:54

    In case you want to check if a program exists and is really a program, not a Bash built-in command, then command, type and hash are not appropriate for testing as they all return 0 exit status for built-in commands.

    For example, there is the time program which offers more features than the time built-in command. To check if the program exists, I would suggest using which as in the following example:

    # First check if the time program exists
    timeProg=`which time`
    if [ "$timeProg" = "" ]
    then
      echo "The time program does not exist on this system."
      exit 1
    fi
    
    # 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
    
    0 讨论(0)
提交回复
热议问题