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

后端 未结 22 1142
轮回少年
轮回少年 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:05
    $name="rsync"
    
    [ `which $name` ] $$ echo "$name : installed" || sudo apt-get install -y $name
    
    0 讨论(0)
  • 2020-12-02 04:06
    which <command>
    if [ $? == 1 ]; then
        <pkg-manager> -y install <command>
    fi
    
    0 讨论(0)
  • 2020-12-02 04:07

    This feature already exists in Ubuntu and Debian, in the command-not-found package.

    0 讨论(0)
  • 2020-12-02 04:08

    Use:

    apt-cache policy <package_name>
    

    If it is not installed, it will show:

    Installed: none
    

    Otherwise it will show:

    Installed: version
    
    0 讨论(0)
  • 2020-12-02 04:08
    apt list [packagename]
    

    seems to be the simplest way to do it outside of dpkg and older apt-* tools

    0 讨论(0)
  • 2020-12-02 04:09

    I offer this update since Ubuntu added its "Personal Package Archive" (PPA) just as this question was answered, and PPA packages have a different result.

    1. Native Debian repository package not installed:

      ~$ dpkg-query -l apache-perl
      ~$ echo $?
      1
      
    2. PPA package registered on host and installed:

      ~$ dpkg-query -l libreoffice
      ~$ echo $?
      0
      
    3. PPA package registered on host but not installed:

      ~$ dpkg-query -l domy-ce
      ~$ echo $?
      0
      ~$ sudo apt-get remove domy-ce
      [sudo] password for user: 
      Reading package lists... Done
      Building dependency tree       
      Reading state information... Done
      Package domy-ce is not installed, so not removed
      0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
      

    Also posted on: https://superuser.com/questions/427318/test-if-a-package-is-installed-in-apt/427898

    0 讨论(0)
提交回复
热议问题