- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- shell issue: parameter substitution
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 08:03 PM
09-25-2007 08:03 PM
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 ?
Solved! Go to Solution.
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 08:28 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 08:32 PM
09-25-2007 08:32 PM
Re: shell issue: parameter substitution
# IP=192.112
# echo "${IP%+([0-9].+([0-9])}"
192.112
Best regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 09:01 PM
09-25-2007 09:01 PM
Re: shell issue: parameter substitution
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 09:24 PM
09-25-2007 09:24 PM
Re: shell issue: parameter substitution
Try this next one:
echo "${IP%+([0-9]).+([0-9])}"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 09:35 PM
09-25-2007 09:35 PM
Re: shell issue: parameter substitution
Can you clear what actually you want to print?
Regards,
Awadhesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 10:10 PM
09-25-2007 10:10 PM
Re: shell issue: parameter substitution
# echo "${IP%+([0-9]).+([0-9])}"
19
# uname -r
B.11.11
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 10:42 PM
09-25-2007 10:42 PM
Re: shell issue: parameter substitution
# IP=192.112
# AA=`echo $IP | awk -F. '{print $1}'`
# echo "${AA%+([0-9])}"
19
Thanks
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 10:44 PM
09-25-2007 10:44 PM
Re: shell issue: parameter substitution
# IP=192.112
# BB=`echo $IP | awk -F. '{print $2}'`
# echo "${BB%+([0-9])}"
11
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 11:48 PM
09-25-2007 11:48 PM
Re: shell issue: parameter substitution
# echo "${IP%\.*}"
192
# echo "${IP##[0-9]*\.}"
112
You should escape the dot which otherwise means any character.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2007 11:56 PM
09-25-2007 11:56 PM
Re: shell issue: parameter substitution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2007 04:00 AM
09-26-2007 04:00 AM
Re: shell issue: parameter substitution
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2007 09:38 PM
09-26-2007 09:38 PM
Re: shell issue: parameter substitution
Actually no. This is a file matching pattern not a regular expression. In a pattern you have "*" and "?".
- Tags:
- regex
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2007 10:32 PM
09-26-2007 10:32 PM
Re: shell issue: parameter substitution
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2007 10:51 PM
09-26-2007 10:51 PM
Re: shell issue: parameter substitution
>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 ;-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2007 02:45 AM
09-27-2007 02:45 AM
Re: shell issue: parameter substitution
>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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2007 09:03 PM
09-27-2007 09:03 PM