I'm working on a simple bash script that should be able to run on Ubuntu and CentOS distributions (support for Debian and Fedora/RHEL would be a plus) and I need to know the name and version of the distribution the script is running (in order to trigger specific actions, for instance the creation of repositories). So far what I've got is this:
OS=$(awk '/DISTRIB_ID=/' /etc/*-release | sed 's/DISTRIB_ID=//' | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')
VERSION=$(awk '/DISTRIB_RELEASE=/' /etc/*-release | sed 's/DISTRIB_RELEASE=//' | sed 's/[.]0/./')
if [ -z "$OS" ]; then
OS=$(awk '{print 1ドル}' /etc/*-release | tr '[:upper:]' '[:lower:]')
fi
if [ -z "$VERSION" ]; then
VERSION=$(awk '{print 3ドル}' /etc/*-release)
fi
echo $OS
echo $ARCH
echo $VERSION
This seems to work, returning ubuntu
or centos
(I haven't tried others) as the release name. However, I have a feeling that there must be an easier, more reliable way of finding this out -- is that true?
It doesn't work for RedHat. /etc/redhat-release contains : Redhat Linux Entreprise release 5.5
So, the version is not the third word, you'd better use :
OS_MAJOR_VERSION=`sed -rn 's/.*([0-9])\.[0-9].*/1円/p' /etc/redhat-release`
OS_MINOR_VERSION=`sed -rn 's/.*[0-9].([0-9]).*/1円/p' /etc/redhat-release`
echo "RedHat/CentOS $OS_MAJOR_VERSION.$OS_MINOR_VERSION"
21 Answers 21
To get OS
and VER
, the latest standard seems to be /etc/os-release
.
Before that, there was lsb_release
and /etc/lsb-release
. Before that, you had to look for different files for each distribution.
Here's what I'd suggest
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
# Older SuSE/etc.
...
elif [ -f /etc/redhat-release ]; then
# Older Red Hat, CentOS, etc.
...
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
I think uname
to get ARCH
is still the best way. But the example you gave obviously only handles Intel systems. I'd either call it BITS
like this:
case $(uname -m) in
x86_64)
BITS=64
;;
i*86)
BITS=32
;;
*)
BITS=?
;;
esac
Or change ARCH
to be the more common, yet unambiguous versions: x86
and x64
or similar:
case $(uname -m) in
x86_64)
ARCH=x64 # or AMD64 or Intel64 or whatever
;;
i*86)
ARCH=x86 # or IA32 or Intel32 or whatever
;;
*)
# leave ARCH as-is
;;
esac
but of course that's up to you.
-
2I would check for lsb_release and use it if available, but it isn't available everywhere. For instance, it isn't in the default install of Fedora 14.Steven D– Steven D2011年01月24日 05:47:06 +00:00Commented Jan 24, 2011 at 5:47
-
1
rpm -qa "lsb*
prints nothing. According toyum provides */lsb_release
it is contained in a package called redhat-lsb which I guess is not installed by default. However, I'm not a Fedora expert, these are just the results from a random Fedora VM I have and could be very wrong.Steven D– Steven D2011年01月24日 06:01:50 +00:00Commented Jan 24, 2011 at 6:01 -
3Just installed Fedora 14 in a VM, and there is no
lsb-release
, as you said.Mikel– Mikel2011年01月24日 06:07:13 +00:00Commented Jan 24, 2011 at 6:07 -
1@Mikel:
lsb_release
seems to work great on Ubuntu 10.04 LTS, however in Fedora and CentOS I get a "command not found" error message. Also,/etc/lsb-release
doesn't seem to exists in RHEL distros. On Fedora 13 for instance I havefedora-release
,redhat-release
andsystem-release
.Alix Axel– Alix Axel2011年01月24日 06:24:13 +00:00Commented Jan 24, 2011 at 6:24 -
8Don't use
$OS
,$VERSION
, etc -- by convention, we capitalize environment variables (PAGER, EDITOR, SHELL, ...) and internal shell variables (BASH_VERSION, RANDOM, ...). All other variable names should contain at least one lowercase letter. This convention avoids accidentally overriding environmental and internal variables.Chris Down– Chris Down2011年09月16日 20:18:59 +00:00Commented Sep 16, 2011 at 20:18
I'd go with this as a first step:
ls /etc/*release
Gentoo, RedHat, Arch & SuSE have a file called e.g. /etc/gentoo-release
. Seems to be popular, check this site about release-files.
Debian & Ubuntu should have a /etc/lsb-release
which contains release info also, and will show up with the previous command.
Another quick one is uname -rv
. If the kernel installed is the stock distro kernel, you'll (削除) usually (削除ここまで) sometimes find the name in there.
-
On Debian, that's
uname -a
.tshepang– tshepang2011年03月29日 20:31:41 +00:00Commented Mar 29, 2011 at 20:31 -
3doesn't
uname -rv
contain the info though?uname -a
will print things like type of processor and hostname that are irrelevant here. (But both kernel release (-r
) and version (-v
) are necessary, not all distros do it the same there)Mat– Mat2011年03月29日 20:34:16 +00:00Commented Mar 29, 2011 at 20:34 -
uname -r gives
2.6.32-5-686-bigmem
and uname -v gives#1 SMP Tue Mar 8 22:14:55 UTC 2011
on Debian 6. uname -a OTOH givesLinux debian 2.6.32-5-686-bigmem #1 SMP Tue Mar 8 22:14:55 UTC 2011 i686 GNU/Linux
.tshepang– tshepang2011年03月29日 22:22:23 +00:00Commented Mar 29, 2011 at 22:22 -
the second part of
uname -a
is-n
, i.e. nodename == hostname. so I'd sayuname
will not be interesting on Debian. Come to think of it, I'm not sure it is so useful on RedHat either.Mat– Mat2011年03月29日 22:26:13 +00:00Commented Mar 29, 2011 at 22:26 -
Oh, ok. So it's just by luck that I set my hostname as default,
debian
.tshepang– tshepang2011年03月29日 22:29:24 +00:00Commented Mar 29, 2011 at 22:29
lsb_release -a
. Works on Debian and I guess Ubuntu, but I'm not sure about the rest. Normally it should exist in all GNU/Linux distributions since it is LSB (Linux Standard Base) related.
-
3It's not installed by default on Gentoo. It wasn't by default on Fedora either at some point (probably is now though)Mat– Mat2011年03月29日 19:13:06 +00:00Commented Mar 29, 2011 at 19:13
-
2This worked on Debian squeeze, Fedora 14, and OpenSUSE 11.3. On Debian and SUSE the package containing lsb_release was lsb-release, on Fedora it was redhat-lsb. It was already installed on Debian and SUSE, but not on Fedora. I think this is the best answer so far. +1.Faheem Mitha– Faheem Mitha2011年03月29日 20:09:51 +00:00Commented Mar 29, 2011 at 20:09
-
3lsb_release is not part of the CentOS 6 minimal install.a coder– a coder2013年10月08日 15:30:41 +00:00Commented Oct 8, 2013 at 15:30
-
2lsb_release -a returns "-bash: lsb_release: command not found" on Raspberry with Raspbian (Debian 7.1 derived).Peter Mortensen– Peter Mortensen2014年06月05日 11:32:32 +00:00Commented Jun 5, 2014 at 11:32
-
lsb_release -a works on Red Hat Enterprise Linux Server release 6.6 (Santiago)jwilleke– jwilleke2015年01月28日 13:56:18 +00:00Commented Jan 28, 2015 at 13:56
One-liner, fallbacks, one line of output, no errors.
( lsb_release -ds || cat /etc/*release || uname -om ) 2>/dev/null | head -n1
-
Beautiful, thank you. Note this still doesn't work on Mac OS X, but it works fine on Ubuntu and CentOS (which are the three I have handy).Wildcard– Wildcard2015年12月23日 03:32:31 +00:00Commented Dec 23, 2015 at 3:32
-
2Brilliant! Why such a bueatiful answer not on the top?Neo– Neo2017年03月28日 02:08:18 +00:00Commented Mar 28, 2017 at 2:08
-
Sadly it falls apart for
/etc/os-release
on Git for Windows SDK (MSYS2), because it will result inNAME=MSYS
whenuname -om
gives a uniquer answer (x86_64 Msys
).Martin Braun– Martin Braun2024年01月24日 23:57:30 +00:00Commented Jan 24, 2024 at 23:57 -
Be aware that few (if any) of the various
/etc/*release
files don't require their lines to be in any particular order. Depending on the first line to be a specific field is fragile, so while using this as a generic one-liner on the command line probably feels great I wouldn't depend upon it for a shell script.Ti Strga– Ti Strga2024年12月30日 22:34:12 +00:00Commented Dec 30, 2024 at 22:34
python -m platform
sample outputs:
Ubuntu:
Linux-4.9.184-linuxkit-x86_64-with-Ubuntu-18.04-bionic
Debian:
Linux-4.14.117-grsec-grsec+-x86_64-with-debian-buster-sid
Centos:
Linux-3.10.0-957.1.3.el7.x86_64-x86_64-with-centos-7.6.1810-Core
Mac OS X:
Darwin-17.7.0-x86_64-i386-64bit
See platform module docs if you need a specific value from that line. For example, if you need the Linux distro name only, use
python -c "import platform;print(platform.linux_distribution()[0])"
-
@ThiefMaster Here are a few sample outputs of the command: * Linux-3.10.0-957.1.3.el7.x86_64-x86_64-with-centos-7.6.1810-Core * Linux-4.14.117-grsec-grsec+-x86_64-with-debian-buster-sid Is there anything that is not clear about the linux distribution here?Ilya Kharlamov– Ilya Kharlamov2020年01月07日 15:06:47 +00:00Commented Jan 7, 2020 at 15:06
-
OK, so on those distros it contains some information. In any case, it's arbitrary text and thus not really useful to use in scripts... on Gentoo I have this for example:
inux-4.19.86-gentoo-x86_64-Intel-R-_Core-TM-_i5-8259U_CPU_@_2.30GHz-with-glibc2.4
ThiefMaster– ThiefMaster2020年01月07日 17:35:01 +00:00Commented Jan 7, 2020 at 17:35 -
@ThiefMaster Updated the original answer on how to get the specific value instead of arbitrary text.Ilya Kharlamov– Ilya Kharlamov2020年01月08日 20:34:16 +00:00Commented Jan 8, 2020 at 20:34
- lsb-* isn't installed/doesn't exist on base CentOS or Debian systems
- /proc/* doesn't exist on OSX
Take a tip from JavaScript developers: Don't test for the version, but for the capability. It's not pretty, but it works. Expand as necessary.
function os_type {
case `uname` in
Linux )
LINUX=1
which yum && { echo "CentOS"; return; }
which zypper && { echo "openSUSE"; return; }
which apt-get && { echo "Debian"; return; }
;;
Darwin )
DARWIN=1
;;
* )
# Handle AmigaOS, CPM, and modified cable modems.
;;
esac
}
-
7Yum could very well be Red Hat, Scientific Linux, or Fedora. There was an apt command for Fedora mimiking Debian's. And so on. Just clasifying anything with yum as CentOS gives you a range of supported systems from Red Hat 4 to Fedora 18, that is some 8 years of Linux history right there. If you need to know if <foo> is supported, specifically check for <foo> instead of guessing based on (unreliable) distribution identification! Sure, it leads to something of the ilk of autotools, but that can't be helped (just thank $DEITY that the Unix wars are over).vonbrand– vonbrand2013年01月18日 03:21:25 +00:00Commented Jan 18, 2013 at 3:21
-
@vonbrand indeed, it is as you say. i don't attempt to differentiate between sub-flavours. and the order i check in (hopefully) removes most tricky situations (RHEL with apt-get installed), although i didn't do a lot of research. if RHEL can have apt-get installed, and Ubuntu users have yum... well, you're just bang out of luck.Orwellophile– Orwellophile2013年01月22日 02:40:00 +00:00Commented Jan 22, 2013 at 2:40
-
apt-get is also used by altlinux on rpm bazis, so this is the false answerМалъ Скрылевъ– Малъ Скрылевъ2021年03月10日 07:51:02 +00:00Commented Mar 10, 2021 at 7:51
-
1@МалъСкрылевъ I think my general point was: if you are only identifying the operating system in order to determine which package manager to run, then just check for the package manager. That's the "JavaScript" test-for-feature concept. FWIW, I have
apt-get
running here under cygwin (apt-cyg renamed). I should add that there entire concept of "test-for-feature" has worked for decades withconfigure
and to an extent the more recentcmake
build systems.Orwellophile– Orwellophile2021年03月10日 11:12:24 +00:00Commented Mar 10, 2021 at 11:12 -
well of example when I run the script on my linux it will return "debian" what is wrong, please note it on our postМалъ Скрылевъ– Малъ Скрылевъ2021年03月10日 18:16:55 +00:00Commented Mar 10, 2021 at 18:16
In order of most probable success, these:
cat /etc/*version
cat /proc/version #linprocfs/version for FreeBSD when "linux" enabled
cat /etc/*release
uname -rv
cover most cases (AFAIK): Debian, Ubuntu, Slackware, Suse, Redhat, Gentoo, *BSD and perhaps others.
-
cat /etc/*version
does not work (there is no such file) on CentOS (at least on 6.4)om-nom-nom– om-nom-nom2013年10月23日 12:32:08 +00:00Commented Oct 23, 2013 at 12:32 -
If you run the command in a container, /proc/version will contain the HOST distribution version, and not the container distribution one.Dereckson– Dereckson2015年03月24日 20:14:06 +00:00Commented Mar 24, 2015 at 20:14
For most modern Linux OS systems, the file /etc/os-release
is really becoming standard and is getting included in most OS. So inside your Bash script you can just include the file, and you will have access to all variables described here (for example: NAME, VERSION, ...)
So I'm just using this in my Bash script:
if [ -f /etc/os-release ]
then
. /etc/os-release
else
echo "ERROR: I need the file /etc/os-release to determine what my distribution is..."
# If you want, you can include older or distribution specific files here...
exit
fi
-
1Just bear in mind that sourcing this file introduces another place to hide stuff. unix.stackexchange.com/a/433245/5132JdeBP– JdeBP2020年03月10日 19:42:10 +00:00Commented Mar 10, 2020 at 19:42
If the file /etc/debian_version
exists, the distribution is Debian, or a Debian derivative. This file may have a release number; on my machine it is currently 6.0.1
. If it is testing or unstable, it may say testing/unstable, or it may have the number of the upcoming release. My impression is that on Ubuntu at least, this file is always testing/unstable, and that they don't put the release number in it, but someone can correct me if I am wrong.
Fedora (recent releases at least), have a similar file, namely /etc/fedora-release
.
-
Newer Debian versions use /etc/os-release instead.Dereckson– Dereckson2015年03月24日 20:14:41 +00:00Commented Mar 24, 2015 at 20:14
-
1@Dereckson I checked my wheezy machine, and it has both
/etc/debian_version
and/etc/os-release
.Faheem Mitha– Faheem Mitha2015年03月24日 20:17:20 +00:00Commented Mar 24, 2015 at 20:17 -
Tested in a Jessie chroot created with debootstrap. But indeed, a local machine also under Jessie, this time installed a more conventional way, contains a
/etc/debian_version
file. So the way we prepare a Debian installation could affect the available release files, that's interesting.Dereckson– Dereckson2015年03月28日 10:05:35 +00:00Commented Mar 28, 2015 at 10:05 -
Debian's
os-release
does not include the full version number fromdebian_version
. See unix.stackexchange.com/questions/382531 .JdeBP– JdeBP2017年07月29日 07:08:44 +00:00Commented Jul 29, 2017 at 7:08
If you can't or don't want to use the LSB release file (due to the dependencies the package brings in), you can look for the distro-specific release files. Bcfg2 has a probe for the distro you might be able to use: http://trac.mcs.anl.gov/projects/bcfg2/browser/doc/server/plugins/probes/group.txt.
2 ways from many:
1) use
lsb_release -a
I tested it on CentOS 5.5 and Ubuntu 10.04
the output for CentOS is:
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description: CentOS release 5.5 (Final)
Release: 5.5
Codename: Final
and for Ubuntu is:
LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description: CentOS release 5.5 (Final)
Release: 5.5
Codename: Final
2) enter the following command:
cat /etc/*-release
I tested it on CentOS 5.5 and Ubuntu 10.04, and it works fine.
-
lsb_release is not in CentOS 6 (the min install fwiw)a coder– a coder2013年10月08日 15:34:14 +00:00Commented Oct 8, 2013 at 15:34
-
1lsb_release -a returns "-bash: lsb_release: command not found" on Raspberry with Raspbian (Debian 7.1 derived).Peter Mortensen– Peter Mortensen2014年06月05日 11:34:55 +00:00Commented Jun 5, 2014 at 11:34
-
Under Centos 7, you need the
redhat-lsb-core
package from EPEL installed, because why bother installing the "Linux Standard Base Core" package by default? Apparently you need thelsb-release
package installed under Debian and derivatives.Ben Stern– Ben Stern2020年05月29日 01:11:55 +00:00Commented May 29, 2020 at 1:11
This script works on Debian, (may need some tweak on Ubuntu)
#!/usr/bin/env bash
echo "Finding Debian/ Ubuntu Codename..."
CODENAME=`cat /etc/*-release | grep "VERSION="`
CODENAME=${CODENAME##*\(}
CODENAME=${CODENAME%%\)*}
echo "$CODENAME"
# => saucy, precise, lucid, wheezy, squeeze
I find a good script from here which works for most of common linux dists:
#! /bin/bash
# return an awkable string consisting of
# unix OS type, or
# Linux dist, or
# a long guess (based on /proc), or
# no clue
giveUp () {
echo "Unknown"
exit 0
}
# keep this easily awkable, prepending an initial clue
versionGuess () {
if [ -e /proc/version ]; then
echo -n "Unsure "
cat /proc/version
exit 0
fi
return 1
}
# if we have ignition, print and exit
gotDist () {
[ -n "1ドル" ] && echo "1ドル" && exit 0
}
# we are only interested in a single word "dist" here
# various malformations can occur; admin will have to code appropately based on output
linuxRelease () {
if [ -r /etc/lsb-release ]; then
dist=$(grep 'DISTRIB_ID' /etc/lsb-release | sed 's/DISTRIB_ID=//' | head -1)
gotDist "$dist"
fi
dist=$(find /etc/ -maxdepth 1 -name '*release' 2> /dev/null | sed 's/\/etc\///' | sed 's/-release//' | head -1)
gotDist "$dist"
dist=$(find /etc/ -maxdepth 1 -name '*version' 2> /dev/null | sed 's/\/etc\///' | sed 's/-version//' | head -1)
gotDist "$dist"
return 1
}
# start with uname and branch the decision from there
dist=$(uname -s 2> /dev/null)
if [ "$dist" = "Linux" ]; then
linuxRelease
versionGuess
giveUp
elif [ -n "$dist" ]; then
echo "$dist"
exit 0
else
versionGuess
giveUp
fi
# we shouldn't get here
giveUp
# done
Some distros use *-issue files, some use *-version, some use *-release, or any combination of those 3 variations. Dirty and dirty way :
[root@radio ~]# for file in /etc/{*issue*,*version*,*release*}; do [[ -f $file ]] || continue; echo -- $file; cat $file; done;
-- /etc/issue
\S
Kernel \r on an \m
-- /etc/issue.net
\S
Kernel \r on an \m
-- /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
-- /etc/centos-release-upstream
Derived from Red Hat Enterprise Linux 7.8 (Source)
-- /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
-- /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
-- /etc/system-release
CentOS Linux release 7.9.2009 (Core)
-- /etc/system-release-cpe
cpe:/o:centos:centos:7
[root@radio ~]#
Since many have a file in the /etc folder, ending in the word "release" you can use this
cat $(find /etc -maxdepth 1 -readable -type f -name *release)
which will give like
Rocky Linux release 8.6 (Green Obsidian)
Here is my simple chech_distro version. ^^;
#!/usr/bin/env bash
check_distro2(){
if [[ -e /etc/redhat-release ]]
then
DISTRO=$(cat /etc/redhat-release)
elif [[ -e /usr/bin/lsb_release ]]
then
DISTRO=$(lsb_release -d | awk -F ':' '{print 2ドル}')
elif [[ -e /etc/issue ]]
then
DISTRO=$(cat /etc/issue)
else
DISTRO=$(cat /proc/version)
fi
}
check_distro2
echo $DISTRO
Without version, just only dist:
cat /etc/issue | head -n +1 | awk '{print 1ドル}'
-
1An answer from a year earlier by Yahya Yahyaoui said the same thing. Both of these answers were in fact addressed in 2011 in answers that have since been deleted. The reasons that they are wrong still apply.JdeBP– JdeBP2017年07月29日 06:56:59 +00:00Commented Jul 29, 2017 at 6:56
This was tested on Ubuntu 14 and CentOS 7
cat /etc/os-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/["]//g' | awk '{print 1ドル}'
-
@don_crissti How would you do it?BsdHelper– BsdHelper2015年11月23日 01:18:43 +00:00Commented Nov 23, 2015 at 1:18
-
sed -n -e '/PRETTY_NAME/ s/^.*=\|"\| .*//gp' /etc/os-release
cas– cas2015年11月23日 01:41:35 +00:00Commented Nov 23, 2015 at 1:41 -
@cas Very nice! Adding that to my toolbox. Thank you for sharing.BsdHelper– BsdHelper2015年11月24日 06:35:42 +00:00Commented Nov 24, 2015 at 6:35
If somebody needs the distro as well:
#!/usr/bin/env bash
LINUX_VERSION_NAME=`lsb_release -sr`
# Distribution specific installation
if [[ ${LINUX_VERSION_NAME} == "18.04" ]]
then
echo "It is: ${LINUX_VERSION_NAME}"
else
echo "It is not ${LINUX_VERSION_NAME}"
fi
Output:
It is: 18.04
Change to: lsb_release -sc to get the distro:
LINUX_VERSION_NAME=`lsb_release -sc`
# Distribution specific installation
if [[ ${LINUX_VERSION_NAME} == "bionic" ]]
then
echo "It is: ${LINUX_VERSION_NAME}"
else
echo "It is not ${LINUX_VERSION_NAME}"
fi
Output:
It is: bionic
-
Won't your output of the first example be wrong, if you are not on
18.04
for a server that has does have thelsb_release
command in the$PATH
?Mark Stewart– Mark Stewart2021年06月14日 19:52:27 +00:00Commented Jun 14, 2021 at 19:52
Since the question doesn't specify restriction to /bin/sh etc, there is the solution with ruby shell interpreter.
There is a facter ruby gem, which gives you some facts about os OS, it analyzes OS release files, other data and prints to terminal screen. You can try is as follows, begining with rubygems installation:
# apt-get install ruby rubygems
Please use the case above that is eligible for your OS. Then install the gem itself.
# gem install facter
Then use:
$ facter
NOTE: See the facter gem sources to get more info in installation.
-
downvers argue?Малъ Скрылевъ– Малъ Скрылевъ2020年09月14日 11:35:44 +00:00Commented Sep 14, 2020 at 11:35
-
maybe because it requires ruby. I think we want only logic for
/bin/sh
airtonix– airtonix2020年10月08日 22:34:53 +00:00Commented Oct 8, 2020 at 22:34 -
@airtonix and where is the resitriction to "sh" in the question?Малъ Скрылевъ– Малъ Скрылевъ2020年10月09日 21:46:37 +00:00Commented Oct 9, 2020 at 21:46
-
@МалъСкрылевъ it's implied by the tag shell-script. Not that I'm downvoting, mindChris Davies– Chris Davies2020年11月06日 22:57:56 +00:00Commented Nov 6, 2020 at 22:57
-
1@МалъСкрылевъ should be obvious? you want a general function to return the platform name and so forth... but you choose to implement it in a way where it's useless on most platforms? Typically this operation is performed before installing anything else. so that means you have to work with /bin/shairtonix– airtonix2021年02月27日 03:55:37 +00:00Commented Feb 27, 2021 at 3:55
This command works for Debian based and Redhat based distributions:
Using tr
filter, you convert the document to one-word-per-line format and then count the first line which contains the distribution name.
tr -s ' 011円' '012円' < /etc/issue | head -n 1
-
The comments on the now-deleted answer from 2011 that said the same thing, still apply. As does the mis-placed answer, again now deleted, that said that
/etc/issue
did not contain a version or a name on zenwalk. This is an administrator-modifiable file containing a message presented to humans that has no guarantees of containing anything that can be pulled out programatically.JdeBP– JdeBP2017年07月29日 06:54:03 +00:00Commented Jul 29, 2017 at 6:54
+-release
works? Effectively you're looking assuming it will be/etc/lsb-release
, so perhaps just call it that.*
with+
to avoid the comment formating, it should beetc/*-release
, it seems to work.uname -rv | grep -i "name_of_distro"
and use exit code?$ awk -F= '1ドル ~ /ID|VERSION_ID/ {print 2ドル;}' /etc/os-release
; orawk -F= '1ドル ~ /DISTRIB_ID|DISTRIB_RELEASE/ {print 2ドル;}' /etc/lsb-release