我正在Ubuntu系统上工作,目前我正在做的是:
if ! which command > /dev/null; then
echo -e "Command not found! Install? (y/n) \c"
read
if "$REPLY" = "y"; then
sudo apt-get install command
fi
fi
大多数人都会这么做吗?还是有更优雅的解决方案?
我正在Ubuntu系统上工作,目前我正在做的是:
if ! which command > /dev/null; then
echo -e "Command not found! Install? (y/n) \c"
read
if "$REPLY" = "y"; then
sudo apt-get install command
fi
fi
大多数人都会这么做吗?还是有更优雅的解决方案?
当前回答
这似乎很有效。
$ sudo dpkg-query -l | grep <some_package_name> | wc -l
如果没有安装,则返回0;如果安装,则返回某个数字> 0。
其他回答
Use:
apt-cache policy <package_name>
如果没有安装,它将显示:
Installed: none
否则它将显示:
Installed: version
这个就行了。Apt-get install是幂等的。
sudo apt-get install --no-upgrade command
这个特性已经存在于Ubuntu和Debian的command-not-found包中。
这一行代码为“nano”包返回1(已安装)或0(未安装)…
$(dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed")
即使包不存在或不可用。
下面的例子将安装'nano'包,如果它没有安装…
if [ $(dpkg-query -W -f='${Status}' nano 2>/dev/null | grep -c "ok installed") -eq 0 ];
then
apt-get install nano;
fi
这个命令是最容易记住的:
dpkg --get-selections <package-name>
如果已经安装,它会打印:
<包名称>安装
否则它会打印
没有找到匹配<package-name>的包。
这是在Ubuntu 12.04.1 (Precise穿山甲)上测试的。