Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

shell issue: parameter substitution

SOLVED
Go to Solution
Highlighted
Respected Contributor 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 ?
1 ACCEPTED SOLUTIONS
Honored Contributor Honored Contributor

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
16 REPLIES
Honored Contributor Honored Contributor

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 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.
Honored Contributor 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
Respected Contributor 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])}"
Honored Contributor 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
Outstanding Contributor Outstanding Contributor

Re: shell issue: parameter substitution

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


Pete

Pete
Honored Contributor 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
Honored Contributor 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
Acclaimed Contributor 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...
Honored Contributor 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
Honored Contributor 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
Acclaimed Contributor 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 "?".
Respected Contributor 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
Acclaimed Contributor 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 ;-)
Honored Contributor 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.
Respected Contributor Respected Contributor

Re: shell issue: parameter substitution

thanx