LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-18-2012, 03:22 PM   #1
maniakk
LQ Newbie
 
Registered: Jul 2012
Location: hampton, georgia, USA
Distribution: Fedora 17
Posts: 16

Rep: Reputation: Disabled
Question 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. >.<)
 
Old 07-18-2012, 03:40 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
bash-4.2$ name="music.mp3";echo ${name%.*}
music
See also http://tldp.org/LDP/abs/html/string-manipulation.html
 
2 members found this post helpful.
Old 07-18-2012, 03:41 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,309

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

Last edited by schneidz; 07-18-2012 at 03:46 PM.
 
Old 07-18-2012, 03:57 PM   #4
dukeherc
LQ Newbie
 
Registered: Jul 2012
Posts: 1

Rep: Reputation: Disabled
try file2 = echo "$(basename $file .mp3)"
 
Old 07-18-2012, 04:00 PM   #5
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Use the method provided...

Code:
${name%.*}
^ this is preferred over "basename"
 
Old 07-18-2012, 04:13 PM   #6
maniakk
LQ Newbie
 
Registered: Jul 2012
Location: hampton, georgia, USA
Distribution: Fedora 17
Posts: 16

Original Poster
Rep: Reputation: Disabled
What is the % sign for?
 
Old 07-18-2012, 04:23 PM   #7
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
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
 
2 members found this post helpful.
Old 07-18-2012, 05:11 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,329

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
Quote:
^ this is preferred over "basename"
Why ? (serious qn)
 
Old 07-18-2012, 06:41 PM   #9
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by chrism01 View Post
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
 
Old 07-18-2012, 06:41 PM   #10
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
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.
 
Old 07-18-2012, 06:46 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,329

Rep: Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745Reputation: 2745
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.
 
Old 07-18-2012, 06:53 PM   #12
AmbitEnerg
LQ Newbie
 
Registered: Jul 2012
Location: Bay City, Michigan
Posts: 1

Rep: Reputation: Disabled
How to do it?
 
Old 07-18-2012, 07:01 PM   #13
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by chrism01 View Post
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
 
Old 07-20-2012, 11:46 AM   #14
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
"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
 
1 members found this post helpful.
  


Reply

Tags
basename, bash scripting


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 2 several words filename trintukaz Programming 8 11-27-2011 10:12 PM
[SOLVED] bash script: to truncate filename casualzone Linux - Software 3 05-24-2011 11:01 PM
bash: best way to get filename and extension using bash frenchn00b Programming 5 01-09-2010 02:47 PM
filename escaping in bash dash9 Linux - Software 1 06-04-2009 11:36 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 02:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:25 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
Open Source Consulting | Domain Registration