Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

for loop - spaces in filenames

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
User avatar
sunrat
Administrator
Administrator
Posts: 6470
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 117 times
Been thanked: 474 times

for loop - spaces in filenames

#1 Post by sunrat »

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!

Dai_trying
Posts: 1101
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: for loop - spaces in filenames

#2 Post by Dai_trying »

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.

peter_irich
Posts: 1405
Joined: 2009-09-10 20:15
Location: Saint-Petersburg, Russian Federation
Been thanked: 11 times

Re: for loop - spaces in filenames

#3 Post by peter_irich »

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.

User avatar
sunrat
Administrator
Administrator
Posts: 6470
Joined: 2006-08-29 09:12
Location: Melbourne, Australia
Has thanked: 117 times
Been thanked: 474 times

Re: for loop - spaces in filenames

#4 Post by sunrat »

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!

Dai_trying
Posts: 1101
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: for loop - spaces in filenames

#5 Post by Dai_trying »

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.

Post Reply