Shell脚本编写-猜测当前系统是哪个发行版

1、编写脚本

       该脚本会确定当前系统中可用的包管理器。同时还以已安装的软件包管理器为指导,猜测当前系统是基于哪个 Linux 发行版。

#!/bin/bash
#检查当前系统的可用包管理器,以安装的软件包管理器为指导,猜测当前的系统是基于哪个Linux发行版
#
##################### 检查 redhat #######################
#
echo " 检查基于红帽的包管理器 "
echo " 应用程序容器... "
#####
if (which rpm &> /dev/null)
then
 item_rpm=1
 echo " 您有rpm包管理器 "
#
else
 item_rpm=0
#
fi
####
if (which dnf &> /dev/null)
then
 item_dnfyum=1
 echo " 您有dnf包管理器 "
#
elif (which yum &> /dev/null)
then
 item_dnfyum=1
 echo " 您有yum包管理器 "
else
 item_dnfyum=0
#
fi
####
if (which flatpak &> /dev/null)
then
 item_flatpak=1
 echo " 您有flatpak应用程序容器。"
#
else
 item_flatpak=0
#
fi 
####
#脚本会计算出一个分数(redhatscore)。这个分数随后会用于对系统采用的发行版进行猜测
redhatscore=$[$item_rpm + $item_dnfyum + $item_flatpak] 

#
##################### 检查 Debian #######################
#
echo
echo " 检查基于debian的包管理器 "
echo " 应用程序容器... "
#####
if (which dpkg &> /dev/null)
then
 item_dpkg=1
 echo " 您有基本的dpkg包管理器 "
#
else
 item_dpkg=0
#
fi
####
if (which apt &> /dev/null)
then
 item_aptaptget=1
 echo " 您有apt包管理器 "
#
elif (which apt-get &> /dev/null)
then
 item_aptaptget=1
 echo " 您有apt-get/apt-cache包管理器 "
#
else
 item_aptaptget=0
fi
####
if (which snap &> /dev/null)
then
 item_snap=1
 echo " 您有snap应用程序容器 "
#
else
 item_snap=0
#
fi
####
#
debianscore=$[$item_dpkg + $item_aptaptget + $item_snap]
#
#
##################### Determine Distro #######################
#
echo
if [ $debianscore -gt $redhatscore ]
then
 echo " 您的Linux发行版很可能是基于debian的 "
 #
elif [ $redhatscore -gt $debianscore ]
then
 echo " 您的Linux发行版很可能是基于Red-hat的 "
else
 echo " 无法确定Linux发行版 "
fi
#
echo
#
#############################################################
#
exit

2、运行脚本测试!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值