I’m trying to automate some basics in my Linux Server, I would like to check if the certain version of application like git, python or some other apps are installed or not, if installed than update to latest stable version. Here is my script and it seems I’m missing something:
#!/bin/sh
CURRENT_OS=$(hostnamectl | awk '/Operating/ { print $3 }')
CURRENT_PY=$(python3 -V | awk '{ print $2 }')
CURRENT_GT=$(git --version | awk '{ print $3 }')
if [ $CURRENT_OS=~"Fedora" || $CURRENT_OS=~"Centos" || $CURRENT_OS=~"Red Hat" ]; then
echo "➥➥➥ Current System Is Based On $CURRENT_OS"
sudo dnf clean all
sudo dnf -y upgrade
if [ $CURRENT_PY!="Python 3.8" ]; then
echo "➥➥➥ Installing Latest Python Release"
sudo dnf -y install python3.8
else
echo "➥➥➥ Latest Python Release Found In System"
fi
fi
Also is there any chance to swap output of command python3 -V | awk '{ print $2 }'
to 3.8 instead of 3.8.5?
And also how can I refactor the OS release check so either is Red Hat, Centos or Fedora?
2
Answers
You can filter the the Python version using
sed
, e.g.The question about the OS release check is not clear. If you only want to have the correct distribution name in the output you can use e.g.
Edit:
This was not meant as a fix for a "typo" but my (possibly wrong) interpretation of the unclear question.
If you want to find a common criteria for Red Hat, Centos or Fedora, the existence of a file
/etc/redhat-release
might be an indication for this family of distributions as/etc/debian_version
is an indication for a Debian based distribution (including e.g. Ubuntu). That means a check likemight do what you need.
Please clarify the question.
If you are only dealing with CentOS/Fedora/Red Hat you can use /etc/redhat-release to find which distribution it is, or also lsb_release -i instead of hostnamectl.
Also on Fedora and CentOS you usually also have fedora-release and centos-release files, in addition to redhat-release.
You can also grep ID from /etc/os-release.
You will need to have redhat-lsb-core package installed to have lsb_release command on Red Hat based distributions.