Visit Our UNIX and Linux User Community


meaning of ${0%${0##*/}}


 
Thread Tools Search this Thread
Operating Systems AIX meaning of ${0%${0##*/}}
# 1  
Old 09-22-2008
meaning of ${0%${0##*/}}

. ${0%${0##*/}}Script_Name

if i issue this command, it is executing the script. can any one tell what is the meaning of ${0%${0##*/}}
# 2  
Old 09-22-2008
Code:
man <your shell>

# 3  
Old 09-22-2008
That's a sequence of two variable substitutions.

${var##string} returns $var with the longest possible prefix matching string removed

${var%string} returns $var with the shortest possible suffix matching string removed

Combined, these return the path name component from $0 which is the name of the currently running script. In other words, the construct attempts to find the file Script_Name from the same directory where the current script is running from, and execute it using the shell's source (dot) command. This is a common technique for including a snippet of shared code from another file into a shell script.
# 4  
Old 09-22-2008
Thanks... That was really very helpful... I came to know the meaning of ${var##string} and ${var%string}.

if i am in some XYZ dir. I have 2 scripts, Script1 and Script2 in it.

To call Script2 from Script1, I can use

. ${0%${0##*/}}Script2 or . ./Script2

Both do the same job. then why use this ". ${0%${0##*/}}Script2" instead of simply using ". ./Script2"?

Last edited by nyelavarthy; 09-22-2008 at 07:31 AM..
# 5  
Old 09-22-2008
Code:
$ temp=/u/ppattusa/unix
$ echo ${temp%${temp##*/}}
 
OUTPUT
/u/ppattusa/

pls see the above example, temp having some path and and the substitution will return the base path.

I think to pass the path as parameter they used like that. If you provide the script1 code sample, then it will be easy to find for wat purpose they using like that.
# 6  
Old 09-22-2008
Quote:
Originally Posted by nyelavarthy
. ${0%${0##*/}}Script2 or . ./Script2

Both do the same job. then why use this ". ${0%${0##*/}}Script2" instead of simply using ". ./Script2"?
In this case they expand to the same value, but that has not always to be the case. Suppose the following: you call your script not from the directory where it is stored but from somewhere else:

Code:
$ cd /some/dir
$ /some/other/dir/script
$ _

inside your script the "./" will expand to the PWD, which is not where the script is stored but where you have been when starting the script - "/some/dir" in the example, not "/some/other/dir". If you now execute

Code:
. ./script2

you will execute "/some/dir/script2" instead of "/some/other/dir/script2". Still $0 will contain "/some/other/dir/script" and subsequently the expansion will find script2 on its correct place.

As a rule of thumb: avoid relative paths like the plague in scripts. Always use absolute paths to make sure you end up where you suppose to end up.

I hope this helps

bakunin
# 7  
Old 09-23-2008
scripts adminBuildDomain and crstEnv are in the same directory. from this directory only we run all the scripts.

adminBuildDomain calls crstEnv

Code in adminBuildDomain is: . ${0%${0##*/}}crstEnv

${0%${0##*/}}crstEnv returns crstEnv value

since the directory path where we run the scripts is same as the directory where the scripts are present, we can use . ./crstEnv instead of . ${0%${0##*/}}crstEnv

i really dont understand the importance of ". ${0%${0##*/}}crstEnv" in this case. i dont understand why ". ${0%${0##*/}}crstEnv" is used instead of ". ./crstEnv"

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Meaning of $1^

Hello everyone, I'm looking for the meaning of this expression, as I don't understand it quite clearly : $1^ What do you think it could be? I thought either: - match lines starting with argument 1 but it should be ^$1 - turn line around : word becomes drow Thanks in advance for your... (4 Replies)
Discussion started by: bibelo
4 Replies

2. Shell Programming and Scripting

Meaning

Please let me know the meaning for the below statements in shell scripting. 1) exit -99 -------------------------------- 2) set prgdir = `pwd` set runFlag = runFlag:FALSE ------------------------------------- 3) if (-f $prgdir/maillst.eml) then set distEmail = `cat $prgdir/maillst.eml`... (1 Reply)
Discussion started by: lg123
1 Replies

3. UNIX for Dummies Questions & Answers

meaning of <<!

Hi all, I wanna know the meaning of the last word "<<! " sudo su - user <<! please help on this !!!! (1 Reply)
Discussion started by: sudharson
1 Replies

4. Shell Programming and Scripting

^$$ meaning

Hi , Can anyone please let me know whta the follwoing piece of code for ScriptName=${0##*/} if pgrep -f "$ScriptName" | grep -v "^$$\$" ; then echo `date`": Sctipt $ScritName is already runnig" exit fi Thnx a lot in advance Please use code tags when posting data and code... (8 Replies)
Discussion started by: Pratik4891
8 Replies

5. Shell Programming and Scripting

meaning of !*

can someone please tell what !* means in shell syntax. Regards, (3 Replies)
Discussion started by: busyboy
3 Replies

6. Shell Programming and Scripting

What is the meaning of $_

Hi, Can somebody tell the usage of "$_" cd $_ ? and ls $_ ? (4 Replies)
Discussion started by: giri_luck
4 Replies

7. UNIX for Dummies Questions & Answers

what the meaning of #*

can some one please tell the meaning of the second statement i.e n=${m#*=} i couldnt get the meaning of the #*= 1.) m="mohit=/c/main/issue" echo $m result ----------- mohit=/c/main/issue 2.) n=${m#*=} echo $n RESULT ------- /c/main/issue (1 Reply)
Discussion started by: narang.mohit
1 Replies

8. Linux

What are the meaning of these macros..

Masters, I am trying to learn the serial mouse driver for linux kernel. On the kernel source tree I find out these macros and I am unable to find out the meaning of these macros. Please anyone help me to understand these. These macros are defined in linux/serio.h... (2 Replies)
Discussion started by: iamjayanth
2 Replies

9. UNIX for Dummies Questions & Answers

Use and meaning of $*

Can someone explain the use and meaning of "$*" expression. (2 Replies)
Discussion started by: sinpeak
2 Replies

10. Shell Programming and Scripting

what is the meaning here?

#!/bin/sh $ORACLE_HOME/bin/sqlplus -S $orauserid/$orapasswd@$oradb << _TMP alter session set nls_date_format = 'YYYYMMDD HH24:MI'; set linesize 100 set pagesize 400 ok the above is part of a script..i just wanna know what does sqlplus -S means?? as in why we need to insert the -S behind? (2 Replies)
Discussion started by: forevercalz
2 Replies

Featured Tech Videos