bash 我如何知道 Arch Linux 中是否已经安装了特定的软件包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26274415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I find out, whether a specific package is already installed in Arch Linux
提问by Random Citizen
I want to write a bash script allowing me to check, whether a certain package is already installed in arch linux.
我想编写一个 bash 脚本,让我检查某个包是否已经安装在 arch linux 中。
How can I do that?
我怎样才能做到这一点?
回答by Fredszaq
You should use Pacman, the package manager of Arch Linux.
你应该使用 Arch Linux 的包管理器 Pacman。
You want to use the -Q
operation to query the installed local package database and the -i
option to get information on the package.
您希望使用该-Q
操作来查询已安装的本地包数据库以及-i
获取该包信息的选项。
This gives you
这给你
pacman -Qi <packageName>
You can then use the exit code to determine if the packages existes on the system or not (0 the package exists, 1 it doesn't)
然后,您可以使用退出代码来确定系统上是否存在包(0 包存在,1 不存在)
The usage of -i
rather than -s
ensures you will check for exactly that package and not for the presence of a a package containing the package name in its name.
使用-i
而不是-s
确保您将准确地检查该包,而不是检查名称中包含包名称的包的存在。
For example if I search for chromium
(the web browser) on a system where only chromium-bsu
(the game) is installed,
例如,如果我chromium
在只chromium-bsu
安装了(游戏)的系统上搜索(网络浏览器),
# This exits with 1 because chromium is not installed
pacman -Qi chromium
# This exits with 0 because chromium-bsu is installed
pacman -Qs chromium
As Random Citizenpointed out, you certainly want to redirect any output to /dev/null
if you are writing a script and don't want Pacman to pollute your output:
正如Random Citizen指出的那样,/dev/null
如果您正在编写脚本并且不希望 Pacman 污染您的输出,您当然希望将任何输出重定向到:
pacman -Qi <packageName> > /dev/null
回答by Random Citizen
You can use the arch package management tool pacman.
As you can see in the Arch-Wiki, the -Qs
option can be used to search within the installed packages.
您可以使用 arch 包管理工具pacman。
正如您在Arch-Wiki 中所见,该-Qs
选项可用于在已安装的软件包中进行搜索。
If the package exists, pacman -Qs
will exit with the exit-code 0, otherwise with the exit-code 1
如果包存在,pacman -Qs
将以退出代码 0 退出,否则以退出代码 1
You script might look like:
您的脚本可能如下所示:
package=firefox
if pacman -Qs $package > /dev/null ; then
echo "The package $package is installed"
else
echo "The package $package is not installed"
fi
The > /dev/null
pipe is used to suppress the printed output.
该> /dev/null
管被用来抑制打印输出。
回答by GreenRaccoon23
I usually just do ls /bin | grep $package
(replacing $package with the package I'm looking for). It's quick for the computer too.
我通常只是这样做ls /bin | grep $package
(用我正在寻找的包替换 $package )。电脑也很快。
It depends on the name of the package though, because this will list all of the installed executables that have $package in their name. Nevertheless, if you have executables installed with $package in their name, there's a big chance the package you're looking for is already installed.
不过,这取决于包的名称,因为这将列出名称中包含 $package 的所有已安装可执行文件。不过,如果您安装了名称中带有 $package 的可执行文件,则您要查找的软件包很可能已经安装。
Update
更新
Here is a more accurate one:
这是一个更准确的:
package="lshw";
check="$(sudo pacman -Qs --color always "${package}" | grep "local" | grep "${package} ")";
if [ -n "${check}" ] ; then
echo "${package} is installed";
elif [ -z "${check}" ] ; then
echo "${package} is NOT installed";
fi;
Even better, how about turning it into a function?
更好的是,把它变成一个函数怎么样?
There's 2 examples in the code below. You can use _install
to install just one package. You can use _installMany
to install as many packages as you want. I included both functions because _installMany
is kind of complex, and looking at the slightly simpler _install
function might help someone understand it.
下面的代码中有 2 个示例。您可以使用_install
仅安装一个软件包。您可以根据_installMany
需要安装任意数量的软件包。我包含了这两个函数,因为它们_installMany
有点复杂,查看稍微简单的_install
函数可能有助于人们理解它。
#!/bin/bash
_isInstalled() {
package="";
check="$(sudo pacman -Qs --color always "${package}" | grep "local" | grep "${package} ")";
if [ -n "${check}" ] ; then
echo 0; #'0' means 'true' in Bash
return; #true
fi;
echo 1; #'1' means 'false' in Bash
return; #false
}
# `_install <pkg>`
_install() {
package="";
# If the package IS installed:
if [[ $(_isInstalled "${package}") == 0 ]]; then
echo "${package} is already installed.";
return;
fi;
# If the package is NOT installed:
if [[ $(_isInstalled "${package}") == 1 ]]; then
sudo pacman -S "${package}";
fi;
}
# `_installMany <pkg1> <pkg2> ...`
# Works the same as `_install` above,
# but you can pass more than one package to this one.
_installMany() {
# The packages that are not installed will be added to this array.
toInstall=();
for pkg; do
# If the package IS installed, skip it.
if [[ $(_isInstalled "${pkg}") == 0 ]]; then
echo "${pkg} is already installed.";
continue;
fi;
#Otherwise, add it to the list of packages to install.
toInstall+=("${pkg}");
done;
# If no packages were added to the "${toInstall[@]}" array,
# don't do anything and stop this function.
if [[ "${toInstall[@]}" == "" ]] ; then
echo "All packages are already installed.";
return;
fi;
# Otherwise, install all the packages that have been added to the "${toInstall[@]}" array.
printf "Packages not installed:\n%s\n" "${toInstall[@]}";
sudo pacman -S "${toInstall[@]}";
}
package="lshw";
_install "${package}";
packages=("lshw" "inkscape");
_installMany "${packages[@]}";
#Or,
_installMany "lshw" "inkscape"
回答by nathanchere
Try this:
尝试这个:
isPackageInstalled() {
pacman -Qi "$packageName" &> /dev/null
echo $?
}
Using in a script is as simple as
在脚本中使用非常简单
if [ $(isPackageInstalled 'libssl') ]; then
# do things here
echo 'Package libssl is installed'
fi