我想查看软件列表(如果已安装)。如果不是,则应显示该脚本,并且脚本应中止/退出。如果我执行一次脚本,输出应如下所示:
wget is not installed
telnet is not installed
当前看起来如下:
wget is not installed
再次执行脚本...
telnet is not installed
当前脚本正在检查已安装的软件,如果未安装当前检查的软件,则中止/存在。这不好,因为您必须多次运行脚本才能识别和检查是否已安装每个软件:
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
我也认为,我的解决方案不是最好的。有人可以调整吗?提前致谢! :)
提示:我两次声明了变量“NEEDED_SOFTWARE_LIST”,因为我认为我将需要两个“数组”的软件列表,因为某些发行版需要另一个软件包。
最佳答案
为什么您要对安装的软件包进行严格的测试,而不是仅仅检查某些实用程序的可用性?
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
但是,如果要检查软件包是否已安装在Debian上,请更换此噩梦
dpkg -l | grep -i $SOFTWARE | head -1 | if [[ "$(cut -d ' ' -f 1)" != "ii" ]]; then
和
if dpkg -l $SOFTWARE; then
关于bash - 如果未安装某些软件,如何检入bash脚本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20815433/