0

I want to make a bash script to take the system os and the version as a simple string.

Possible ways to get these info is from

  • /etc/issue
  • cat /etc/*-release
  • lsb_release -a

and probably some others which i dont know. The problem is that i want the bash script to work on Ubuntu 12,13,14 and CentOS. Some of the above does not work in these systems. For example the lsb_release does not work on CentOS and sometimes the /etc/issue is empty so i'm little confused about it.

As for the string i want to get it in this way (and save it to var). I will give examples.

If OS is Ubuntu 12.x i want to take it as ubuntu12

If OS is Ubuntu 13.x i want to take it as ubuntu13

If OS is CentOS 7.x i want to take it as centos7

Is that easy?

THANK YOU

5
  • i think it's better to ask this in unix.stackexchange.com Commented Aug 21, 2014 at 14:48
  • well i wasnt sure where to put it since it's the bash script actually and not the way to get the system os Commented Aug 21, 2014 at 14:48
  • 1
    did you try uname -a + some string manipulation? Commented Aug 21, 2014 at 14:50
  • No, that does not even output the distro name. At least in my VPS. Well i found out that the cat /etc/*-release actualyl works in all distros. The problem is the string now. Commented Aug 21, 2014 at 14:52
  • So, what bash script did you try? How does it not work? This is not a scripting service. Commented Aug 21, 2014 at 15:10

1 Answer 1

1

Here is a bash solution. I tested on Ubuntu, but not on CentOS (I only have RHEL available now). But you can test the CentOS part and modify as needed.

#!/usr/bin/env bash

RELEASE=unknown

version=$( lsb_release -r | grep -oP "[0-9]+" | head -1 )
if lsb_release -d | grep -q "CentOS"; then
    RELEASE=centos$version
elif lsb_release -d | grep -q "Ubuntu"; then
    RELEASE=ubuntu$version
fi

echo $RELEASE

Or, without lsb_release on CentOS:

#!/usr/bin/env bash

RELEASE=unknown

if [ -f /etc/redhat-release ]; then
    version=$( cat /etc/redhat-release | grep -oP "[0-9]+" | head -1 )
    RELEASE=centos$version
elif [ -n $(which lsb_release 2> /dev/null) ] && lsb_release -d | grep -q "Ubuntu"; then
    version=$( lsb_release -d | grep -oP "[0-9]+" | head -1 )
    RELEASE=ubuntu$version
fi

echo $RELEASE

In any case, there's more than one way to skin this cat.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.