LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Newbie - Extract Filename from URL (https://www.linuxquestions.org/questions/programming-9/bash-newbie-extract-filename-from-url-4175416710/)

spadez 07-14-2012 11:41 AM

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

David the H. 07-15-2012 10:03 AM

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.

torchnw 07-15-2012 10:44 AM

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.

David the H. 07-15-2012 11:12 AM

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.


All times are GMT -5. The time now is 10:35 PM.