Check if an apt-get package is installed and then install it if it's not on Linux

后端 未结 22 1295
轮回少年
轮回少年 2020-12-02 04:02

I\'m working on a Ubuntu system and currently this is what I\'m doing:

if ! which command > /dev/null; then
   echo -e \"Command not found! Install? (y/n)         


        
22条回答
  •  眼角桃花
    2020-12-02 04:20

    I've found all solutions above can produce a false positive if a package is installed and then removed yet the installation package remains on the system.

    To replicate: Install package apt-get install curl
    Remove package apt-get remove curl

    Now test above answers.

    The following command seems to solve this condition:
    dpkg-query -W -f='${Status}\n' curl | head -n1 | awk '{print $3;}' | grep -q '^installed$'

    This will result in a definitive installed or not-installed

提交回复
热议问题