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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-18-2012, 02:22 PM
|
#1
|
|
LQ Newbie
Registered: Jul 2012
Location: hampton, georgia, USA
Distribution: Fedora 17
Posts: 16
Rep: 
|
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. >.<)
|
|
|
|
07-18-2012, 02:40 PM
|
#2
|
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 563
Rep: 
|
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.
|
07-18-2012, 02:41 PM
|
#3
|
|
Senior Member
Registered: May 2005
Location: boston, usa
Distribution: fc-12/ fc-11-live-usb/ aix
Posts: 2,733
|
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 02:46 PM.
|
|
|
|
07-18-2012, 02:57 PM
|
#4
|
|
LQ Newbie
Registered: Jul 2012
Posts: 1
Rep: 
|
try file2 = echo "$(basename $file .mp3)"
|
|
|
|
07-18-2012, 03:00 PM
|
#5
|
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,782
Rep: 
|
Use the method provided...
^ this is preferred over "basename"
|
|
|
|
07-18-2012, 03:13 PM
|
#6
|
|
LQ Newbie
Registered: Jul 2012
Location: hampton, georgia, USA
Distribution: Fedora 17
Posts: 16
Original Poster
Rep: 
|
What is the % sign for?
|
|
|
|
07-18-2012, 03:23 PM
|
#7
|
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 563
Rep: 
|
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.
|
07-18-2012, 04:11 PM
|
#8
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
Quote:
|
^ this is preferred over "basename"
|
Why ? (serious qn)
|
|
|
|
07-18-2012, 05:41 PM
|
#9
|
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,782
Rep: 
|
Quote:
Originally Posted by chrism01
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
|
|
|
|
07-18-2012, 05:41 PM
|
#10
|
|
Member
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 563
Rep: 
|
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.
|
|
|
|
07-18-2012, 05:46 PM
|
#11
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,261
|
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.
|
|
|
|
07-18-2012, 05:53 PM
|
#12
|
|
LQ Newbie
Registered: Jul 2012
Location: Bay City, Michigan
Posts: 1
Rep: 
|
How to do it? 
|
|
|
|
07-18-2012, 06:01 PM
|
#13
|
|
Senior Member
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , Solaris 10, RHEL
Posts: 1,782
Rep: 
|
Quote:
Originally Posted by chrism01
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
|
|
|
|
07-20-2012, 10:46 AM
|
#14
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,704
|
"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.
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:46 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|