LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Make a variable = filename without the extension (https://www.linuxquestions.org/questions/linux-newbie-8/make-a-variable-%3D-filename-without-the-extension-876719/)

greengrocer 04-23-2011 06:31 PM

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

jthill 04-23-2011 06:41 PM

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

MTK358 04-23-2011 07:28 PM

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

jthill 04-23-2011 08:16 PM

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.

GVrooman 04-23-2011 08:54 PM

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

MTK358 04-24-2011 08:23 AM

Quote:

Originally Posted by GVrooman (Post 4334042)
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?

GVrooman 04-24-2011 09:18 AM

Quote:

Originally Posted by MTK358 (Post 4334390)
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.

MTK358 04-24-2011 09:39 AM

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

GVrooman 04-24-2011 10:16 AM

Quote:

Originally Posted by MTK358 (Post 4334466)
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. :)


All times are GMT -5. The time now is 07:24 PM.