Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

magical script

SOLVED
Go to solution
Highlighted
Vittorio_3
Advisor

magical script

Hi,
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%/*}/} # !!!!!!!!!
7 REPLIES
Laurent Menase
Honored Contributor
Solution

Re: magical script

man ksh
${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.




Matti_Kurkela
Honored Contributor

Re: magical script

Wouldn't ${0##*/} accomplish the same thing in a more straightforward way?

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
MK
Vittorio_3
Advisor

Re: magical script

Tx, Laurent and Matt !!
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
Vittorio_3
Advisor

Re: magical script

All clear now, tx all
Dennis Handly
Acclaimed Contributor

Re: magical script

>so those 2 lines works in the same way?
${parameter%pattern}
${parameter%%pattern}

No. From ksh(1): in the latter case, the largest matching pattern is deleted.
Laurent Menase
Honored Contributor

Re: magical script

${parameter%pattern}
${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

Bill Hassell
Honored Contributor

Re: magical script

Removing leading directories can be done with the command basename. However, any POSIX shell will support the very simple construct mentioned above:

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