LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 04-23-2011, 07:31 PM   #1
greengrocer
Member
 
Registered: Aug 2005
Distribution: Ubuntu Intrepid and Meerkat, formerly used Debian 3.1 (Sarge) with Gnome Desktop
Posts: 353

Rep: Reputation: 37
Make a variable = filename without the extension


Hi all,

I am trying to strip the .wav file extension from a file name so that I can pass the result to lame encoder.

I started to write a BASH script that looks like this:
Code:
for f in /home/user/wavfiles*; do FILE=basename $f .wav; lame $FILE; done
It doesn't work very well though. For the life of me I can't seem to discover how to use basename in a suitable way for a script like this.

I'm hoping someone here at LQ can help.

Many thanks in advance

Greenie
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-23-2011, 07:41 PM   #2
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 66
You don't need basename in the shell, just use stripping. FILE="${f%.*}".wav % strips shortest matching suffix, %% longest, # shortest matching prefix, ## longest. So ${f##*.} gets you the suffix, ${f%/*} gets the dirname, etc. And since you're not preserving the value of FILE for anything, just do it: do lame "${f%.*}".wav; done.

What you were looking for is useful other times, though, and it's called "command substitution" in the manpage. Do it with $() (<-parens instead of curlies) or backticks. $(basename "$f").
 
2 members found this post helpful.
Old 04-23-2011, 08:28 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
"basename" has nothing to do with extensions. It simply returns the last item in a slash-separated path (i.e. get the actual name of the file from a path). Similarly, "dirname" returns the path, removing the filename from the end.

And just in case you didn't know, Linux or its filesystems has no concept of extensions. Some programs do pay attention to them, though (most notably GUI file maangers, to tell the type of file, becasue it's faster to read the file's name than open in and look throught it to figure out its contents).
 
Old 04-23-2011, 09:16 PM   #4
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 66
Shows how long it's been since I used basename I guess. :-P greengrocer's just lucky it was a side point. Sorry 'bout that.
 
Old 04-23-2011, 09:54 PM   #5
GVrooman
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 38

Rep: Reputation: 11
Lame has trouble handling file names with blanks in them. Here is a discussion of the subject, and a slick method that works:

http://ubuntuforums.org/archive/inde...t-1089562.html
 
Old 04-24-2011, 09:23 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by GVrooman View Post
Lame has trouble handling file names with blanks in them. Here is a discussion of the subject, and a slick method that works:

http://ubuntuforums.org/archive/inde...t-1089562.html
I don't really understand that thread. Is the trouble about spacing between arguments or in filenames? And isn't it bash that has trouble with spaces, not the program launched?
 
Old 04-24-2011, 10:18 AM   #7
GVrooman
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 38

Rep: Reputation: 11
Quote:
Originally Posted by MTK358 View Post
I don't really understand that thread. Is the trouble about spacing between arguments or in filenames? And isn't it bash that has trouble with spaces, not the program launched?
The problem seems to be blank spaces in file names. I tried using basename to convert the extensions from wav to mp3.

Code:
for i in *.wav ; do
  echo $i
  b=`basename $i .wav`
  lame -V $i $b.mp3 
done

I ended up with a screen full of "excess arg" errors. Lame would consider any name up to the first blank space to be one argument. It could be a shell problem. Probably the escape characters get stripped out of the file names. Anyway I tracked down that thread, and their little copy and paste command worked. Since the OP's real problem was wanting to convert wav files to mp3 files, I thought I would pass it on.
 
Old 04-24-2011, 10:39 AM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
DIdn't I already say that basename has nothing to do with extensions?!? Just scroll up to the 3rd post.

And it's the shell that splits filenames with spaces into separate arguments. To stop it from splitting the contents if a variable into multiple args, surround it in double-quotes (i.e. "$i").
 
Old 04-24-2011, 11:16 AM   #9
GVrooman
Member
 
Registered: May 2008
Distribution: Slackware
Posts: 38

Rep: Reputation: 11
Quote:
Originally Posted by MTK358 View Post
DIdn't I already say that basename has nothing to do with extensions?!? Just scroll up to the 3rd post.

And it's the shell that splits filenames with spaces into separate arguments. To stop it from splitting the contents if a variable into multiple args, surround it in double-quotes (i.e. "$i").
You da man. I think I will go listen to some mp3s.
 
  


Reply

Tags
bash, lame, quoting


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
Convert filename extension to lowercase jakev383 Programming 6 12-07-2008 07:24 AM
How to get filename extension makowka Programming 7 04-30-2007 06:32 PM
rm: cannot remove 'filename.extension' Operation not permitted shinystuffrox Linux - Software 8 03-07-2007 06:09 AM
redirect to same filename without extension geekpie Linux - Software 1 10-31-2006 11:12 AM
Filename extension question BajaNick Linux - General 2 07-05-2004 06:07 PM


All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
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