0

I'm very new to linux, I'm trying to get names of .ogg files from a text file and take mp3 files of directory music and rename and move them to a different directory with the name of that file line by line but not working. shellcheck.net shows this output:

f referrenced but not assigned

I can't figure it out.

#!/bin/bash
while -r read f;
do
for mp3 in ./music/*; 
   do
     "mv $mp3 /home/shady/Music/$f"
                  ^––SC2154 f is referenced but not assigned.
   break
   done
done < /home/shady/piku.txt

Update:

*

motegi_2004.ogg
assen_2004.ogg
phakisa_2004.ogg
donington_2004.ogg
catalunya_2004.ogg
strange_symbol.ogg
credits.ogg
philip_2004.ogg
jerez_2004.ogg
brno_2004.ogg
sepang_2004.ogg
estoril_2004.ogg
motogp.ogg
frontend.ogg
valencia_2004.ogg
lemans_2004.ogg
rio_2004.ogg
mugello_2004.ogg
sachsenring_2004.ogg
thqintro.ogg
qatar_2004.ogg
training_2004.ogg
climax.ogg

*

piku.txt contains these names and the for loop breaks after moving the first file as ogg in a different directory so when the wile loop takes the next name from line 2 from piku.txt the for loop takes the first file it finds in the directory in our case thats the file 2 cause we already moved previous one to a different directory .And dont worry about mp3s bening renamed as oggs ,this is just a test.I just wanna know if this works

edit:2

 1  #!/bin/bash
   2  while read -r f
   3  do
   4    for mp3 in /home/shady/Desktop/music/* 
   5    do
   6                mv -i "$mp3" "/home/shady/Music/$f"
   7                break
   8    done
   9    done < /home/shady/piku.txt
  10  

i assigned full path now for the for loop but it gives me

mv: cannot stat ‘/home/shady/Desktop/music/*’: No such file or directory


*

motegi_2004.ogg
assen_2004.ogg
phakisa_2004.ogg
donington_2004.ogg
catalunya_2004.ogg
strange_symbol.ogg
credits.ogg
philip_2004.ogg
jerez_2004.ogg
brno_2004.ogg
sepang_2004.ogg
estoril_2004.ogg
motogp.ogg
frontend.ogg
valencia_2004.ogg
lemans_2004.ogg
rio_2004.ogg
mugello_2004.ogg
sachsenring_2004.ogg
thqintro.ogg
qatar_2004.ogg
training_2004.ogg
climax.ogg

*

piku.txt contains these names and the for loop breaks after moving the first file as ogg in a different directory so when the wile loop takes the next name from line 2 from piku.txt the for loop takes the first file it finds in the directory in our case thats the file 2 cause we already moved previous one to a different directory .And dont worry about mp3s bening renamed as oggs ,this is just a test.I just wanna know if this works

edit:2

 1  #!/bin/bash
   2  while read -r f
   3  do
   4    for mp3 in /home/shady/Desktop/music/* 
   5    do
   6                mv -i "$mp3" "/home/shady/Music/$f"
   7                break
   8    done
   9    done < /home/shady/piku.txt
  10  

i assigned full path now for the for loop but it gives me

mv: cannot stat ‘/home/shady/Desktop/music/*’: No such file or directory

  • What is the purpose of -r option to the while loop? while is a shell keyword not a program that accepts parameters as far as I know – the_velour_fog Jan 7 '16 at 7:20
  • can you update your question to include the contents of the text file you are trying to read and what output you are hoping your program will produce – the_velour_fog Jan 7 '16 at 7:27
4

The -r isn't helping here, try placing it after the read.

Optional: After while and for you either need ; or a newline, but you don't need both! Since you have newlines before the do keywords, you can remove those two ;s.

Quotes should go around arguments, not the whole command:

    mv -i "$mp3" "/home/shady/Music/$f"

I added -i so that mv will not try to overwrite any existing files.

Crucially, you have a loop inside a loop. You can't move one mp3 file multiple times! If you want to mix two sets of filenames together, you could try using paste to pull lines from each file and combine them:

paste <(find ./music/* -maxdepth 1 -printf '"%p"\n') /home/shady/piku.txt | sed 's+^+mv -i +' > /tmp/move_my_files.sh

Then you can look at the file you generated and see if it does what you want.

(You may need to put quotes around all the filenames in piku.txt, if they contain spaces.)

If you are happy with how the move_my_files.sh script looks, you can run it:

bash -e -x /tmp/move_my_files.sh

If I have misunderstood your intention, you may need to explain better, including the content of piku.txt and the expected result, and how mp3 files become ogg files!

  • I agree with all of this – the_velour_fog Jan 7 '16 at 7:29
  • 1
    actually - I dont think they have a loop inside a loop. the inner loop breaks, so only one iteration runs – the_velour_fog Jan 7 '16 at 7:40
  • You are correct. So only one mp3 will be moved (the first one selected by *), and then the next line will be read from piku.txt. You can put echo before mv while you are working on your script, so you can see what the script is trying to do. – joeytwiddle Jan 7 '16 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.