LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   getting the filename without the extinsion in bash? (http://www.linuxquestions.org/questions/linux-newbie-8/getting-the-filename-without-the-extinsion-in-bash-4175417439/)

maniakk 07-18-2012 03:22 PM

getting the filename without the extinsion in bash?
 
Code:

#!/bin/bash

clear

echo "You just heard, $(cat ~/lastauto)"
lastfm="You just heard, $(cat ~/lastauto)"

function play ()
{
for file in ~/Downloads/*.mp3; do
clear
#file2 = echo "$(basename $file)"
echo "You just heard: $(cat ~/lastauto). You are listening to O-W-E-N. Next up, we have: $file . Lets go!"
espeak "You just heard, $(cat ~/lastauto). You are listening to O-W-E-N, Next up, we have, $file . Lets go!" 2>/dev/null
echo $file>~/lastauto
clear
mpg123 ./"$file"
done
play
}

play

I want to get the filename without the extension. ($file2 is marked out to remind me to fix it.)
The script is a mp3 player that tells me what just played and whats next, so i dont have to look.
Right now, it tells me what the song is, but says "dot mp3" after the file.
And, I cant ever get the basename. Any suggestions?
(I just started learning Bash earlier this week. and i made the script. the entire thing, so i didnt steal. >.<)

whizje 07-18-2012 03:40 PM

Code:

bash-4.2$ name="music.mp3";echo ${name%.*}
music

See also http://tldp.org/LDP/abs/html/string-manipulation.html

schneidz 07-18-2012 03:41 PM

edit: use whiz's ^

i'm sure someone will have a better idea but heres mine:
Code:

[schneidz@hyper maniakk]$ ll
total 0
-rw-rw-r--. 1 schneidz schneidz 0 Jul 18 15:35 hello-1.c
-rw-rw-r--. 1 schneidz schneidz 0 Jul 18 15:35 hello-2.cpp
-rw-rw-r--. 1 schneidz schneidz 0 Jul 18 15:40 hello-3.java
-rw-rw-r--. 1 schneidz schneidz 0 Jul 18 15:40 hello-4.c.plus.plus
[schneidz@hyper maniakk]$ for item in *; do echo $item | rev | cut -d . -f 2- | rev; done
hello-1
hello-2
hello-3
hello-4.c.plus

related:
http://www.linuxquestions.org/questi...2/#post4703403

dukeherc 07-18-2012 03:57 PM

try file2 = echo "$(basename $file .mp3)"

custangro 07-18-2012 04:00 PM

Use the method provided...

Code:

${name%.*}
^ this is preferred over "basename"

maniakk 07-18-2012 04:13 PM

What is the % sign for?

whizje 07-18-2012 04:23 PM

Code:

${string%substring}
    Deletes shortest match of $substring from back of $string.
${string%%substring}
    Deletes longest match of $substring from back of $string.
${string#substring}
    Deletes shortest match of $substring from front of $string
${string##substring}
    Deletes longest match of $substring from front of $string.

See the link I gave you.http://tldp.org/LDP/abs/html/string-manipulation.html

chrism01 07-18-2012 05:11 PM

Quote:

^ this is preferred over "basename"
Why ? (serious qn)

custangro 07-18-2012 06:41 PM

Quote:

Originally Posted by chrism01 (Post 4732013)
Why ? (serious qn)

Because using a shell "built in" is faster than forking another process (i.e. 'basename')...

I know some will say "who cares in this case"...

But I feel that it's always good to practice good coding practices.

--C

whizje 07-18-2012 06:41 PM

Basename is an external command While the string functions are internal. Which make the string functions faster. In this case that's not so important. Also I think it's cleaner but that's arguable.

chrism01 07-18-2012 06:46 PM

OK; personally I think basename (& dirname) syntax is easier to remember than all the dozens of string fns though.
Depends on case use I guess.

AmbitEnerg 07-18-2012 06:53 PM

How to do it? :newbie:

custangro 07-18-2012 07:01 PM

Quote:

Originally Posted by chrism01 (Post 4732089)
OK; personally I think basename (& dirname) syntax is easier to remember than all the dozens of string fns though.
Depends on case use I guess.

True, easier to remember...and in this case it doesn't "matter"

But when trying to make your scripts faster; it's better to not fork external processes.

--C

David the H. 07-20-2012 11:46 AM

"Easier to remember" is relative to your experience. Use the substitutions long enough and they become pretty much second nature, just like any other skillset. They aren't really that difficult to remember anyway, at least not the ones you'll use most often. There are actually only about 8 main patterns, and logical variations thereof. And the references are always at hand when you need them.

Speaking of which, I prefer this one:
http://wiki.bash-hackers.org/syntax/pe

And this page goes into much more detail on all the various string manipulation options built into the shell:
http://mywiki.wooledge.org/BashFAQ/100


There is one slight advantage that basename has over the built-in, BTW. You can also pass it an optional suffix argument to remove the file ending at the same time.

Code:

$ file=/path/to/foobar.baz
$ basename "$file" ".baz"
foobar

Although even then you're still probably going experience faster performance doing it in the shell, even though it requires two separate substitutions.

Code:

$ file=/path/to/foobar.baz
$ file=${file##*/}
$ echo "${file%.baz}"
foobar



All times are GMT -5. The time now is 04:56 PM.