Re: magical script (195 Views)
Reply
Advisor
Vittorio_3
Posts: 24
Registered: ‎08-03-2003
Message 1 of 8 (195 Views)
Accepted Solution

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%/*}/} # !!!!!!!!!
Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 2 of 8 (195 Views)

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.




Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 3 of 8 (195 Views)

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
Advisor
Vittorio_3
Posts: 24
Registered: ‎08-03-2003
Message 4 of 8 (195 Views)

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
Advisor
Vittorio_3
Posts: 24
Registered: ‎08-03-2003
Message 5 of 8 (195 Views)

Re: magical script

All clear now, tx all
Acclaimed Contributor
Dennis Handly
Posts: 25,074
Registered: ‎03-06-2006
Message 6 of 8 (195 Views)

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.
Honored Contributor
Laurent Menase
Posts: 1,079
Registered: ‎11-06-2003
Message 7 of 8 (195 Views)

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

Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 8 of 8 (195 Views)

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.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.