Subject: Re: [ast-users] Is there a simple way for a script to know if it's run
as a script (ksh script ) or sourced (. script) ?
--------
> I've been working on submitting my scripts to unit tests (a story in
> itself, ask me if you're interested).
>
> This puts some (good) restrictions and challenges on my scripts:
>
> So should all code should be wrapped in functions. (Without functions,
> no units, no unit tests)
>
> But I need a way to determine if a script is being sourced or is
> actually called by the interpreter.
>
> So far the only way I've found requires that the script knows the name
> it is going to be called by.
>
> For the ksh88 shell under Solaris, I found that $LINENO does not
> increment beyond 1 when a file is sourced.
> Alas, this may be considered a bug, as ksh93 does not show this
> behaviour.
>
> Any ideas beyond:
>
> ----- snip -----
> #!/bin/ksh
> .
> . function definitions ...
> .
> myname=script.sh
> [[ ${0##*/} == $myname ]] && main "$@"
> ----- snip -----
>
> Cheers,
> Henk Langeveld
You should be able to use the variable SHLVL for this. The value is
incremented if the script is run by a separate shell or as a script.
David Korn
[email protected]
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users