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

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

提交回复
热议问题