BASH scripting problem, spaces in filenames / using basename
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH scripting problem, spaces in filenames / using basename
I'm having a hard time trying to do a very simple task. I want to run lame encoder in a directory and have it: 1. Convert all .wav files to mp3s; and 2. Delete the .wavs when it is done.
for file in *.wav;
do lame -b112 -V1 -h -mj --lowpass 19.5 --athtype 3 "$file" `basename $file .wav`.mp3
rm "$file"
done
Now here is my problem. Having the quotes around "$file" is something done to accomodate for spaces in filenames. (Which a lot of my .wav files have). The problem comes in the string:
`basename $file .wav`.mp3
I canot get this to work no matter what variations I try! The output of the nested "$file" in this string only returns the first string of characters in the filename before the space. Additional parts of the filename are returned to the shell (or in this case, lame) as separate strings, instead of sending the hard space \ character.
Or you could write a short script to replace all the spaces with underscores, run your bash script, then write another script to return all the underscores to spaces. If there are already underscores in the file name, then replace the spaces with something like "_-_", or "___".
How strange, my execution of your exact same script returns (and all I did was copy and paste your text into an editor, save & chmod +x it.
To verify:
peter@(~)$ less testscript
#!/bin/bash
for file in *.mp3;
do echo `basename "$file".hello'.all;
done
testscript (END):
Anyway, here's what my execution yields:
peter@(~)$ ./testscript
./testscript: line 3: unexpected EOF while looking for matching ``'
./testscript: line 5: syntax error: unexpected end of file
peter@(~)$
#!/bin/bash
for file in *.wav;
do lame -b112 -V1 -h -mj --lowpass 19.5 --athtype 3 "$file" "`basename $file .wav`.mp3"
done
Gets me a step closer!
but... still not there:
peter@(~/AUDIO/Deep Dish _ Yoshitoshi Artists/In house we trust vol. 1)$ /home/peter/testscript
basename: too many arguments
Try `basename --help' for more information.
LAME version 3.93 MMX (http://www.mp3dev.org/)
CPU features: i387, MMX (ASM used)
Using polyphase lowpass filter, transition band: 19383 Hz - 19916 Hz
Encoding 05-alcatraz _giv me luv_.wav to .mp3
Sorry I'm sort of a novice here.... (as if I ::needed:: to come out and say that!)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.