I have written a bash script which is checking OS version and count for available security patches according to OS distribution.
The query here is the script is not working for Ubuntu OS. The script is as below:
#!/bin/bash
# Detecting OS Distribution
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$PRETTY_NAME
elif [ -f /etc/redhat-release ]; then
REDHAT=$(cat /etc/redhat-release)
else
echo " OS is not supported "
fi
#Check how many packages for security and available to update
if [[ $OS == *"Red Hat"* ]] || [[ $OS == *"Amazon Linux"* ]] || [[ $OS ==
*"CentOS"* ]] || [[ $REDHAT == *"Red Hat"* ]] ; then
SECPKGCNT=$(sudo yum list-security --security | awk 'NR>2 {print last}
{last=$0}' | wc -l)
echo "${OS},${REDHAT},${SECPKGCNT}"
elif [[ $OS == *"Ubuntu"* ]] ;then
SECPKGCOUNT=$(sudo apt list --upgradable 2>/dev/null | grep "\-security"
| wc -l)
echo "${OS},${SECPKGCOUNT}"
else
echo " No security packages to update"
fi
O/p in ubuntu machine:
root@ip-XXXX:~# sh -x getos.sh
+ [ -f /etc/os-release ]
+ . /etc/os-release
+ NAME=Ubuntu
+ VERSION=14.04.5 LTS, Trusty Tahr
+ ID=ubuntu
+ ID_LIKE=debian
+ PRETTY_NAME=Ubuntu 14.04.5 LTS
+ VERSION_ID=14.04
+ HOME_URL=http://www.ubuntu.com/
+ SUPPORT_URL=http://help.ubuntu.com/
+ BUG_REPORT_URL=http://bugs.launchpad.net/ubuntu/
+ OS=Ubuntu 14.04.5 LTS
+ [[ Ubuntu 14.04.5 LTS = *Red Hat* ]]
getos.sh: 29: getos.sh: [[: not found
+ [[ Ubuntu 14.04.5 LTS = *Amazon Linux* ]]
getos.sh: 29: getos.sh: [[: not found
+ [[ Ubuntu 14.04.5 LTS = *CentOS* ]]
getos.sh: 29: getos.sh: [[: not found
+ [[ = *Red Hat* ]]
getos.sh: 29: getos.sh: [[: not found
+ [ Ubuntu 14.04.5 LTS = *Ubuntu* ]
getos.sh: 34: [: Ubuntu: unexpected operator
+ echo No security packages to update
No security packages to update
In above ubuntu system the script is not working as expected, I mean it should print OS version and count of security patches.