tar_com=`rpm -qa|grep tar`
if [ -n tar_com ];then
echo 'tar ok'
else
echo 'tar no'
fi
-n : 来判定字符串非空
if [ -n str1 ] 当串的长度大于0时为真(串非空)
上面的方法不太好
- 一般判断某个包是否安装不会使用grep去匹配,而使用rpm -qa xxx(包名),去确定指定的包是否安装
- 使用 $? 去获取上一条指令的返回码,其中 0表示指令指令成功,否则执行失败,此时如果执行rpm -qa|grep xxx就算没有这个包,也不会报指令执行失败
tar_com=`rpm -qa tar`
if [ -n tar_com ];then
echo 'tar ok'
else
echo 'tar no'
fi
或者
rpm -qa xxx
if [ $? -ne 0 ];then
echo "xxx包已经安装"
else
echo "xxx包没有安装"
fi