Operating System - Linux
1752270 Members
4848 Online
108786 Solutions
New Discussion юеВ

Re: shell issue: parameter substitution

 
SOLVED
Go to solution
AwadheshPandey
Honored Contributor

Re: shell issue: parameter substitution

http://wwwhome.cs.utwente.nl/~broekjg/bsc/scripts/phase2.txt
It's kind of fun to do the impossible
Sandman!
Honored Contributor

Re: shell issue: parameter substitution

# echo "${IP%+([0-9]).+([0-9])}"

In the above expression the shell is doing what it has been asked of; delete the shortest matching pattern as a single percent sign was specified. On the other hand if the expression was:

# echo "${IP%%+([0-9]).+([0-9])}"

...then it deletes the longest matching pattern which is the entire string as a double percent sign was specified. See the sh-posix(1) man page for details.

~cheers
Dennis Handly
Acclaimed Contributor

Re: shell issue: parameter substitution

>JRF: You should escape the dot which otherwise means any character.

Actually no. This is a file matching pattern not a regular expression. In a pattern you have "*" and "?".
Jdamian
Respected Contributor

Re: shell issue: parameter substitution

Some explanations:

I'm trying to check lexicographically an IP address.

I'm very distrustful :-( -- I don't trust external commands as grep or awk, so I prefer to use shell internal tricks


I know the difference of ${%} and ${%%} but in my example pattern

echo "${IP%+([0-9]).+([0-9])}"

I cannot understand why the same sub-pattern

+([0-9])

matches the full last byte (112) BUT only one digit from the first byte (192).

Does the 'shortest length' behaviour of ${%} apply only to the sub-pattern following the character '%' ?

Thanx in advance
Dennis Handly
Acclaimed Contributor

Re: shell issue: parameter substitution

>BUT only one digit from the first byte (192).
>Does the 'shortest length' behaviour of ${%} apply only to the sub-pattern following the character '%'?

As Sandman said, it applies to everything. Since the last pattern must match ALL of 112 to match the ".", it does. But to match 192, it only has to match the shortest.

I would suggest you trust in the grep and regular expressions. grep/awk aren't the darkside ;-)
Sandman!
Honored Contributor

Re: shell issue: parameter substitution

>Does the 'shortest length' behaviour of ${%} apply only to the sub-pattern
>following the character '%' ?

Yes it does...and the difference can be seen in the following constructs...

# IP=192.112
# echo "${IP%+([0-9])}"
192.11
# echo "${IP%%+([0-9])}"
192.
Jdamian
Respected Contributor

Re: shell issue: parameter substitution

thanx