for loop - spaces in filenames

Need help with C, C++, perl, python, etc?

for loop - spaces in filenames

Postby sunrat » 2019-12-07 06:15

I have this command which does exactly what I want but doesn't work if filenames have spaces:

Code: Select all
for i in *.flac; do echo $i; sox -G $i -b 16 -r 44100 ${i%%.flac}r.flac; done


Can it be modified to accept spaces? Also I'm not sure that "echo" is necessary, any thoughts on that?
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2928
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: for loop - spaces in filenames

Postby Dai_trying » 2019-12-07 08:36

Try this

Code: Select all
for i in *.flac; do echo "$i"; sox -G "$i" -b 16 -r 44100 "${i%%.flac}"r.flac; done


simply putting quotes around the variables usually prevents the error you are seeing.

And no the echo is not necessary for the function of the script and could be removed.
Dai_trying
 
Posts: 858
Joined: 2016-01-07 12:25

Re: for loop - spaces in filenames

Postby peter_irich » 2019-12-07 09:16

I prefer use more detailed scheme. This script works whit spaces in file names:

Code: Select all
#!/bin/bash

cnt=1          #extra
rm -f list_flac
touch list_flac
ls -1 *.flac >> list_flac
while read flnm
  do
echo $cnt "flnm" "$flnm"        #extra
name="`echo "$flnm" | cut -d "." -f 1`"
echo $cnt "name" "$name"      #extra
#ext=`echo "$flnm" | cut -d "." -f 2`     #extra
flnm1="${name}"r.flac

#sox -G "$flnm" -b 16 -r 44100 "$flnm1"

echo $cnt "flnm1" "$flnm1"       #extra
echo $cnt "new" "${name}"r.flac        #extra
let cnt++         #extra
done < list_flac

#rm -f list_flac

You can remove extra and insert your command in that place what you want.

Peter.
Last edited by peter_irich on 2019-12-07 09:26, edited 1 time in total.
peter_irich
 
Posts: 1262
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation

Re: for loop - spaces in filenames

Postby sunrat » 2019-12-07 09:22

Great, thanks Dai_trying. I thought I had tried it with quotes but I must have missed something. Doesn't need echo either. I adapted it from an answer on StackExchange. There are some weird things on there sometimes!
Strangely it gives a warning about 1 clipped sample which the "-G" is supposed to prevent. Not an issue though, it's easy to add an option to decrease volume if needed.
Code: Select all
$ for i in *.flac;do sox -G "$i" -b 16 -r 44100 "${i%%.flac}"r.flac; done
sox WARN dither: dither clipped 1 samples; decrease volume?


And thanks peter_irich. I'll try to work out what yours is doing sometime. I prefer KISS though. ;)
“ computer users can be divided into 2 categories:
Those who have lost data
...and those who have not lost data YET ”
Remember to BACKUP!
User avatar
sunrat
 
Posts: 2928
Joined: 2006-08-29 09:12
Location: Melbourne, Australia

Re: for loop - spaces in filenames

Postby Dai_trying » 2019-12-07 10:45

You're welcome, I have had many a script do unexpected things due to missing quotes, and tbh it's one of the reasons I tend to prefer Python for my scripts now.
Dai_trying
 
Posts: 858
Joined: 2016-01-07 12:25


Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable