Operating System - Linux
1748210 Members
3692 Online
108759 Solutions
New Discussion юеВ

shell issue: parameter substitution

 
SOLVED
Go to solution
Jdamian
Respected Contributor

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 ?
16 REPLIES 16
TY 007
Honored Contributor
Solution

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
J. Bravo
Respected Contributor

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.
TY 007
Honored Contributor

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
Jdamian
Respected Contributor

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])}"
AwadheshPandey
Honored Contributor

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
Pete Randall
Outstanding Contributor

Re: shell issue: parameter substitution

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


Pete

Pete
TY 007
Honored Contributor

Re: shell issue: parameter substitution

Hello Oscar,

# IP=192.112

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

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

Thanks
TY 007
Honored Contributor

Re: shell issue: parameter substitution

Hello Oscar,

# IP=192.112

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

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

Thanks
James R. Ferguson
Acclaimed Contributor

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...