bash - 如何制作 bash 脚本来检查程序或库是否存在

标签 bash ubuntu debian

我想创建一个函数来检查程序或库是否已经安装。

这就是我现在所做的:

dpkg --status software-properties-common | grep -q not-installed
if [ $? -eq 0 ]; then
    sudo apt-get install -y software-properties-common
fi

我想要的是:
isPackageNotInstalled() {???} 

if [ $(isPackageNotInstalled 'software-properties-common') ]; then 
   sudo apt-get install -y software-properties-common
fi

任何帮助将不胜感激。

最佳答案

一个示例脚本,包括一个检查和安装丢失包的函数:

#!/bin/bash

isPackageNotInstalled() {

    dpkg --status $1 &> /dev/null
    if [ $? -eq 0 ]; then
    echo "$1: Already installed"
    else
    sudo apt-get install -y $1
    fi
    }

isPackageNotInstalled $1

另存为script , 用法 ./script package_name .
man dpkg :
EXIT STATUS
       0      The requested action was successfully performed.  Or a check  or
              assertion command returned true.

       1      A check or assertion command returned false.

       2      Fatal  or unrecoverable error due to invalid command-line usage,
              or interactions  with  the  system,  such  as  accesses  to  the
              database, memory allocations, etc.

关于bash - 如何制作 bash 脚本来检查程序或库是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51769310/

相关文章:

bash - 在变量中执行命令不执行管道的后半部分

linux - 用于替换文件中所有出现的占位符的 bash 脚本

bash - 有没有办法在不重启终端的情况下通过 bash_profile 到 "resource"?

python - TensorFlow:libcudart.so.7.5:无法打开共享对象文件:没有这样的文件或目录

ubuntu - mysqld 在运行简单查询时收到信号 6

bash - bash 脚本中 $() 中的命令

bash - 如何在unix shell脚本中查找文件中字符串的位置

sql - 带有 TimescaleDB 的 PostgreSQL 在索引创建期间仅使用单个核心

linux - 使用 Bash 在动态网页上模拟掷骰子

java - 无法删除openjdk debian9.6