Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

building a string

Tags:

bash

ubuntu

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?

like image 732
Pascal Avatar asked Aug 16 '12 08:08

Pascal


People also ask

How do you make a string?

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”.

How do you create a string in C++?

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++.

Why StringBuilder is faster than string?

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.

How do you concatenate strings?

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.


2 Answers

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.

like image 67
Brian Agnew Avatar answered Oct 05 '22 19:10

Brian Agnew


Several problems:

  1. 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

  2. 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".

  3. 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"
like image 22
chepner Avatar answered Oct 05 '22 18:10

chepner