rotate a "for" loop with 5 variables together

Asked

Viewed 102 times

0

Use a program to create audio files called stem which combines vocals, drums, melody, bass and an audio guide into a single file.

I am trying to create a loop that stores these files and so the application can use it.

Inside the folder I have the files:

a.vocal.m4a a.bateria.m4a a.melodia.m4a a.baixo.m4a a.master.m4a 
b.vocal.m4a b.bateria.m4a b.melodia.m4a b.baixo.m4a b.master.m4a 
c.vocal.m4a c.bateria.m4a c.melodia.m4a c.baixo.m4a c.master.m4a

I tried to spin:

for f in *.vocal.m4a; for g in *.bateria.m4a; for h in *.melodia.m4a; for i in *.baixo.m4a; for j in *.master.m4a; 
do mesclar "$f" "$g" "$h" "$i" "$j" ; 
done

But the tie only works on the last for. I would like a loop that runs all the files "a" after the files "b" and then the "c" and so on.

  • I suggest removing the python tag as it is Shel script.

2 answers

2

In shell script, just make a loop by the letters of a up to the letter you want (let’s assume it’s c):

for i in {a..c}
do
    mesclar $i.*
done

This, assuming that in the directory you only have the files you want (if you have any others whose name is a.algumacoisa, it will also be passed to the command mesclar). Also, when using $i.*, the files will be passed in alphabetical order. If you want to restrict the files and control the order, you can switch to:

for i in {a..c}
do
    mesclar $(echo $i.{vocal,bateria,melodia,master}.m4a)
done

In the case, the echo prints the letter, the dot, and then vocal, bateria, etc, in this order. That is, the files are passed in the desired order. And I use the syntax of command substitution so that the exit of the echo is passed to command mesclar.

If instead of a, b, c, etc, the names can be anything (not necessarily a sequence), you can put them separated by comma.

Also, once you have these names, you can compose the command however you want. Using the example of your comment, would be:

for i in {nome,outronome,maisoutro}
do
    mesclar $(echo $i.{melodia,vocal,baixo,bateria}.m4a) -x $i.master.m4a
done

That will call the commands:

mesclar nome.melodia.m4a nome.vocal.m4a nome.baixo.m4a nome.bateria.m4a -x nome.master.m4a
mesclar outronome.melodia.m4a outronome.vocal.m4a outronome.baixo.m4a outronome.bateria.m4a -x outronome.master.m4a
mesclar maisoutro.melodia.m4a maisoutro.vocal.m4a maisoutro.baixo.m4a maisoutro.bateria.m4a -x maisoutro.master.m4a

In Python the idea is similar, just use one of the many ways of calling external processes to call the command. Example with os.system:

import os

files = ['vocal', 'bateria', 'melodia', 'master']
for i in 'abc':
    params = ' '.join(f'{i}.{file}.m4a' for file in files)
    os.system(f'mesclar {params}')

Or, with a list of names and the command indicated in the comments:

import os

files = ['melodia', 'vocal', 'baixo', 'bateria']
for i in ['nome', 'outronome', 'maisoutro']:
    params = ' '.join(f'{i}.{file}.m4a' for file in files)
    os.system(f'mesclar {params} -x {i}.master.m4a')

That will call the commands:

mesclar nome.melodia.m4a nome.vocal.m4a nome.baixo.m4a nome.bateria.m4a -x nome.master.m4a
mesclar outronome.melodia.m4a outronome.vocal.m4a outronome.baixo.m4a outronome.bateria.m4a -x outronome.master.m4a
mesclar maisoutro.melodia.m4a maisoutro.vocal.m4a maisoutro.baixo.m4a maisoutro.bateria.m4a -x maisoutro.master.m4a
  • thanks for the answer. I may not have expressed myself right. the command I use is like this: python . /ni-Stem.py create -m . /Stemmetadata.json -s ". /file.melodia.m4a" ". /file.vocal.m4a" ". /file.baixo.m4a" ". /file.bateria.m4a" -x ". /file.master.m4a" I’m trying a way and optimize the process by running a command for all the files in the folder. in case to make a lac to read the address of the files type "a". then to type "b". a and b are examples, I will have other names. but all of them will have melody, vocal, bass, drums and master

  • @underson14 I updated the answer with a few more examples, see if that’s what you need

0

Use the zip() function. With it you can browse several lists.

>>> letters = ['a', 'b', 'c', 'd', 'e']
>>> numbers = [0, 1, 2, 3, 4]
>>> for l, n in zip(letters, numbers):
...     print(f'Letter: {l}')
...     print(f'Number: {n}')

Letter: a
Number: 0
Letter: b
Number: 1
Letter: c
Number: 2
Letter: d
Number: 3
Letter: e
Number: 4
  • the theoretical concept is good - but just be aware of the question, that is not what is being asked - and yes, run the command "merge <files>" first with the files starting with "a" - and so on.

  • @brunoi-fields I tried to do something like this : cd . /audio created a file . /teste02.Gyp with: for a, b, c, d, and in zip(*. 1.melodia.m4a, *.2.vocais.m4a, *.3.baixo.m4a, .4.battery.m4a,.5.master.m4a): python './ni-Stem.py' create -m './Stemmetadata.json' -s f'{a}' f'{b}' f'{c}' f'{d}' -x f'{e}' made python . /teste02.Gyp and appeared: File ". /teste02.Gyp", line 2 python './ni-Stem.py' create -m './Stemmetadata.json' -s f'{a}' f'{b}' f'{c}' f'{d}' -x f'{e}' Syntaxerror: invalid syntax what happened?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.