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

后端 未结 22 1350
轮回少年
轮回少年 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:14

    This one-liner returns 1 (installed) or 0 (not installed) for the 'nano' package..

    $(dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed")
    

    even if the package does not exist / is not available.

    The example below installs the 'nano' package if it is not installed..

    if [ $(dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed") -eq 0 ];
    then
      apt-get install nano;
    fi
    

提交回复
热议问题