if [ ${filename##*.} != "gz" ]
I don't understand the meaning of the sentence written in the subject. What I don't understand exactly is the meaning of ## in this case. What does it mean?
How does it combine with a ${variable} construction? |
It removes the longest match from the front of the string. For example if the file name is something.tar.gz a single # would be tar.gz. And ## would be gz.
http://tldp.org/LDP/abs/html/string-manipulation.html |
That is an example of Bash's parameter expansion. Specifically, it is substring removal.
|
I'm currently reading the advanced bash scripting tutorial from tldp.org, and there are a lot of examples at the beginning using notions that are explained later on. Even so, I'm still trying to understand as much as possible at this stage so as to understand it much better later on when they're actually explained :)
I still haven't understood it fully at the stage, but both your links are useful, so I guess I'll get it eventually. @michaelk what do you mean by 'the front of the string'? Do you mean to say the beginning? So let's say we have: filename=something.tar.gz if [ ${filename##.*} != "gz" ] Would it be a match in this case? If so, I don't understand the .* at the end, 'cause that's where .tar.gz or .gz should be, shouldn't it? It's not that straightfoward :D P.S. I've just read this from turbocapitalist's link. Made things much more comprehensible: Get name without extension ${FILENAME%.*} ⇒ bash_hackers.txt Get extension ${FILENAME##*.} ⇒ bash_hackers.txt Get directory name ${PATHNAME%/*} ⇒ /home/bash/bash_hackers.txt Get filename ${PATHNAME##*/} ⇒ /home/bash/bash_hackers.txt |
It's not a regular expression, it's a shell parameter expansion, so .* is not a pattern
.* in a parameter expansion means a dot char '.' followed by zero or more characters |
Yeah, I'm starting to get it.
One other question. Let's say I'm in /home/user. If I try: echo ${PWD%/*} I get: /home Is there a way I can get only the "home" string using substring removal without turning to another tool, such as sed, or tr or whatever? Now I think it's a little bit of a silly question, 'cause you either start from the beginning or the end with subtring removal, so if you start from the end of the string, the first character of the string "/" is obviously going to be there :) |
This is a bash trick
Code:
path=(${PWD//\// }) Code:
# replace all slashes (/) with a space in $PWD value (A work around is possible with IFS setting) |
PE does what is says, but you can of course take the o/p of one PE and feed it into another PE to eg remove the leading '/' ... ;)
|
Code:
if [ "${filename##*.}" != "gz" ] To only (not) match a ".gz" name I suggest Code:
if [[ $filename != *.gz ]] |
There's also the '=~ ' operator
http://www.tldp.org/LDP/abs/html/bas...#REGEXMATCHREF http://www.computerworld.com/article...pressions.html |
It is string manipulation in Bash. Check page 122 in the following:
http://www.tldp.org/LDP/abs/abs-guide.pdf |
Quote:
Ok, now I get it. You mean to say that if there's a file called "gz" (without any extension), then it would take it (or not) to be a .gz archive, for instance, so it would be a completely correct condition. Yes, nice observation. Thank you for that :) |
All times are GMT -5. The time now is 06:12 PM. |