Home > database >  How can I do calculations inside a for loop with echo bash
How can I do calculations inside a for loop with echo bash

Time:12-17

My code

for ((i=1 ; i < $words2   1 ; i  )) ; 
do
  printf '%s %s %s\n' '<text x="30" y="10">'$i'</text>' | sed 's/ *$//' 
done

and then I get as output

<text x="30" y="10">'$1'</text>
<text x="30" y="10">'$2'</text>
<text x="30" y="10">'$3'</text>

But I want to change something I want to add 20 to 30 in every line so It my output changes like this

<text x="30" y="10">'$1'</text>
<text x="50" y="10">'$2'</text>
<text x="70" y="10">'$3'</text>

I have tried to do calculations but it doesn't seem to work

CodePudding user response:

Like this:

for ((i=1, j=30 ; i < 10, j<=70 ; i  , j =20)); do
    printf '<text x="%d" y="10">"$%d"</text>\n' $j $i
done 

Output

<text x="30" y="10">"$1"</text>
<text x="50" y="10">"$2"</text>
<text x="70" y="10">"$3"</text>
  • Related