bash / shell:在命令行上获取内核名称组件的最简单方法

时间:2022-03-31 23:02:32

On my Fedora machine I sometimes need to find out certain components of the kernel name, e.g.

在我的Fedora机器上,我有时需要找出内核名称的某些组件,例如

VERSION=3.18.9-200.fc21
VERSION_ARCH=3.18.9-200.fc21.x86_64
SHORT_VERSION=3.18
DIST_VERSION=fc21
EXTRAVERSION = -200.fc21.x86_64

I know uname -a/-r/-m but these give me not all the components I need.

我知道uname -a / -r / -m但是这些并不是我需要的所有组件。

Of course I can just disassemble uname -r e.g.

当然我可以拆解uname -r,例如

KERNEL_VERSION_ARCH=$(uname -r)
KERNEL_VERSION=$(uname -r | cut -d '.' -f 1-4)
KERNEL_SHORT_VERSION=$(uname -r | cut -d '.' -f 1-2)
KERNEL_DIST_VERSION=$(uname -r | cut -d '.' -f 4)
EXTRAVERSION="-$(uname -r | cut -d '-' -f 2)"

But this seems very cumbersome and not future-safe to me.

但这对我来说似乎非常麻烦,对我来说不安全。

Question: is there an elegant way (i.e. more readable and distribution aware) to get all kernel version/name components I need?

问题:是否有一种优雅的方式(即更具可读性和分布感)来获取我需要的所有内核版本/名称组件?

Nice would be s.th. like

尼斯将是s.th.喜欢

kernel-ver -f "%M.%m.%p-%e.%a"
3.19.4-200.fc21.x86_64
kernel-ver -f "%M.%m"
3.19
kernel-ver -f "%d"
fc21

1 个解决方案

#1


Of course the uname -r part would need a bit sed/awk/grep magic. But there are some other options you can try:

当然uname -r部分需要一点sed / awk / grep魔法。但是您可以尝试其他一些选项:

  • cat /etc/os-release
  • cat /etc/lsb-release
  • Since it's fedora you can try: cat /etc/fedora-release
  • 因为它是fedora你可以尝试:cat / etc / fedora-release

  • lsb_release -a is also worth a try.
  • lsb_release -a也值得一试。

  • cat /proc/version, but that nearly the same output as uname -a
  • cat / proc / version,但与uname -a的输出几乎相同

In the files /etc/*-release the format is already VARIABLE=value, so you could source the file directly and access the variables later:

在文件/ etc / * - release中,格式已经是VARIABLE = value,因此您可以直接获取文件并稍后访问变量:

$ source /etc/os-release
$ echo $ID
fedora

To sum this up a command that should work on every system that combines the above ideas:

总结一个命令应该适用于结合上述想法的每个系统:

cat /etc/*_ver* /etc/*-rel* 2>/dev/null

#1


Of course the uname -r part would need a bit sed/awk/grep magic. But there are some other options you can try:

当然uname -r部分需要一点sed / awk / grep魔法。但是您可以尝试其他一些选项:

  • cat /etc/os-release
  • cat /etc/lsb-release
  • Since it's fedora you can try: cat /etc/fedora-release
  • 因为它是fedora你可以尝试:cat / etc / fedora-release

  • lsb_release -a is also worth a try.
  • lsb_release -a也值得一试。

  • cat /proc/version, but that nearly the same output as uname -a
  • cat / proc / version,但与uname -a的输出几乎相同

In the files /etc/*-release the format is already VARIABLE=value, so you could source the file directly and access the variables later:

在文件/ etc / * - release中,格式已经是VARIABLE = value,因此您可以直接获取文件并稍后访问变量:

$ source /etc/os-release
$ echo $ID
fedora

To sum this up a command that should work on every system that combines the above ideas:

总结一个命令应该适用于结合上述想法的每个系统:

cat /etc/*_ver* /etc/*-rel* 2>/dev/null