1

I'm having trouble with this piece of code:

22 for filename in "$( ls "$1" | grep ".*\.flac$" )"; do
23                 file_path="$1${filename}"
24                 ffmpeg -i "${file_path}" -f ffmetadata $HOME/metadata

Instead of a metadata file on each iteration, I'm getting this error message:

Downloads/Ariel Pink's Haunted Graffiti - Worn Copy (2005)/01 Trepanated Earth.flac ... 17 Jagged Carnival Tours.flac: File name too long

So it appears that inside the loop the $filename variable is equal the names of all FLAC files lumped together.

Of course, omitting quote marks on line 22 results in whitespace problems.

How do I make this work? I'm new to bash and very confused.

zvukva
  • 13

1 Answers1

1

how about

 for filepath in "$1/"*.flac 
 do
      ffmpeg -i "${file_path}" -f ...

where

  • "$1/"*.flac will garantee .flac suffix in the end
  • be sure to quote "${file_path}"
  • basename can be found using bn=$(basename "${file_path}")
  • un flac'ed basename can be found using bnnf=$(basename "${file_path}" .flac)

sample

A > ls -l Music 
total 0
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 foo bar
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 foo bar.flac
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 fubar
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 fubar.flac

now run:

 A > for f in Music/*.flac; do echo $f; ls -l "$f" ; done 
Music/foo bar.flac
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 Music/foo bar.flac
Music/fubar.flac
-rw-rw-r-- 1 Arc Arc 0 Mar 14 03:37 Music/fubar.flac
Archemar
  • 31,554
  • I haven't used it in my script yet, but I did some debugging and it seems to work. The solution looks so simple I'm confused even further. :)

    Thank you!

    – zvukva Mar 14 '15 at 11:02