[英]Bash bad substitution with subshell
I am trying to get the Distro name without the quotation marks. 我试图获取不带引号的发行版名称。
cat /etc/*-release | grep "NAME=.*" -o | cut -d "=" -f2 | head -n1
Returns: "CentOS Linux"
返回:
"CentOS Linux"
Now, using shell substitution, I tried to remove the quotation marks my placing the command in a sub-shell: 现在,使用外壳替换,我尝试删除引号,将命令放在子外壳中:
echo ${$(cat /etc/*-release | grep "NAME=.*" -o | cut -d "=" -f2 | head -n1)/\"}
I was thinking I could escape the quote using \\"
and then omitting the last /
to simply delete the quotes. 我当时想我可以使用
\\"
转义引号,然后省略最后一个/
来删除引号。
EDIT: I know this could be done with awk
, but I don't know how. 编辑:我知道这可以用
awk
完成,但我不知道如何。
EDIT: Output of cat /etc/*-release
: 编辑:
cat /etc/*-release
-release的输出:
CentOS Linux release 7.2.1511 (Core)
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"
CentOS Linux release 7.2.1511 (Core)
CentOS Linux release 7.2.1511 (Core)
Expected output: CentOS Linux 预期输出:CentOS Linux
/etc/os-release
appears to be a simple set of shell assignment statement; /etc/os-release
似乎是一组简单的shell分配语句; you might simply source it and output the value of $NAME
(using a subshell to avoid potentially overwriting any variables in the current shell): 您可以简单地获取它并输出
$NAME
的值(使用子shell以避免潜在覆盖当前shell中的任何变量):
( . /etc/os-release; echo "$NAME" )
This might be a security risk, depending on your level of trust that /etc/os-release
could contain other executable code for whatever reason. 这可能是安全隐患,具体取决于您对
/etc/os-release
可能出于任何原因包含其他可执行代码的信任程度。
Depending on your actual needs, you might also simply use /etc/centos-release
, which contains the information you want as a simple string. 根据您的实际需求,您还可以简单地使用
/etc/centos-release
,它以简单的字符串形式包含您想要的信息。 (You might not be assuming that your script will run on a CentOS box, of course. os-release
appears to be a more distro-agnostic version.) (当然,您可能并不假定脚本会在CentOS机器上运行
os-release
似乎是一个与发行版无关的版本。)
Using awk
, getting the OS name following the NAME
variable. 使用
awk
,在NAME
变量之后获取操作系统名称。
awk -F"=" '$1=="NAME"{gsub(/"/, "", $2);print $2; exit}' /etc/os-release
CentOS Linux
Your solution almost works. 您的解决方案几乎可行。 The quotes in
"CentOS Linux"
were part of the release-file, using a subshell doesn't help. "CentOS Linux"
中的引号是发行文件的一部分,使用子外壳无济于事。 You can use cut
with a double quote as delimiter: 您可以使用带有双引号的
cut
作为分隔符:
cat /etc/*-release | grep "NAME=.*" -o | cut -d "=" -f2 | head -n1 | cut -d '"' -f2
You can do the same using sed
with 您可以使用
sed
与
sed -n '/^NAME=/ {s/NAME="\(.*\)".*/\1/p;q}' /etc/*-release
Explanation: Tell sed
only to print lines asked for with a p
, look for a line starting with NAME=
, match the part between the quotes and quit after the first line with NAME=
. 说明:仅告诉
sed
打印要求使用p
的行,查找以NAME=
开头的行,匹配引号之间的部分,并在使用NAME=
的第一行之后退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.