skip to Main Content

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


  1. You can filter the the Python version using sed, e.g.

    python3 -V | awk '{ print $2 }' | sed 's/([0-9]*.[0-9]*).*/1/'
    

    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.

    echo "➥➥➥ Current System Is Based On $CURRENT_OS"
    

    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 like

    if [ -e /etc/redhat-release ]; then
       echo "➥➥➥ Current system seems to be a distribution of the RedHat family"
       # ...
    fi
    

    might do what you need.

    Please clarify the question.

    Login or Signup to reply.
  2. 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.

    [user@redhat8 ~]# cat /etc/redhat-release 
    Red Hat Enterprise Linux release 8.1 (Ootpa)
    [user@redhat8 ~]# lsb_release -i
    Distributor ID: RedHatEnterprise
    [user@redhat8 ~]# ls -l /etc/*release
    -rw-r--r--. 1 root root 501 Sep 25  2019 /etc/os-release
    -rw-r--r--. 1 root root  45 Sep 25  2019 /etc/redhat-release
    lrwxrwxrwx. 1 root root  14 Sep 25  2019 /etc/system-release -> redhat-release
    [user@redhat8 ~]# grep -w ID /etc/os-release
    ID="rhel"
    
    
    [user@centos7 ~]# cat /etc/redhat-release 
    CentOS Linux release 7.6.1810 (Core) 
    [root@centos7 ~]# lsb_release -i
    Distributor ID: CentOS
    [user@centos7 ~]# ls -l /etc/*release
    -rw-r--r--. 1 root root  38 Nov 23  2018 /etc/centos-release
    -rw-r--r--. 1 root root 393 Nov 23  2018 /etc/os-release
    lrwxrwxrwx. 1 root root  14 Apr 15 09:05 /etc/redhat-release -> centos-release
    lrwxrwxrwx. 1 root root  14 Apr 15 09:05 /etc/system-release -> centos-release
    [user@centos7 ~]# grep -w ID /etc/os-release 
    ID="centos"
    
    
    [user@Fedora ~]$ cat /etc/redhat-release 
    Fedora release 32 (Thirty Two)
    [user@Fedora ~]$ lsb_release -i
    Distributor ID: Fedora
    [user@fedora ~]$ ls -l /etc/*release
    lrwxrwxrwx. 1 root root 25 Jun  5 22:00 /etc/fedora-release -> ../usr/lib/fedora-release
    lrwxrwxrwx. 1 root root 21 Jun  5 22:00 /etc/os-release -> ../usr/lib/os-release
    lrwxrwxrwx. 1 root root 14 Jun  5 22:00 /etc/redhat-release -> fedora-release
    lrwxrwxrwx. 1 root root 14 Jun  5 22:00 /etc/system-release -> fedora-release
    [user@fedora ~]$ grep -w ID /etc/os-release
    ID=fedora
    

    You will need to have redhat-lsb-core package installed to have lsb_release command on Red Hat based distributions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search