LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   if [ ${filename##*.} != "gz" ] (https://www.linuxquestions.org/questions/linux-newbie-8/if-%5B-%24%7Bfilename-%2A-%7D-%3D-gz-%5D-4175586421/)

vincix 08-06-2016 07:30 AM

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?

michaelk 08-06-2016 08:00 AM

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

Turbocapitalist 08-06-2016 08:01 AM

That is an example of Bash's parameter expansion. Specifically, it is substring removal.

vincix 08-06-2016 08:24 AM

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

keefaz 08-06-2016 09:07 AM

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

vincix 08-06-2016 09:26 AM

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 :)

keefaz 08-06-2016 07:21 PM

This is a bash trick
Code:

path=(${PWD//\// })
echo ${path[0]}

Basicaly it does:

Code:

# replace all slashes (/) with a space in $PWD value
${PWD//\// }

# use the string delimited with spaces as array values
# array=($string_with_spaces)
path=(${PWD//\// })

# echo first element
echo ${path[0]}

Note: this only works with directories names that contain no spaces of course ;)
(A work around is possible with IFS setting)

chrism01 08-07-2016 08:35 PM

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 '/' ... ;)

MadeInGermany 08-08-2016 03:34 AM

Code:

if [ "${filename##*.}" != "gz" ]
is not 100% correct because it also (not) matches the file mame "gz".
To only (not) match a ".gz" name I suggest
Code:

if [[ $filename != *.gz ]]

chrism01 08-09-2016 12:32 AM

There's also the '=~ ' operator
http://www.tldp.org/LDP/abs/html/bas...#REGEXMATCHREF
http://www.computerworld.com/article...pressions.html

aragorn2101 08-09-2016 04:21 AM

It is string manipulation in Bash. Check page 122 in the following:

http://www.tldp.org/LDP/abs/abs-guide.pdf

vincix 08-09-2016 12:20 PM

Quote:

Originally Posted by MadeInGermany (Post 5587731)
Code:

if [ "${filename##*.}" != "gz" ]
is not 100% correct because it also (not) matches the file mame "gz".
To only (not) match a ".gz" name I suggest
Code:

if [[ $filename != *.gz ]]

I'm not sure if I follow you. So if the file is called "gz.tar", for instance, you mean to say that the condition will not verify, basically, if the file has the .gz extension? I'm not sure you're right. Can you expand on that?

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.