O
O
oleww052018-12-28 05:02:34
bash
oleww05, 2018-12-28 05:02:34

How to loop with two variables?

I use a loop within a loop

#!/bin/bash
for a in {1..3}; do
for b in {4..6}; do
echo "text untitled/$a.rar /untitled/$b.rar"
done; done

Получается так:
text untitled/1.rar /untitled/4.rar
text untitled/1.rar /untitled/5.rar
text untitled/1.rar /untitled/6.rar
text untitled/2.rar /untitled/4.rar
text untitled/2.rar /untitled/5.rar
text untitled/2.rar /untitled/6.rar
text untitled/3.rar /untitled/4.rar
text untitled/3.rar /untitled/5.rar
text untitled/3.rar /untitled/6.rar

The question is how to get such a result?
text /untitled/1.rar /untitled/4.rar
text /untitled/2.rar /untitled/5.rar
text /untitled/3.rar /untitled/6.rar

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2018-12-28
@oleww05

#!/bin/bash
for (( a=1, b=4; a <= 3; a++, b++ ))
do
echo "text untitled/$a.rar /untitled/$b.rar"
done

B
BornID, 2021-09-06
@BornID

You can achieve the result with the usual for loop:
for a in "1 3" "2 4" "5 6" ; do set -- $a ; echo "text untitled/$1.rar /untitled/$2.rar" ; done
Essence:
We break the $a variable into arguments.
Next, we print not the variable $a itself, but the arguments $1 and $2.
Result:
text untitled/1.rar /untitled/3.rar
text untitled/2.rar /untitled/4.rar
text untitled/5.rar /untitled/6.rar
Source: https://www.opennet.ru/ docs/RUS/bash_scripting_gui...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question