Getting OS name and version from Linx os-release file
Note; the order of the contents of /etc/*-release
can't be guaranteed. This answer focus on OP's case;
Use tac
to reverse the output, then use tr '\n' ' '
to concatenate the lines;
cat /etc/*-release | egrep "PRETTY_NAME|VERSION_ID" | cut -d = -f 2 | tr -d '"' | tac | tr '\n' ' '
# 10 Debian GNU/Linux 10 (buster)
Another option would be to read the second and third line of lsb_release
witch has a more consistent output;
lsb_release -as 2>/dev/null | sed -n 2,3p | tac | tr '\n' ' '
# 10 Debian GNU/Linux 10 (buster)
Python: How to determine the specific Linux distribution being used?
In [24]: open('/etc/lsb-release').readline().strip().split('=')[-1]
Out[24]: 'LinuxMint'
Detecting the linux distribution from java
I think parsing a file is the way to go under Linux. You could of course do it only once.
The correct file to find such information on Linux would be /etc/*-release
, which on my machine gives the following output:
$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 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/"
You could then parse ID_LIKE
variable which would give you the exact information you need (I can't guarantee that but I suppose it will always be in the 8th line on every Linux distribution).
Why does platform.linux_distribution() returns different results on one OS?
Ubuntu 14.04 includes two release files:
# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.3 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.3 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/"
root@yeni2:/# cat /etc/debian_version
jessie/sid
Both are used by the function platform.linux_distribution()
However, this function is patched by Ubuntu to return the Ubuntu OS name, see also the comment in the code (right is the file installed by Ubuntu, left is the file from the sources found in Python 2.7.10):
Python-2.7.10 # diff Lib/platform.py /mnt/ubu/\@/usr/lib/python2.7/platform.py
1c1
< #!/usr/bin/env python
---
> #! /usr/bin/python2.7
262c262
< 'UnitedLinux', 'turbolinux')
---
> 'UnitedLinux', 'turbolinux', 'Ubuntu')
290a291,294
> _distributor_id_file_re = re.compile("(?:DISTRIB_ID\s*=)\s*(.*)", re.I)
> _release_file_re = re.compile("(?:DISTRIB_RELEASE\s*=)\s*(.*)", re.I)
> _codename_file_re = re.compile("(?:DISTRIB_CODENAME\s*=)\s*(.*)", re.I)
>
314a319,337
> # check for the LSB /etc/lsb-release file first, needed so
> # that the distribution doesn't get identified as Debian.
> try:
> with open("/etc/lsb-release", "rU") as etclsbrel:
> for line in etclsbrel:
> m = _distributor_id_file_re.search(line)
> if m:
> _u_distname = m.group(1).strip()
> m = _release_file_re.search(line)
> if m:
> _u_version = m.group(1).strip()
> m = _codename_file_re.search(line)
> if m:
> _u_id = m.group(1).strip()
> if _u_distname and _u_version:
> return (_u_distname, _u_version, _u_id)
> except (EnvironmentError, UnboundLocalError):
> pass
>
Your python 2.7.9 compiled the source, does not contain the patch from Ubuntu, that is why it is returning the content of /etc/debian_version
How to get Linux distribution name and version?
Try:
cat /etc/lsb-release
You can also try
lsb_release -a
Or:
cat /proc/version
Extracting OS name and version number in Makefile
Try this
if [ -f /etc/lsb-release ]; then
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
else
OS=$(uname -s)
VER=$(uname -r)
fi
echo $OS
echo $VER
Or like this sort command
OS=$(lsb_release -si)
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VER=$(lsb_release -sr)
echo $OS
echo $VER
echo $ARCH
Or in makefile you require like this
UNAME_OS := $(shell lsb_release -si)
ifeq ($(UNAME_OS),Ubuntu)
$(info YES UBUNTU DETECTED)
else
$(info NO UBUNTU DETECTED)
endif
Related Topics
What Is The Linux Equivalent to Msvc++'s Option /D1Reportsingleclasslayout
How to Translate X11 Keycode Back to Scancode or Hid Usage Id Reliably
Busybox in Embedded Linux Shows "Applet Not Found"
Gcloud - How to Automate Installation of Gcloud on a Server
Time Waste of Execv() and Fork()
Using "And" in Bash While Loop
Short Command to Find Total Size of Files Matching a Wild Card
Opening Default Text Editor in Bash
Installing Oracle Instantclient on Linux Without Setting Environment Variables
Why Is My Linux Application Pulling in The Wrong .So Library
Do I Need to "Enable" a Pcie Memory Region in a Linux 3.12 Driver
Too Many Open Files Error on Lucene
Crystalspace VS. Irrlicht VS. .....