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

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

    For Ubuntu, apt provides a fairly decent way to do this. Below is an example for google chrome:

    apt -qq list google-chrome-stable 2>/dev/null | grep -qE "(installed|upgradeable)" || apt-get install google-chrome-stable

    I'm redirecting error output to null because apt warns against using its "unstable cli". I suspect list package is stable so I think it's ok to throw this warning away. The -qq makes apt super quiet.

提交回复
热议问题