Sunday, May 5, 2024
84
rated 0 times [  84] [ 0]  / answers: 1 / hits: 148652  / 2 Years ago, thu, july 7, 2022, 12:25:01

I know the commands to check the name of the Linux machine running on my machine. For example:



Ubuntu



cat /etc/version


CentOS



cat /etc/issue


How do I get the output from the terminal and compare to see if it is UBUNTU or CENTOS and perform the following commands?



apt-get install updates 


or



yum update


Ubuntu 14.04



cat /etc/issue

More From » command-line

 Answers
6

Unfortunately, there is no surefire, simple way of getting the distribution name. Most major distros are moving towards a system where they use /etc/os-release to store this information. Most modern distributions also include the lsb_release tools but these are not always installed by default. So, here are some approaches you can use:




  1. Use /etc/os-release



    awk -F= '/^NAME/{print $2}' /etc/os-release

  2. Use the lsb_release tools if available



    lsb_release -d | awk -F"	" '{print $2}'

  3. Use a more complex script that should work for the great majority of distros:



    # Determine OS platform
    UNAME=$(uname | tr "[:upper:]" "[:lower:]")
    # If Linux, try to determine specific distribution
    if [ "$UNAME" == "linux" ]; then
    # If available, use LSB to identify distribution
    if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
    export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^ '//)
    # Otherwise, use release info file
    else
    export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
    fi
    fi
    # For everything else (or if above failed), just use generic identifier
    [ "$DISTRO" == "" ] && export DISTRO=$UNAME
    unset UNAME

  4. Parse the version info of gcc if installed:



    CentOS 5.x



    $ gcc --version
    gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
    Copyright (C) 2006 Free Software Foundation, Inc.


    CentOS 6.x



    $ gcc --version
    gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
    Copyright (C) 2010 Free Software Foundation, Inc.


    Ubuntu 12.04



    $ gcc --version
    gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
    Copyright (C) 2011 Free Software Foundation, Inc.


    Ubuntu 14.04



    $ gcc --version
    gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
    Copyright (C) 2013 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.



This has basically been directly copied from @slm's great answer to my question here.


[#25490] Friday, July 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ilityushing

Total Points: 18
Total Questions: 100
Total Answers: 113

Location: Senegal
Member since Wed, May 13, 2020
4 Years ago
;