如何在 bash 的循环内附加到现有的字符串变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19696063/
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
How do I append to an existing string variable inside a loop in bash?
提问by Michael A
I have a simple bash script that downloads stock prices and appends them to a variable, which is then outputted:
我有一个简单的 bash 脚本,它下载股票价格并将它们附加到一个变量中,然后输出:
#!/bin/bash
output=" "
for stock in GOOG AAPL
do
price=$(curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=$stock&f=l1")
output+="$stock: $price "
done
echo "$output"
This script only displays AAPL: 524.896
, the last piece of data fetched. According to whatswrongwithmyscript, there isn't anything wrong with the script, and I thought I was following this answerproperly. This answerdiscussed a similar problem (appending to a string variable inside a loop) and suggested a different method which I used like this:
此脚本仅显示AAPL: 524.896
,最后获取的数据。根据whatswrongwithmyscript,脚本没有任何问题,我以为我正确地遵循了这个答案。这个答案讨论了一个类似的问题(附加到循环内的字符串变量)并建议了一种我这样使用的不同方法:
#!/bin/bash
output=" "
for stock in GOOG AAPL
do
price=$(curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=$stock&f=l1")
output="$output$stock: $price "
done
echo "$output"
The output is still the same. I'm using bash 4.2.45 on debian jessie x64.
输出还是一样的。我在 debian jessie x64 上使用 bash 4.2.45。
More info
更多信息
I echoed the result in a loop to debug, and from the first script, this is what I get:
我在循环中回显了结果以进行调试,从第一个脚本开始,我得到了以下结果:
GOOG: 1030.42
AAPL: 524.896
AAPL: 524.896
And the second script gives the same thing:
第二个脚本给出了同样的东西:
GOOG: 1030.42
AAPL: 524.896
AAPL: 524.896
回答by Dan R
I'm pretty sure that the problem is that curl is returning a carriage return and this is messing with the printing of both values. If you redirect the output of the curl command to a file and view it in vi, you'll see it's created a DOS file.
我很确定问题在于 curl 正在返回一个回车符,这会影响两个值的打印。如果您将 curl 命令的输出重定向到一个文件并在 vi 中查看它,您将看到它创建了一个 DOS 文件。
This seems to work:
这似乎有效:
#!/bin/bash
output=""
for stock in GOOG AAPL
do
price=$(curl -s "http://download.finance.yahoo.com/d/quotes.csv?s=$stock&f=l1" | tr -d '\r')
output+="$stock $price\n"
done
echo -e "$output"
回答by Chris Dodd
When I run your script and pipe the output to od -c
, the result is illuminating:
当我运行您的脚本并将输出通过管道传输到 时od -c
,结果很明显:
0000000 G O O G : 1 0 3 0 . 4 2 \r
0000020 A A P L : 5 2 4 . 8 9 6 \r \n
0000040
So you can see that it IS in fact getting all the entries and concatenating them, but its ALSO getting CR characters (the \r
in the od output), which causes them to print over the top of each other when you print the string.
所以你可以看到它实际上是获取所有条目并将它们连接起来,但它也获取 CR 字符(\r
在 od 输出中),这导致它们在打印字符串时打印在彼此的顶部。
You can pipe the output of curl
to tr -d '\r'
to strip off the problematic CRs:
您可以通过管道curl
传递tr -d '\r'
to的输出以去除有问题的 CR:
price=$(curl -s "...." | tr -d '\r')