LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-13-2003, 10:02 AM   #1
textures
LQ Newbie
 
Registered: Nov 2003
Location: Lexington, SC, USA
Distribution: Gentoo
Posts: 12

Rep: Reputation: 0
Question 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
 
Old 11-13-2003, 10:12 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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
 
Old 11-13-2003, 10:37 AM   #3
textures
LQ Newbie
 
Registered: Nov 2003
Location: Lexington, SC, USA
Distribution: Gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
Unhappy

Yes, that is indeed different than BASH.

I'm ripping my hair out here!
 
Old 11-13-2003, 10:44 AM   #4
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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

Last edited by jkobrien; 11-13-2003 at 10:52 AM.
 
Old 11-13-2003, 10:59 AM   #5
textures
LQ Newbie
 
Registered: Nov 2003
Location: Lexington, SC, USA
Distribution: Gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
not installed...

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

(Yes, I'm a gentoo user.)

./peter
 
Old 11-13-2003, 11:03 AM   #6
quatsch
LQ Addict
 
Registered: Aug 2003
Location: New York, NY
Distribution: gentoo, gentooPPC
Posts: 1,661

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

Doesn't this do it?
 
Old 11-13-2003, 11:06 AM   #7
textures
LQ Newbie
 
Registered: Nov 2003
Location: Lexington, SC, USA
Distribution: Gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 11-13-2003, 11:41 AM   #8
quatsch
LQ Addict
 
Registered: Aug 2003
Location: New York, NY
Distribution: gentoo, gentooPPC
Posts: 1,661

Rep: Reputation: 47
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.
 
Old 11-13-2003, 11:48 AM   #9
quatsch
LQ Addict
 
Registered: Aug 2003
Location: New York, NY
Distribution: gentoo, gentooPPC
Posts: 1,661

Rep: Reputation: 47
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.
 
Old 11-13-2003, 11:51 AM   #10
textures
LQ Newbie
 
Registered: Nov 2003
Location: Lexington, SC, USA
Distribution: Gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
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@(~)$
 
Old 11-13-2003, 11:54 AM   #11
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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
 
Old 11-13-2003, 11:58 AM   #12
textures
LQ Newbie
 
Registered: Nov 2003
Location: Lexington, SC, USA
Distribution: Gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
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!)

 
Old 11-13-2003, 11:59 AM   #13
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Sorry, or rather the ' in hello'.all should be a `

John
 
Old 11-13-2003, 12:03 PM   #14
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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
 
Old 11-13-2003, 12:12 PM   #15
textures
LQ Newbie
 
Registered: Nov 2003
Location: Lexington, SC, USA
Distribution: Gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash scripting: problem with generating filenames mad_juno Programming 2 07-22-2005 10:57 AM
bash scripting: filenames with spaces joanq Programming 2 05-24-2005 12:50 PM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 08:55 PM
Scripting question reg. filenames with spaces Awfki Linux - Newbie 3 05-16-2004 08:01 PM


All times are GMT -5. The time now is 11:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration