Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

KSH how to get script name when sourced?

196 views
Skip to first unread message

Yonghang

unread,
Aug 23, 2016, 9:21:29 AM8/23/16
to
KSH only,

suppose I have to run a script in the way of ". ./test.sh", how can I get its name "test.sh" or "./test.sh" inside itself? this way $0 was set to ksh and its own name is just missing. need the solution in korn shell, is there anything like bash source variable in bash?

thanks.

Icarus Sparry

unread,
Aug 24, 2016, 2:52:34 PM8/24/16
to
If you have a sufficently modern ksh, then you will probably find the
filename inside the ${.sh.file} variable, although it should have been
converted to an absolute pathname, when you are inside the sourced file.


Yonghang

unread,
Aug 25, 2016, 2:22:55 PM8/25/16
to
thanks. Unfortunately ksh in my environment doesn't recognize this syntax.

Janis Papanagnou

unread,
Aug 25, 2016, 3:50:53 PM8/25/16
to
On 25.08.2016 20:22, Yonghang wrote:
> thanks. Unfortunately ksh in my environment doesn't recognize this syntax.

It would help to provide version information then; unfortunately we have no
second sight about your environment.

Janis

Icarus Sparry

unread,
Aug 26, 2016, 6:26:49 PM8/26/16
to
I agree. (http://catb.org/~esr/faqs/smart-questions.html#beprecise )

Of course it would help if the OP told us what it was that he wanted to
*actually do*. ( http://catb.org/~esr/faqs/smart-questions.html#goal ).

Then tell us what the actual restrictions are. For example why does it
*have* to be run as ". ./test.sh", rather than say ". ./test.sh ./
test.sh"?
Is it allowed to alter the test.sh script or not?
Is it allowed to change the meaning of the "." command?
Is it being run from an interactive shell, so we might be able to use
"history 0 0" to get the command, or is it being run from a script?
Can we set up a function to run the command?
Frequently the reason for using "." is to change the current environment
in some way, could we use have the script output the changes and then use
"eval"? Maybe we could use a couple of "exec" statements to replace the
current environment first with the script and then with a replacement
interactive shell.
0 new messages