Re: shell issue: parameter substitution (433 Views)
Reply
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 1 of 17 (433 Views)
Accepted Solution

shell issue: parameter substitution

Hi

I'm using the ${#} to check lexicographycally an IP address:

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

Why does the pattern +([0-9]) remove just ONE digit from the first number while those same pattern removes all digits from the second number ?
Honored Contributor
TY 007
Posts: 377
Registered: ‎08-06-2007
Message 2 of 17 (433 Views)

Re: shell issue: parameter substitution

Hello Oscar,

Tested in a HP-UX 11.11 OS:

# echo $SHELL
/sbin/sh

# IP=192.112

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

Thanks
Respected Contributor
J. Bravo
Posts: 106
Registered: ‎09-20-2007
Message 3 of 17 (433 Views)

Re: shell issue: parameter substitution

In my 11.23 box it's working fine with POSIX shell:

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

Best regards.
Honored Contributor
TY 007
Posts: 377
Registered: ‎08-06-2007
Message 4 of 17 (433 Views)

Re: shell issue: parameter substitution

Hello Oscar,

Tested as well in HP-UX 11.00 OS.

# uname -r
B.11.00

# echo $SHELL
/sbin/sh

# IP=192.112

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

Thanks
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 5 of 17 (433 Views)

Re: shell issue: parameter substitution

I'm sorry but my patter posted here has a missing character ')', i.e., it's wrong.

Try this next one:

echo "${IP%+([0-9]).+([0-9])}"
Honored Contributor
AwadheshPandey
Posts: 737
Registered: ‎09-18-2005
Message 6 of 17 (433 Views)

Re: shell issue: parameter substitution

Oscar!!

Can you clear what actually you want to print?
Regards,

Awadhesh
It's kind of fun to do the impossible
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 7 of 17 (433 Views)

Re: shell issue: parameter substitution

# IP=192.112
# echo "${IP%+([0-9]).+([0-9])}"
19
# uname -r
B.11.11


Pete

Pete
Honored Contributor
TY 007
Posts: 377
Registered: ‎08-06-2007
Message 8 of 17 (433 Views)

Re: shell issue: parameter substitution

Hello Oscar,

# IP=192.112

# AA=`echo $IP | awk -F. '{print $1}'`

# echo "${AA%+([0-9])}"
19

Thanks
Honored Contributor
TY 007
Posts: 377
Registered: ‎08-06-2007
Message 9 of 17 (433 Views)

Re: shell issue: parameter substitution

Hello Oscar,

# IP=192.112

# BB=`echo $IP | awk -F. '{print $2}'`

# echo "${BB%+([0-9])}"
11

Thanks
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 17 (433 Views)

Re: shell issue: parameter substitution

Hi Oscar:

# echo "${IP%\.*}"
192

# echo "${IP##[0-9]*\.}"
112

You should escape the dot which otherwise means any character.

Regards!

...JRF...
Honored Contributor
AwadheshPandey
Posts: 737
Registered: ‎09-18-2005
Message 11 of 17 (433 Views)

Re: shell issue: parameter substitution

http://wwwhome.cs.utwente.nl/~broekjg/bsc/scripts/phase2.txt
It's kind of fun to do the impossible
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 12 of 17 (433 Views)

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
Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 13 of 17 (433 Views)

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 "?".
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 14 of 17 (433 Views)

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
Acclaimed Contributor
Dennis Handly
Posts: 25,272
Registered: ‎03-06-2006
Message 15 of 17 (433 Views)

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 ;-)
Honored Contributor
Sandman!
Posts: 2,220
Registered: ‎01-13-2005
Message 16 of 17 (433 Views)

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.
Respected Contributor
Jdamian
Posts: 721
Registered: ‎06-21-2000
Message 17 of 17 (433 Views)

Re: shell issue: parameter substitution

thanx
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.