Bash function - return parent script file path -


i have bash script containing function sourced number of different bash scripts. function may fail based on input, , i'd create logging within function identify script(s) causing failures.

e.g., source /path/to/function.sh

the closest i've come this:

ps --no-heading -ocmd -p $$ 

this works enough if full file path used run parent script, returning:

/bin/bash /path/to/parent.sh 

but fails provide full path if parent script run relative path, returning:

/bin/bash ./parent.sh 

ideally, i'd way reliably return parent script file path both cases.

i suppose have each parent script pass file path function (via $0 or similar), seems hard enforce , not terribly elegant.

any ideas, or alternative approaches? should not worry relative path case, , use full/absolute file paths everything?

thanks!

i'm using centos 5.9. bash version - gnu bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

you can use readlink follow symbolic links absolute path.

echo $(readlink -f $0) 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -