[英]Extract minor version from kernel to bash variable
I am new to bash and writing a script that needs to compare the minor version of the kernel to see if it is greater than or equal to 10, and exit if it is not. 我是bash的新手,并编写了一个脚本,该脚本需要比较内核的次要版本以查看它是否大于或等于10,如果不是,则退出。 Currently I have something like this:
目前我有这样的事情:
KERNEL=$(uname -r)
declare -i MINOR_VERSION=$(echo $KERNEL | cut -c 3-4)
if [ "10" -gt "$MINOR_VERSION"]; then exit 0; fi
This is bad code, and doesn't work if the minor version is < 10 since I am using cut and depending on it being two digits. 这是错误的代码,如果次要版本<10,则无效,因为我使用的是cut并取决于它是两位数。 What I probably need is something that parses the minor version by using the dots.
我可能需要的是通过点来解析次要版本的东西。 Example:
例:
$ uname -r
3.4.0-60-generic
$ MNR_VAR=<awesome bash code, with cut or sed or something>
$ echo $MNR_VAR
4
I have been reading cut
and sed
documentation but have just been slow picking it up. 我一直在阅读
cut
和sed
文档,但一直很缓慢。 I would appreciate the help! 我将不胜感激!
TL;DR - looking for a bash command that will extract an int
surrounded by the first two dots in a variable. TL; DR-寻找bash命令,该命令将提取变量中前两个点所包围的
int
。 "3.13.0.x" returns '13', "3.2.0.x" returns '2', etc. “ 3.13.0.x”返回“ 13”,“ 3.2.0.x”返回“ 2”,依此类推。
EDIT: Some answers as one liners below for those curious. 编辑:对于那些好奇的人,下面是一些答案。
uname -r | cut -d '.' -f2
uname -r | awk -F . '{print $2}'
kernel="$(uname -r)" | tmp="${kernel#*.}" | minor="${tmp%%.*}" | echo "$minor"
In pure bash: 用纯bash:
#!/bin/bash
ker="$(uname -r)"
minker="${ker#*.}"
minker="${minker%%.*}"
echo "$minker"
"${ker#*.}"
is the string after the first match of a .
"${ker#*.}"
是的第一个匹配项之后的字符串.
in $ker
. 在
$ker
。 Thus $minker
becomes 13.0-generic...
from 3.13.0-generic...
因此
$minker
从3.13.0-generic...
变为13.0-generic...
3.13.0-generic...
"${minker%%.*}"
is the string left by cutting all matches (from right) of .
"${minker%%.*}"
是通过从"${minker%%.*}"
所有匹配项(从右开始)而剩下的字符串.
and whatever after it, in $minker
. 以及之后的任何内容,在
$minker
。 Thus $minker
becomes 13
from 13.0-generic...
因此
$minker
从13.0-generic...
变成13
13.0-generic...
See the Bash Parameter Expansion Manual for more info 有关更多信息,请参见Bash参数扩展手册。
Using Bash Regex: 使用Bash Regex:
#!/bin/bash
regex='([0-9]+)\.([0-9]+)'
[[ $(uname -r) =~ $regex ]]
echo ${BASH_REMATCH[2]}
The problem is you are using -c
to cut
. 问题是您正在使用
-c
cut
。 Don't do that. 不要那样做
Use the -f
and -d
flags instead to control the delimiter and fields to output. 请改用
-f
和-d
标志来控制定界符和要输出的字段。
Or use awk -F . '{print $2}' <<< "$(uname -r)"
或使用
awk -F . '{print $2}' <<< "$(uname -r)"
awk -F . '{print $2}' <<< "$(uname -r)"
. awk -F . '{print $2}' <<< "$(uname -r)"
。
Or use IFS=. read -r _ minor _rest <<< "$(uname -r)"; echo "$minor"
或使用
IFS=. read -r _ minor _rest <<< "$(uname -r)"; echo "$minor"
IFS=. read -r _ minor _rest <<< "$(uname -r)"; echo "$minor"
IFS=. read -r _ minor _rest <<< "$(uname -r)"; echo "$minor"
(which has the benefit of not using any external utilities). IFS=. read -r _ minor _rest <<< "$(uname -r)"; echo "$minor"
(它的好处是不使用任何外部实用程序)。
The usage of <<< "$(uname -r)"
is bash-specific (I believe) but avoids the need for a pipe ( |
) and the sub-shell that it involves. <<< "$(uname -r)"
的用法是bash特定的(我相信),但是避免了对管道( |
)及其涉及的子外壳的需要。
Extracting just minor version & comparing it with something is risky, because major number can change too... 仅提取次要版本并将其与某些内容进行比较是有风险的,因为主要数字也可能会更改...
I normally prefer padding the numbers with zeros, so that they can be easily compared using simple string compare. 我通常更喜欢用零填充数字,以便可以使用简单的字符串比较轻松地比较它们。
kernel_version=$(uname -r | sed -r 's/([0-9]+)/0000\1/g; s/0*([0-9]{4})/\1/g') # gives 0003.0004.0000-0060-generic
if [[ "$kernel_version" < "0003.0010" ]]; then exit 0; fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.