RE: Was I Sourced?

2013-02-12 Thread Teske, Devin
On Tue, 12 Feb 2013, Tim Daneliuk wrote:

 Is there a way for script to determine whether is was sourced
 or forked off as a subprocess when it was invoked?
 

Not that I'm aware of.


 I have a script that needs to be sourced to work properly and
 I want to warn the luser if they exec or subshell it instead.
 

In order for a user to exec a script it has to have the invocation line:

#!/bin/sh

So naturally, if your script is missing this as the first line, the file will 
not be interpretable (and thus exec will fail). And if the file is not 
executable (due to lack of invocation line) then you also shouldn't have any 
worry about execution within a sub-shell (though it will still be possible to 
source within a sub-shell using . -- but that shouldn't be any different 
than sourcing in the main shell).

One thin you could do is to use an invocation line that will warn the user that 
it shouldn't be executed directly...


#!/not_directly_executable # Use the source luke

So when someone executes your script they get...

-bash: ./script: /not_directly_executable: bad interpreter: No such file or 
directory
-- 
Devin

_
The information contained in this message is proprietary and/or confidential. 
If you are not the intended recipient, please: (i) delete the message and all 
copies; (ii) do not disclose, distribute or use the message in any manner; and 
(iii) notify the sender immediately. In addition, please be aware that any 
message addressed to our domain is subject to archiving and review by persons 
other than the intended recipient. Thank you.
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Was I Sourced?

2013-02-12 Thread Fabian Keil
Teske, Devin devin.te...@fisglobal.com wrote:

 On Tue, 12 Feb 2013, Tim Daneliuk wrote:
 
  Is there a way for script to determine whether is was sourced
  or forked off as a subprocess when it was invoked?
  
 
 Not that I'm aware of.

sysutils/zogftw, which has to find and parse itself to
generate the verbose help, does the following:

zogftw_location=$0

if [ zogftw != $(basename ${zogftw_location}) ]; then
# Looks like zogftw has been sourced.
# Try to get the zogftw location through the PATH.
zogftw_location=$(which zogftw)
fi

The check is expected to fail if the user renamed the shell to
zogftw, or the script to something else, but that's unlikely to
happen by accident and the functionality is not essential anyway.

A more reliable method might be investigating $$ and its parents
with ps and friends, but it would also require a lot more code.

I don't remember ever having never seen a ps-based check in the
real world and my impression is that looking at $0 is best practice
if the check doesn't have to be perfect.

Fabian


signature.asc
Description: PGP signature


Re: Was I Sourced?

2013-02-12 Thread Robert Bonomi

 Date: Tue, 12 Feb 2013 08:53:37 -0600
 From: Tim Daneliuk tun...@tundraware.com
 To: FreeBSD Mailing List freebsd-questions@freebsd.org
 Subject: Was I Sourced?

 Is there a way for script to determine whether is was sourced
 or forked off as a subprocess when it was invoked?

 I have a script that needs to be sourced to work properly and
 I want to warn the luser if they exec or subshell it instead.

a 'sourced' script does -not- honor a shebag line.
you can exploit that.

The executable script /usr/local/bin/source_only;
   #!/bin/sh
   echo  Error: this script must be sourced

Your script:
   #!/usr/local/bin/source_only

   {cmd}
   {cmd}
   {cmd}
   {cmd}
   {cmd}
   {cmd}
   ...
   ...

Trying to do it totally self-contained is not easy.


___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org


Re: Was I Sourced?

2013-02-12 Thread Tim Daneliuk

On 02/12/2013 11:10 AM, Robert Bonomi wrote:

Date: Tue, 12 Feb 2013 08:53:37 -0600
From: Tim Daneliuk tun...@tundraware.com
To: FreeBSD Mailing List freebsd-questions@freebsd.org
Subject: Was I Sourced?

Is there a way for script to determine whether is was sourced
or forked off as a subprocess when it was invoked?

I have a script that needs to be sourced to work properly and
I want to warn the luser if they exec or subshell it instead.


a 'sourced' script does -not- honor a shebag line.
you can exploit that.

The executable script /usr/local/bin/source_only;
#!/bin/sh
echo  Error: this script must be sourced

Your script:
#!/usr/local/bin/source_only

{cmd}
{cmd}
{cmd}
{cmd}
{cmd}
{cmd}
...
...

Trying to do it totally self-contained is not easy.





Actually, it's not that hard.  Setting the shebang line to this does
the trick:
  
  #!/bin/echo This Script Must Be Sourced
  


Thanks to all who replied on this one ...

  


--
---
Tim Daneliuk
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to freebsd-questions-unsubscr...@freebsd.org