bash 脚本抱怨文件名太长
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46959151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash script complains about file name too long
提问by Jerry Skidmore
So I have a script that does this (jq is a command line JSON processor):
所以我有一个执行此操作的脚本(jq 是命令行 JSON 处理器):
echo "Getting LB Node IDs"
echo $LB_STATUS | jq '.loadBalancer.nodes[] .id'
The output of the last line is:
最后一行的输出是:
1
2
3
But when I try and assign it to an array:
但是当我尝试将它分配给一个数组时:
echo "Creating an Array"
nodeIdArray=($($LB_STATUS | jq '.loadBalancer.nodes[] .id'))
I get this error:
我收到此错误:
./myShellScript.sh: line 53: {"loadBalancer":{"name":"lbName","id":1,"protocol":"HTTP","port":80,"algorithm":"WEIGHTED_LEAST_CONNECTIONS","status":"ACTIVE","cluster":{"name":"ztm-n22.dfw1.lbaas.rackspace.net"},"nodes":[{"address":"1.2.3.4","id":1,"type":"PRIMARY","port":80,"status":"ONLINE","condition":"ENABLED","weight":1},{"address":"1.2.3.4","id":2,"type":"PRIMARY","port":80,"status":"ONLINE","condition":"ENABLED","weight":1},{"address":"1.2.3.4","id":3,"type":"PRIMARY","port":80,"status":"ONLINE","condition":"ENABLED","weight":1}],"timeout":30,"created":{"time":"2016-06-28T22:14:24Z"},"healthMonitor":{"type":"CONNECT","delay":10,"timeout":5,"attemptsBeforeDeactivation":2},"sslTermination":...<A BOAT LOAD MORE JSON I CUT OUT FOR BREVITY'S SAKE>: File name too long
SO $LB_STATUS | jq '.loadBalancer.nodes[] .id' produces a few numbers while trying to assign those numbers to an array doesn't work
所以 $LB_STATUS | jq '.loadBalancer.nodes[] .id' 在尝试将这些数字分配给数组时会产生一些数字不起作用
回答by Charles Duffy
What Went Wrong
什么地方出了错
$variable | something
doesn't pass the text in variable
as input to something
-- instead, it runs the contents of $variable
as a command. Presumably you wanted echo "$variable" | something
instead (but see below!)
$variable | something
不会将文本variable
作为输入传递给something
-- 而是将其内容$variable
作为命令运行。大概你想要echo "$variable" | something
(但见下文!)
Even if that were fixed, the array=( $(some-command) )
idiom is itself buggy. See BashPitfalls #50describing why it shouldn't be used, and the various alternatives.
即使修复了,这个array=( $(some-command) )
习语本身也是有问题的。请参阅BashPitfalls #50描述为什么不应该使用它,以及各种替代方案。
What To Do Instead
该怎么做
When feeding content from a variable as an input to a command, it's idiomatic to use a herestring: somecommand <<<"$variable"
. These aren't free (as they create temporary files), but they're cheaper than pipelines (which fork off subshells).
当将变量中的内容作为命令的输入提供时,使用 herestring: 是惯用的做法somecommand <<<"$variable"
。这些不是免费的(因为它们会创建临时文件),但它们比管道(分叉子外壳)便宜。
If you have bash 4.x or newer, you have readarray
:
如果你有 bash 4.x 或更新版本,你有readarray
:
readarray -t nodeIdArray < <(jq -r '.loadBalancer.nodes[].id' <<<"$LB_STATUS")
If you need compatibility with bash 3.x, read -a
can do the job:
如果您需要与 bash 3.x 兼容,read -a
可以完成以下工作:
IFS=$'\n' read -r -d '' -a nodeIdArray \
< <(jq -r '.loadBalancer.nodes[].id' <<<"$LB_STATUS" && printf 'nodeIdArray=($($LB_STATUS | jq '.loadBalancer.nodes[] .id'))
')
...which also has the advantage of causing read
to return a nonzero exit status if the jq
command fails.
...read
如果jq
命令失败,它还具有导致返回非零退出状态的优点。
回答by Hyman
You left out the echo
.
你遗漏了echo
.
Change
改变
nodeIdArray=($( echo $LB_STATUS | jq '.loadBalancer.nodes[] .id' ))
to
到
##代码##