LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH scripting problem, spaces in filenames / using basename (https://www.linuxquestions.org/questions/programming-9/bash-scripting-problem-spaces-in-filenames-using-basename-115581/)

textures 11-13-2003 10:02 AM

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.


Can anyone please help?


./Peter

jkobrien 11-13-2003 10:12 AM

I don't know if it works in bash (probably not), but in tcsh :r returns the basename.

e.g.

set file = "some file.wav"
echo $file:r # gives you "some file".

John

textures 11-13-2003 10:37 AM

Yes, that is indeed different than BASH.

I'm ripping my hair out here!

jkobrien 11-13-2003 10:44 AM

So, is there more to your script than you showed in your first post?

Can you just re-write it as a tcsh script? I'm not sure about all the lame arguments (no pun intended!!) but it would be something like this...

#!/bin/tcsh
foreach wav ( *.wav )
$bname = $wav:r
lame -b112 -V1 -h -mj --lowpass 19.5 --athtype 3 $wav $bname.mp3
rm $wav
end

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 "___".

John

textures 11-13-2003 10:59 AM

not installed...
 
I don't even have tcsh installed... I guess I'll emerge it now.

(Yes, I'm a gentoo user.)

./peter

quatsch 11-13-2003 11:03 AM

how about putting " around the $file behind basename. I mean something like:
`basename "$file" .wav`

Doesn't this do it?

textures 11-13-2003 11:06 AM

No, it doesn't.

Here's why -- the output of "basename" is not hardspaced (i.e. it doesn't print \ after spaces).

If you want to see what I mean, try running basename on a filename with spaces... and take a gander at the output.

quatsch 11-13-2003 11:41 AM

I just tried and it seems to work for me. Here's the script I used:

#!/bin/bash
for file in *.mp3;
do echo `basename "$file".hello'.all;
done

Did exactly what I expected it to do.

quatsch 11-13-2003 11:48 AM

Sorry. I think I was misunderstanding the problem. But you are able to place double quotes around the whole thing enclosed within `. Like this:

do lame -b112 -V1 -h -mj --lowpass 19.5 --athtype 3 "$file" "`basename $file .wav`.mp3"

I forgot the details of the arguments lame takes but I thought you can pass file names within double qoutes. If it does, the above should do it.

textures 11-13-2003 11:51 AM

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@(~)$

jkobrien 11-13-2003 11:54 AM

Yes, because you're missing a ` at the end of line 3

But, well done quatsch! I just spent half an hour tearing my hair out too!

John

textures 11-13-2003 11:58 AM

Using the revised :

#!/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!)

:)

jkobrien 11-13-2003 11:59 AM

Sorry, or rather the ' in hello'.all should be a `

John

jkobrien 11-13-2003 12:03 PM

Ok, then try a combination of echo and sed in place of basename.

"`echo $file | sed 's/ /\\ /g' | sed 's/.wav$/.mp3/'`"

I don't have lame installed, so I can't test it properly, but it works with a mv command.

John

textures 11-13-2003 12:12 PM

Okay! This gets me much much closer! Something is wrong with the sed command, though (I am now running this string:

do lame -b112 -V1 -h -mj --lowpass 19.5 --athtype 3 "$file" "`echo $file | sed 's/ /\\ /g' | sed 's/.wav$/.mp3/'`"

):

peter@(~/AUDIO/Deep Dish _ Yoshitoshi Artists/In house we trust vol. 1)$ /home/peter/testscriptsed: -e expression #1, char 6: Unterminated `s' command
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 05-alcatraz _giv me luv_.wav.mp3


All times are GMT -5. The time now is 10:23 PM.