我想要一个shell脚本来获取当前的linux发行版名称。
我找到了一个命令:awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower($2) }'
,但输出为"centos"
,需要删除"
。
通过以下方式学习类似的partern:
https://stackoverflow.com/a/25507646/1637673
https://stackoverflow.com/a/20601998/1637673
我最后通过awk '/^ID=/' /etc/*-release | awk -F'=' '{ gsub(/\"/, "") }1 { print tolower($2) }'
解决了这个问题。
但我不明白数字1
在该模式中做了什么。删除1
会破坏该命令。阅读man awk
,awk_regular_expressions和其他教程,其中没有一个提到这种模式。
我在这里复制Floris的解释:
gsub(/^[ \t]+/,"",$2); - starting at the beginning (^) replace all (+ = zero or more, greedy)
consecutive tabs and spaces with an empty string
gsub(/[ \t]+$/,"",$2)} - do the same, but now for all space up to the end of string ($)
1 - ="true". Shorthand for "use default action", which is print $0
- that is, print the entire (modified) line
1 =“true”。 “使用默认操作”的简写,即打印$ 0
但我的脚本中已经有一个操作{ print tolower($2) }
,为什么还需要另一个默认操作?