String structure

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 that 'echo $ latex' always prints the same line instead of several times. What am I missing?

+5
source share
3 answers

If you want to add to the line, just

latex="$latex newstring"

or

latex=${latex}newstring

You must be careful that bash does not interpret this as a new var, for example. "$a4"will be interpreted as a variable a4, not as $aan addition 4.

+10
source

A few problems:

  • , "\n"   , "\" "n", . $'\n'  

  • echo $latex , bash , latex, , echo. : echo "$latex".

  • , .

,

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"
+3

You have

latex=""

in the loop, so every time it resets it. Put this before the start of the cycle.

+1
source

All Articles