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?
There are two ways to create a String object: By string literal : Java String literal is created by using double quotes. For Example: String s=“Welcome”; By new keyword : Java String is created by using a keyword “new”.
if you really need a character pointer (and you haven't said why you think you do), you can get one from a string by using its c_str() member function. All this should be covered by any introductory C++ text book. If you haven't already bought one, get Accelerated C++.
String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer.
You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time. The C# examples in this article run in the Try.NET inline code runner and playground.
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.
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 in latex
, including newlines, so the newlines are never seen by echo
. You need to quote the variable: echo "$latex"
.
As pointed out by others, you are resetting the variable in each iteration of the loop.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With