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

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

提交回复
热议问题