在bash中串联字符串会覆盖它们 [英] Concatenating strings in bash overwrites them
问题描述
我正在解析mysql命令的查询结果(带有--table参数)
I'm parsing query results from a mysql command (with the --table parameter)
local records=`echo "${query}" | $MYSQL -u $MyUSER -h $MyHOST -p$MyPASS --table`
查询成功运行,并且我收到了很好的数据.
The query is run successfully, and I receive good data.
然后我遍历此数据:
for data in $records ;
do
test+=$data
done
代码更广泛,但是基本上就是这样. 但是Bash会将每个空格都视为分隔符,这对于文本字段来说是个问题.
The code is more extensive, but this is basically it. Bash sees every space as a separator though, and that's a problem for text fields.
所以我将它们串联起来.但是当我喂bash这些数据时:
So I just concatenate them. But when I feed bash this data:
*URL*
host:
test.url.com
pass:
anothertest
http://www.test.com
它将其连接为类似的内容:
It concatenates it to something like:
pass:test.url.com.com
好像不是在串联,而是在覆盖. 这可能是回车的问题吗?
As if it's not concatenating, but overwriting. Is this maybe some carriage return problem?
推荐答案
我对从curl
命令检索的stderr输出有相同的问题.我发现输出包含需要删除的回车符.对于上面的示例,可以使用tr
工具完成此操作:
I had the same issue with the stderr output retrieved from the curl
command. I found that the output contains carriage return that needs to be removed. For the example above this could be done using the tr
tool:
for data in $records ;
do
data=$(echo "$data" | tr -d '\r')
test+="$data"
done
这篇关于在bash中串联字符串会覆盖它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!