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:
#################################### 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 |
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. |
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}'` Code:
FILE_NOEXT_VAR=`echo $FILE_VAR | sed -E 's/\.t(ar\.)?[gb]z$//g'` |
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##*/} Code:
shopt -s extglob 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. |