bash 建立一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11983069/
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
building a string
提问by Pascal
I have this code:
我有这个代码:
#!/bin/bash
input="./user.cvs"
while IFS=';' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13
do
path="./QRcodes/$f2$f3.png"
vcard="BEGIN:VCARD%0AN;CHARSET=utf-8:$f3;$f2;;$f1;%0AADR;CHARSET=utf-8;INTL;PARCEL;WORK:;;$f10;$f11;;$f12;$f13%0AEMAIL;INTERNET:$f6%0AORG:$f4%0ATEL;WORK:$f8%0ATEL;FAX;WORK:$f9%0ATITLE:$f5%0AURL;WORK:$f7%0AEND:VCARD"
latex=""
encodedVCard=$(echo "$vcard" | sed -e 's/\+/\%2B/g')
url="http://api.qrserver.com/v1/create-qr-code/?size=300x300&data=$encodedVCard"
wget -O "$path" "$url"
latex+="\n \begin{tabular}{ C C } \includegraphics[height=30mm]{graphic.png} & Name \\ \end{tabular}"
echo $latex
done < "$input"
Everything works except the 'echo $latex' always prints the same line instead of multiple times. What am I missing?
一切正常,除了 'echo $latex' 总是打印同一行而不是多次。我错过了什么?
回答by Brian Agnew
If you want to append to a string, just
如果你想附加到一个字符串,只需
latex="$latex newstring"
or
或者
latex=${latex}newstring
You need to be careful that bash doesn't interpret the above as a new var e.g. "$a4
" would be interpreted as a variable a4
and not as $a
with 4
appended.
你必须小心的bash不解释上述的新变种如"$a4
“将被解释为一个变量a4
而不是$a
与4
追加。
回答by chepner
Several problems:
几个问题:
Backslash expressions are not treated specially inside double quotes, so "\n" is two characters, "\" and "n", not a single newline. Use $'\n' to include an actual newline
echo $latex
expands the variable, but bash performs wordsplitting on any whitespace contained inlatex
, including newlines, so the newlines are never seen byecho
. You need to quote the variable:echo "$latex"
.As pointed out by others, you are resetting the variable in each iteration of the loop.
反斜杠表达式不会在双引号内进行特殊处理,因此“\n”是两个字符,“\”和“n”,而不是单个换行符。使用 $'\n' 来包含一个实际的换行符
echo $latex
扩展变量,但 bash 对包含在 中的任何空格latex
(包括换行符)执行分词,因此换行符永远不会被echo
. 您需要引用变量:echo "$latex"
。正如其他人所指出的那样,您正在循环的每次迭代中重置变量。
Putting it all together,
把这一切放在一起,
input="./user.cvs"
latex=""
while IFS=';' read -r f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13
do
path="./QRcodes/$f2$f3.png"
vcard="BEGIN:VCARD%0AN;CHARSET=utf-8:$f3;$f2;;$f1;%0AADR;CHARSET=utf-8;INTL;PARCEL;WORK:;;$f10;$f11;;$f12;$f13%0AEMAIL;INTERNET:$f6%0AORG:$f4%0ATEL;WORK:$f8%0ATEL;FAX;WORK:$f9%0ATITLE:$f5%0AURL;WORK:$f7%0AEND:VCARD"
#encodedVCard=$(echo "$vcard" | sed -e 's/\+/\%2B/g')
# You can use bash parameter expansion instead of piping into sed
encodedVCard="${vcard//+/%2B}"
url="http://api.qrserver.com/v1/create-qr-code/?size=300x300&data=$encodedVCard"
wget -O "$path" "$url"
latex+=$'\n \begin{tabular}{ C C } \includegraphics[height=30mm]{graphic.png} & Name \\ \end{tabular}'
echo "$latex"
done < "$input"
回答by Barmar
You have
你有
latex=""
in the loop, so it resets it each time. Put that before the beginning of the loop.
在循环中,所以它每次都会重置它。把它放在循环开始之前。