[英]How can I check in a bash script, if some software is installed or not?
I want to check a list of software, if it is installed or not. 我想查看软件列表,如果已安装或未安装。 if not, it should be displayed and the script should abort/exit. 如果没有,它应该显示,脚本应该中止/退出。 The output should look like following, if I execute the script once: 如果我执行一次脚本,输出应如下所示:
wget is not installed
telnet is not installed
Currently it's looking like following: 目前它看起来如下:
wget is not installed
Execute script again... 再次执行脚本......
telnet is not installed
The current script is checking for installed software and aborts/exists, if the current checked software is not installed. 如果未安装当前检查的软件,则当前脚本正在检查已安装的软件并中止/存在。 That's not nice, because you have to run the script more times to identify and check, if each software is installed or not: 这并不好,因为如果安装了每个软件,你必须运行脚本多次以识别和检查:
LINUX_DISTRIBUTATION=$(grep -Eo "(Debian|Ubuntu|RedHat|CentOS)" /etc/issue)
# Debian / Ubuntu
if [ -f /etc/debian_version ] || [ "$LINUX_DISTRIBUTATION" == "Debian" ] || [ "$LINUX_DISTRIBUTATION" == "Ubuntu" ]; then
declare -a NEEDED_SOFTWARE_LIST=(bash rsync wget grep telnet sed)
for SOFTWARE in ${NEEDED_SOFTWARE_LIST[@]}; do
dpkg -l | grep -i $SOFTWARE | head -1 | if [[ "$(cut -d ' ' -f 1)" != "ii" ]]; then
echo -e "[ ${Red}FAILED ${RCol}]\t$SOFTWARE is NOT installed completely! Please install it...\n";
exit 1;
fi
done
# RedHat / CentOS
elif [ -f /etc/redhat-release ] || [ "$LINUX_DISTRIBUTATION" == "RedHat" ] || [ "$LINUX_DISTRIBUTATION" == "CentOS" ]; then
declare -a NEEDED_SOFTWARE_LIST=(bash rsync wget grep telnet sed)
for SOFTWARE in ${NEEDED_SOFTWARE_LIST[@]}; do
if [[ "$(rpm -q $SOFTWARE)" == "package $SOFTWARE is not installed" ]]; then
echo -e "[ ${Red}FAILED ${RCol}]\t$SOFTWARE is NOT installed completely! Please install it...\n";
exit 1;
fi
done
else
echo "[ ${Red}FAILED ${RCol}]\tYour system is currently not supported by this script.";
exit 1;
fi
I also think, that my solution is not the best. 我也认为,我的解决方案不是最好的。 Can anybody adjust it? 任何人都可以调整吗? Thanks in advance! 提前致谢! :) :)
Hint: I declared the variable "NEEDED_SOFTWARE_LIST" twice, because I thought I will need two "arrays" of software lists, because some distributions needs another software packages. 提示:我两次声明变量“NEEDED_SOFTWARE_LIST”,因为我认为我需要两个软件列表“阵列”,因为某些发行版需要另一个软件包。
Why do you perform a monstrous test if package is installed instead of simply checking availability of certain utilities? 如果安装了软件包而不是简单地检查某些实用程序的可用性,为什么要执行一个怪异的测试?
SCRIPTNAME="${0##*/}"
warn() {
printf >&2 "$SCRIPTNAME: $*\n"
}
iscmd() {
command -v >&- "$@"
}
checkdeps() {
local -i not_found
for cmd; do
iscmd "$cmd" || {
warn $"$cmd is not found"
let not_found++
}
done
(( not_found == 0 )) || {
warn $"Install dependencies listed above to use $SCRIPTNAME"
exit 1
}
}
checkdeps wget rsync realpath
However, if you want to check if package is installed on Debian, please replace this nightmare 但是,如果你想检查Debian上是否安装了软件包,请更换这个恶梦
dpkg -l | grep -i $SOFTWARE | head -1 | if [[ "$(cut -d ' ' -f 1)" != "ii" ]]; then
with 同
if dpkg -l $SOFTWARE; then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.