我想创建一个函数来检查程序或库是否已经安装。
这就是我现在所做的:
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/