- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- magical script
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
Discussions
Discussions
Forums
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
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
тАО06-03-2011 11:29 PM
тАО06-03-2011 11:29 PM
Can anybody help me to do reverse explanation how this string was done, I tried to do my best but failed. Could not find anywhere, especially about % sign
# /root/bin/mar/generic_dr.ksh
# becomes generic_dr.ksh
PGM=${0#${0%/*}/} # !!!!!!!!!
Solved! Go to Solution.
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 12:53 AM
тАО06-04-2011 12:53 AM
Solution${parameter#pattern}
${parameter##pattern}
If the shell pattern matches the beginning of the value of parameter, the value of this substitution is the value of the parameter with the matched portion deleted; otherwise the value of this parameter substituted. In the former case, the smallest matching pattern is deleted; in the latter case, the largest matching pattern is deleted.
${parameter%pattern}
${parameter%%pattern}
If the shell pattern matches the end of the value of parameter, the value of parameter with the matched part is deleted; otherwise substitute the value of parameter. In the former, the smallest matching pattern is deleted; in the latter, the largest matching pattern is deleted.
So ${0%/*} remove the shortest ending of the program name $0, which match with /*
so for instance
/titi/toto/tata/tutu ->
echo ${0%/*}
/titi/toto/tata
${0#${0%/*}/} remove the shortest from the begining matching with the previous + /
so
/titi/toto/tata/tutu -> we remove /titi/toto/tata + / from the begining
-> tutu
it is the same as $(basename $0) but without calling a coprocess.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 05:14 AM
тАО06-04-2011 05:14 AM
Re: magical script
Or is/was there a version of ksh that had the ${parameter#pattern} and ${parameter%pattern} matches, but not the ${parameter##pattern} and ${parameter%%pattern} versions?
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 09:48 AM
тАО06-04-2011 09:48 AM
Re: magical script
Matti, your version works just fine too.
Just one small ?:
so those 2 lines works in the same way?
${parameter%pattern}
${parameter%%pattern}
Tx
N
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 10:08 AM
тАО06-04-2011 10:08 AM
Re: magical script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 11:44 AM
тАО06-04-2011 11:44 AM
Re: magical script
${parameter%pattern}
${parameter%%pattern}
No. From ksh(1): in the latter case, the largest matching pattern is deleted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 12:05 PM
тАО06-04-2011 12:05 PM
Re: magical script
${parameter%%pattern}
for instance
parameter a=/toto/titi/tata/tutu
echo ${a%%t*}
-> / => because the longest which matches with t* is toto/titi/tata/tutu
echo ${a%t*}
-> /toto/titi/tata/tu => because the shortest which matches with t* is tu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-04-2011 01:32 PM
тАО06-04-2011 01:32 PM
Re: magical script
MYNAME=$(basename $0)
or
MYNAME=${0##*/}
This shell construct returns just the name of the current script. There is a companion program called dirname which returns everything except the name of the script:
MYDIR=$(dirname $0)
or MYNAME=${0%/*}
These techniques use shell built-in features which are very fast and portable. Unfortunately the man pages are not very clear and don't useful examples.
Here's another technique:
MYCPU=cpu1.mysite.com
echo ${MYCPU%%.*}
cpu1
This extracts just the computer name and drops the rest of the domain name.
Bill Hassell, sysadmin