4

I want to resample a bunch of wav files that I got on a folder.

My script is this:

for f in *.wav; do sox “$f” -r 48000 “${f%%%.wav}.wav”; done

The console give me this error: "sox FAIL formats: can't open input file `“90.wav”': No such file or directory" and so on with the 300 files that are placed on that folder.

How can I batch processing right this files? Why is it giving me this error?

Thanks a lot!

Solution:

for i in *wav; do echo $i; sox $i -r 48000 ${i%%.wav}r.wav; done
DaNoiseMan
  • 139
  • 2
  • 13
  • Try this: for f in ./*.wav; ... – jan Dec 03 '14 at 06:12
  • There seem to be 1 `%` too much, but if you write `"${f%%.wav}.wav"` or even `"${f%.wav}.wav"`, this become useless... You could'nt overwrite you file while you're reading it! – techno Dec 03 '14 at 07:06
  • What's with the triple percent signs? I've seen one and two, but not three. Brief experimentation reveals that the third is used literally, at east in Bash. – tripleee Dec 03 '14 at 07:07
  • @techno Yeah, it works by accident, by actually ending up with `09.wav.wav` as the output file, because the substitution didn't do anything. – tripleee Dec 03 '14 at 07:09

1 Answers1

5

Summary: It is the quote symbols

The problem is with the double-quotes:

for f in *.wav; do sox “$f” -r 48000 “${f%%%.wav}.wav”; done

The double-quotes above are non-standard. For them to be properly processed by the shell, the standard ASCII quote symbol must be used:

for f in ./*.wav; do sox "$f" -r 48000 "${f%%%.wav}.wav"; done

As an aside, note that ${f%%%.wav} removes any occurrences of %.wav from the end of the input file name. ${f%%%.wav}.wav adds one .wav back on to the end after removing any %.wav suffixes. You likely want something else here.

Verification

Using the bad quote characters, as per the question, observe the error message:

$ for f in *.wav; do sox “$f” -r 48000 “${f%%%.wav}.wav”; done
sox FAIL formats: can't open input file `“90.wav”': No such file or directory

Note the file name in the error message is shown with two-sets of quotes around the file name. This is what you saw as per the error message that in the question. The outer single-quotes are supplied by sox. The inner double-quotes are the funny quote characters provided on the command line. Because they are non-standard characters, the shell left them in place and passed them to the sox command.

While the file 90.wav exists, no file by the name of “90.wav” exists. Hence, the error.

Conclusion

Stick to standard ASCII characters for shell commands.

This issue can easily happen if the shell commands are typed in using a fancy word-processing editor that substitutes in typographically-pretty but non-standard characters. As tripleee points out, it can also happen when copying-and-pasting from the websites with inappropriate typographical styling.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • 1
    ... or copy/pasting from a blog which foolishly applies typographical styling to code. – tripleee Dec 03 '14 at 07:05
  • @tripleee Excellent thought: I just added it to the answer. – John1024 Dec 03 '14 at 07:10
  • I had a script that works fine for me with ImageMagick, not from a website. I had the script on my Drive on a text file. Thanks for your answer John. – DaNoiseMan Dec 03 '14 at 15:29
  • @DaNoiseMan Did you try it again with plain ASCII double-quotes? If you did and it didn't work, what error message did you receive? – John1024 Dec 03 '14 at 17:15
  • This did the trick: for i in *wav; do echo $i; sox $i -r 48000 ${i%%.wav}r.wav; done Thanks again John! – DaNoiseMan Dec 03 '14 at 18:56