开发者

Get Current Script Name While Was Calling from Another Script

In KornShell (ksh), I have a script that calls a list of scripts. The callee script needs to know its file name, so that it can generate unique configuration (or anything should be unique).

The problem is variable $0 always points to the caller script. For example, we have two scripts:

caller.sh:

. callee.sh

callee.sh:

echo $0

When I execute caller.sh, the callee.sh print "caller.sh", not "callee.sh". So how can I get the script file name of current running script?开发者_如何学JAVA

The script is running on AIX servers, so bash is not available all the time.


It's because you are sourcing callee.sh, i.e. . callee.sh.

So if you can just execute callee.sh (and you don't need environment variables that are set in callee.sh to be active in caller.sh,) change your caller scritpt to

#!/bin/ksh
callee.sh

I hate to say it, but I think the ksh people would say 'Working as designed'.


Following command in callee.sh script worked for me.

echo $(basename ${BASH_SOURCE[0]})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜