Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Bash脚本中将带有空格的outputstring分配给变量_Bash - Fatal编程技术网

在Bash脚本中将带有空格的outputstring分配给变量

在Bash脚本中将带有空格的outputstring分配给变量,bash,Bash,我想输入一个命令的输出,其中包含一些到var的管道。我编写的代码如下所示: curl https://www.gentoo.org/downloads/signatures/ | grep 0x | cut -d '>' -f3 | cut -d '<' -f1 | while read line; do gpg --recv-keys $line tempfingerprint= `gpg --fingerprint $line | head -2 | tail -1 | cu

我想输入一个命令的输出,其中包含一些到var的管道。我编写的代码如下所示:

curl https://www.gentoo.org/downloads/signatures/ | grep 0x | cut -d '>' -f3 | cut -d '<' -f1  |  while read line; do
gpg --recv-keys $line
tempfingerprint= `gpg --fingerprint $line | head -2 | tail -1 | cut -d'=' -f2 | cut -d ' ' -f2-12`
echo $tempfingerprint

如何将所有指纹分配给变量?

=
后面有一个空格:

tempfingerprint= `gpg --fingerprint $line | head -2 | tail -1 | cut -d'=' -f2 | cut -d ' ' -f2-12`
#               ^
这就是导致错误的原因,请删除它

此外,它不是必需的,但您应该更喜欢
“$(…)”
而不是
“…”
,因为它更安全、更容易阅读:

tempfingerprint="$(gpg --fingerprint $line | head -2 | tail -1 | cut -d'=' -f2 | cut -d ' ' -f2-12)"
通常,总是引用变量展开式

tempfingerprint="$(gpg --fingerprint $line | head -2 | tail -1 | cut -d'=' -f2 | cut -d ' ' -f2-12)"