This is interesting. Though I cannot reproduce the ".sh.level == 0" case.

$ printf "'%s'\n" "${.sh.level}"
''
$ ksh ./sh_level 3
''
'1'
'2'
'3'
$ . ./sh_level 3  
'1'
'2'
'3'
'4'

where shell program sh_level is

$ cat sh_level 
printf "'%s'\n" "${.sh.level}"
(( $1 > 0 )) && . ./sh_level $(( $1 - 1 ))

# Version JM 93t+ 2009-05-01

Under what condition will you get a "0" ?

Janis

----------------------------------------
> Date: Sat, 17 Dec 2011 01:23:10 +0100
> From: [email protected]
> To: [email protected]
> 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) ?
> CC: [email protected]
>
> Thanks Thomas,
>
> This does indeed make a difference. .sh.level appears to return
> the current '.' level.
>
> .sh.level == 0 indicates an interactive shell
> .sh.level == 1 indicates a '. file'
> .sh.level == 2 indicates a '. file1' with file1 containing '. file2'
> etc.
>
> .sh.level == '' indicates that the *current* file was directly invoked
> by the shell. (This resets the .sh.level completely)
>
> This is useful.
>
> Cheers,
> Henk
>
>
> [email protected]:
> > My post seems to have been lost. Re-sending ...
> > ________________________________________
> > From: [email protected]
> > Sent: den 13 december 2011 10:48
>
> > To: [email protected]
> > 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) ?
> >
> > Hi Henk,
> >
> > It seems like the parameter .sh.level does exactly what you are looking 
> > for, though I don't understand the documentation of it.
> >
> > $ cat u.sh
> > #!/bin/ksh
> > if [[ ${.sh.level} ]]
> > then
> > print SOURCED
> > else
> > print CALLED
> > fi
> > $ ./u.sh
> > CALLED
> > $ . ./u.sh
> > SOURCED
> >
> > $ cat t.sh
> > #!/bin/ksh
> > ./u.sh
> > . ./u.sh
> > $ ./t.sh
> > CALLED
> > SOURCED
> >
> > $ echo ${.sh.version}
> > Version JM 93t+ 2009-05-01
> >
> > BR
> > Thomas Magnusson
>
> _______________________________________________
> ast-users mailing list
> [email protected]
> https://mailman.research.att.com/mailman/listinfo/ast-users
                                          
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to