- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: not getting these variable values ${0##*/} an...
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
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
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-06-2013 10:41 AM
06-06-2013 10:41 AM
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
Solved! Go to Solution.
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2013 11:21 AM
06-06-2013 11:21 AM
SolutionThese 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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2013 12:18 AM
06-07-2013 12:18 AM
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2013 04:03 AM
06-07-2013 04:03 AM
Re: not getting these variable values ${0##*/} and ${0%/*}
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2013 12:54 AM - edited 06-08-2013 12:55 AM
06-08-2013 12:54 AM - edited 06-08-2013 12:55 AM
Re: not getting these variable values ${0##*/} and ${0%/*}
>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.
$#
$-
$?
$$
$_
$!