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

后端 未结 30 1963
有刺的猬
有刺的猬 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:55

    I have a function defined in my .bashrc that makes this easier.

    command_exists () {
        type "$1" &> /dev/null ;
    }
    

    Here's an example of how it's used (from my .bash_profile.)

    if command_exists mvim ; then
        export VISUAL="mvim --nofork"
    fi
    
    0 讨论(0)
  • 2020-11-21 07:55

    It could be simpler, just:

    #!/usr/bin/env bash                                                                
    set -x                                                                             
    
    # if local program 'foo' returns 1 (doesn't exist) then...                                                                               
    if ! type -P foo; then                                                             
        echo 'crap, no foo'                                                            
    else                                                                               
        echo 'sweet, we have foo!'                                                    
    fi                                                                                 
    

    Change foo to vi to get the other condition to fire.

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

    The which command might be useful. man which

    It returns 0 if the executable is found and returns 1 if it's not found or not executable:

    NAME
    
           which - locate a command
    
    SYNOPSIS
    
           which [-a] filename ...
    
    DESCRIPTION
    
           which returns the pathnames of the files which would
           be executed in the current environment, had its
           arguments been given as commands in a strictly
           POSIX-conformant shell. It does this by searching
           the PATH for executable files matching the names
           of the arguments.
    
    OPTIONS
    
           -a     print all matching pathnames of each argument
    
    EXIT STATUS
    
           0      if all specified commands are 
                  found and executable
    
           1      if one or more specified commands is nonexistent
                  or not executable
    
           2      if an invalid option is specified
    

    The nice thing about which is that it figures out if the executable is available in the environment that which is run in - it saves a few problems...

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

    If you check for program existence, you are probably going to run it later anyway. Why not try to run it in the first place?

    if foo --version >/dev/null 2>&1; then
        echo Found
    else
        echo Not found
    fi
    

    It's a more trustworthy check that the program runs than merely looking at PATH directories and file permissions.

    Plus you can get some useful result from your program, such as its version.

    Of course the drawbacks are that some programs can be heavy to start and some don't have a --version option to immediately (and successfully) exit.

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

    Use Bash builtins if you can:

    which programname
    

    ...

    type -P programname
    
    0 讨论(0)
  • 2020-11-21 07:59

    If you guys/gals can't get the things in answers here to work and are pulling hair out of your back, try to run the same command using bash -c. Just look at this somnambular delirium. This is what really happening when you run $(sub-command):

    First. It can give you completely different output.

    $ command -v ls
    alias ls='ls --color=auto'
    $ bash -c "command -v ls"
    /bin/ls
    

    Second. It can give you no output at all.

    $ command -v nvm
    nvm
    $ bash -c "command -v nvm"
    $ bash -c "nvm --help"
    bash: nvm: command not found
    
    0 讨论(0)
提交回复
热议问题