Re: not getting these variable values ${0##*/} and ${0%/*} (125 Views)
Reply
Trusted Contributor
Vishu
Posts: 406
Registered: ‎03-27-2007
Message 1 of 5 (144 Views)
Accepted Solution

not getting these variable values ${0##*/} and ${0%/*}

Hi,

 

Please tell what is the actual value (         ${0##*/}  and  ${0%/*}      ) defined to the variable below. we have a script and i did not get these values

 

typeset -x NAME=${0##*/}
typeset -x DIR=${0%/*}

 

 

Thanks

Please use plain text.
Honored Contributor
Patrick Wallek
Posts: 13,630
Registered: ‎06-21-2000
Message 2 of 5 (137 Views)

Re: not getting these variable values ${0##*/} and ${0%/*}

These basically strip down the path of the command executed to the directory (DIR) and FILE (NAME).

 

For example, I created a script:

 

 # cat /root/test.pw
#!/usr/bin/sh

typeset -x NAME=${0##*/}
typeset -x DIR=${0%/*}

echo NAME=${NAME}
echo DIR=${DIR}

 

And then I ran it.

 

First I ran it from the /root directory and invoked it with just a './test.pw'.

# ./test.pw
NAME=test.pw
DIR=.

 

The NAME is the script name and the DIR is the '.' portion indicating it's run from the current directory.

 

I then ran it as '/root/test.pw':

 

# /root/test.pw
NAME=test.pw
DIR=/root

 

NAME still gives test.pw, which it should, and DIR gives '/root' as that is the directory portion of the command line.

 

If I place the script in /var/tmp and run it via '/var/tmp/test.pw' then you get:

 

# /var/tmp/test.pw
NAME=test.pw
DIR=/var/tmp

 

If I copy the script to a directory that is in my PATH environment variable, /root/bin in this case, and run it:

 

# test.pw
NAME=test.pw
DIR=/root/bin

 

I get the directory that the script is in.

 

Does this all make sense?

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,381
Registered: ‎03-06-2006
Message 3 of 5 (125 Views)

Re: not getting these variable values ${0##*/} and ${0%/*}

Basically these are the shell builtin replacements for dirname(1) and basename(1), operating on $0:

export DIR=$(dirname $0)

export NAME=$(basename $0)

Please use plain text.
Trusted Contributor
Vishu
Posts: 406
Registered: ‎03-27-2007
Message 4 of 5 (120 Views)

Re: not getting these variable values ${0##*/} and ${0%/*}

Thanks Partick and Dennis,

so it means if it use the same to $1 variable, then it will provide the DIR and NAME value for that script executed...correct?

e.g. typeset value=$1
typeset file=${1##*/}

It will give me the value of the file name i will be entering at $1 variable...got it.

But how do i get details about these symbols like ##*/ or %/* as one counts for file name and other for directory name. Are there other symbols like these and how can i get all those symbols so that i can use them also???

Much Thanks for your reply.
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,381
Registered: ‎03-06-2006
Message 5 of 5 (108 Views)

Re: not getting these variable values ${0##*/} and ${0%/*}

[ Edited ]

>how do I get details about these symbols like ##*/ or %/*

 

Read the manpage carefully.  Look at what other well written shell scripts do.

And you also need to understand patterns.

 

>Are there other symbols like these

 

Look at the man pages for real shells: ksh(1) and sh_posix(1)

Parameter Substitution:

${parameter}  
${#parameter}
${#identifier[*]}
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}


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

$#
$-
$?
$$
$_
$!

Please use plain text.
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