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

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

    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.

提交回复
热议问题