LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2012, 11:41 AM   #1
spadez
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Rep: Reputation: Disabled
Bash Newbie - Extract Filename from URL


Hi guys!

First post here. So, im trying to make my bash script super fancy but I'm stuck on one bit. This is the code I have been developing all morning. Please know that I have only just started learning BASH and I am not any kind of a programmer:

Code:
####################################
####################################

###########
#CORE SETUP FUCNTION
###########

function core()
{
	apt-get install XYZ
}

###########
#INSTALL FUCNTION
###########

function install()
{
	$FILE_VAR = "<INSERT NEW CODE>"
	cd /opt/
	wget $URL_VAR
	tar xzf $FILE_VAR -C $PROGRAM_VAR
	cd $PROGRAM_VAR
	./configure "$CONFIGURE_VAR"
	make
	make install
	rm $FILE_VAR
}

####################################
####################################

core

$PROGRAM_VAR = "Nginx"
$URL_VAR = "http://nginx.org/download/nginx-1.0.5.tar.gz"
$CONFIGURE_VAR = "--core"
install

$PROGRAM_VAR = "Python"
$URL_VAR = "http://www.python.org/ftp/python/2.7/Python-2.7.tgz"
$CONFIGURE_VAR = "--with-pth --with-dec-threads --with-signal-module --prefix=/opt/python-2.7"
install

####################################
####################################
So, I need help on two parts:

One, how do I get $FILE_VAR from my $URL_VAR automatically?
Two, is it possible to get $FILE_VAR without its extension easily? Perhaps after it has been extracted from its zip?

Thanks for any help you can give.

James
 
Old 07-15-2012, 10:03 AM   #2
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
Well, first of all, you aren't using proper shell syntax. Setting variables is done with "var=value". There's no "$" in front of the variable name, and no spaces around the equals sign. The dollar sign is only used when expanding a variable name back into it's value later.

Second, I can't actually understand your request. What exactly is this script supposed to do? What, exactly do the input values look like, and what exactly is the output that you need to get from it?

For general string manipulation techniques in bash, see here:
http://mywiki.wooledge.org/BashFAQ/100


And QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Finally, be aware that, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them.
 
Old 07-15-2012, 10:44 AM   #3
torchnw
LQ Newbie
 
Registered: Jun 2008
Distribution: Ubuntu, Debian, Mint, Fedora, Arch
Posts: 23

Rep: Reputation: 4
I guess you want to strip the path from the url to get the filename?

You can try:

Code:
FILE_VAR=`echo $URL_VAR | awk -F/ '{print $NF}'`
To remove the extension from the filename:

Code:
FILE_NOEXT_VAR=`echo $FILE_VAR | sed -E 's/\.t(ar\.)?[gb]z$//g'`
Do note the ` is not a quote, it's a backtick.
 
Old 07-15-2012, 11:12 AM   #4
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
If that's all that's needed, why go to all that trouble with awk and sed, when bash has perfectly good extraction ability built in?

To
Code:
FILE_VAR=${URL_VAR##*/}
Removing multiple extensions is a slightly more complex, but doable with extended globbing.

Code:
shopt -s extglob
FILE_NOEXT_VAR=${FILE_VAR%%?(.tar.tgz|.gz)}"

The link I gave before documents all kinds of similar features.
And even if you didn't want to use them, for some reason, there's always the basename command too.
 
1 members found this post helpful.
  


Reply


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
How can I extract the .rpm filename from long list of entries using bash shell script Glenn D. Linux - Software 2 07-14-2012 05:42 AM
extract last number from filename csegau Linux - Newbie 8 05-28-2010 07:24 AM
Grab Filename on the end of a (Download) URL hitman_et Programming 3 12-02-2005 04:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:50 AM.

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