For example, to distinguish between Linux and Windows, I can use (when (eq system-type 'gnu/linux)
.
Is there a way to distinguish between Linux distributions, such as Ubuntu vs CentOS?
For example, to distinguish between Linux and Windows, I can use (when (eq system-type 'gnu/linux)
.
Is there a way to distinguish between Linux distributions, such as Ubuntu vs CentOS?
Here's a quick custom function definition for your init file:
(defun which-linux-distribution ()
"from lsb_release"
(interactive)
(when (eq system-type 'gnu/linux)
(shell-command-to-string "lsb_release -sd")))
then run it as:
M-x which-linux-distribution
or test for a known distribution, for example, as:
(when (eq which-linux-distribution 'LinuxMint)
....)
for unknown distributions or systems without the lsb_release
tool, which-linux-distribution
returns null, thereby stopping at the when
test.
There's no real standard method to detect the Linux distribution that a program is running under. Well, actually, there's a de jure standard: the lsb_release
program. lsb_release -sir
displays the name of the distribution and its version number. However, many Linux systems don't have the lsb_release
program installed. Several distributions don't include it in the default installation.
However, you probably don't want to do this. How will you cope with unknown distributions? Say you have code for Debian and Fedora, and someone creates a new distribution derived from Debian, with its own name: your code wouldn't know that it should use the Debian-specific code.
Instead of testing the distribution, test for the behavior that matters. If different distributions put files in different directories, look for the directory that contains the file you want. If different distributions include different packages, check which packages are available. If a distribution has fixed a bug, test whether the bug is present and apply your workaround only if it is, and so on.
The variable operating-system-release
might be helpful. On my Arch Linux VPS, it is set to 3.14.1-1-ARCH
. On Windows, it's nil
.
So to check if I'm on Arch, I would do something like this:
(when (string-match-p "ARCH" operating-system-release)
...)
This variable contains the same value output by the Linux command uname -r
.
using command cat /etc/*release
maybe find out distribution.It is not always useful.I can get the below output with it.
Arch Linux release
LSB_VERSION=1.4
DISTRIB_ID=Arch
DISTRIB_RELEASE=rolling
DISTRIB_DESCRIPTION="Arch Linux"
NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
ID_LIKE=archlinux
ANSI_COLOR="0;36"
HOME_URL="https://www.archlinux.org/"
SUPPORT_URL="https://bbs.archlinux.org/"
BUG_REPORT_URL="https://bugs.archlinux.org/"
I can get distribution and the based distribution by ID and ID_LIKE,so I use the following elisp do it.
(defun guess-linux-release(regexp)
"Guess linux release"
(let ((maybe-get-dis-str (shell-command-to-string "cat /etc/*release")))
(with-temp-buffer
(insert maybe-get-dis-str)
(beginning-of-buffer)
(condition-case nil
(progn
(search-forward-regexp regexp)
(downcase (buffer-substring (match-beginning 1) (match-end 1))))
(search-failed nil)))))
(defun guess-linux-based-distribution()
"Guess linux distribution family"
(guess-linux-release "^ID_LIKE=\"?\\([a-zA-Z ]*\\)\"?$"))
(defun guess-linux-distribution()
"Guess linux distribution"
(guess-linux-release "^^^^^ID=\"?\\(\\w*\\)\"?$"))