[comp.unix.questions] Can you access command line args from a source'd script?

boysko@dsrgsun.CES.CWRU.Edu (Glenn Boysko) (08/25/90)

Is it possible to get the filename of a sourced file (either Csh or Sh) while
the file is being sourced?

For example, can you have a sourced file echo its pathname?  Something like:

	% source <some_path_name>

	I am <some_path_name>

Curious,
Glenn

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Glenn Boysko		clsi!glenn@uunet.uu.net

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (08/25/90)

In article <1990Aug24.191615.27944@usenet.ins.cwru.edu> boysko@dsrgsun.CES.CWRU.Edu (Glenn Boysko) writes:
> Is it possible to get the filename of a sourced file (either Csh or Sh) while
> the file is being sourced?

Not easily. There is, however, a useful trick in the other direction:
If you start a script with

  #!/bin/echo sorry,try:source

then someone who executes it without typing ``source'' will be warned
what to do. This makes a convenient wrapper for tset-type programs.

---Dan

brad@SSD.CSD.HARRIS.COM (Brad Appleton) (08/27/90)

I had to do this once! What I had to do was create an alias:

	alias mycmd "set my_argv = ( \!* ); source myfile"

then `myfile' could go around using `my_argv' like scripts use $argv;
Big drawback to this approach is that you have to be real carefule not
to stomp on things in the environment since you are NOT a separate shell.
You should probably take care to unset any variables that you used and 
be REAL careful not to overwrite any pre-existing variables (unless you
know what you are doing with the variables in question).
______________________ "And miles to go before I sleep." ______________________
 Brad Appleton        brad@travis.ssd.csd.harris.com   Harris Computer Systems
                          ...!uunet!hcx1!brad          Fort Lauderdale, FL USA
~~~~~~~~~~~~~~~~~~~~ Disclaimer: I said it, not my company! ~~~~~~~~~~~~~~~~~~~

gt0178a@prism.gatech.EDU (BURNS,JIM) (08/29/90)

in article <801@travis.csd.harris.com>, brad@SSD.CSD.HARRIS.COM (Brad Appleton) says:
> 	alias mycmd "set my_argv = ( \!* ); source myfile"
> 
> Big drawback to this approach is that you have to be real carefule not
> to stomp on things in the environment since you are NOT a separate shell.

Just enclose your alias' 2nd parameter in '( and )', ala:

alias mycmd '( set my_argv = ( \!* ); source myfile )'

and you will run in a subshell.
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

volpe@underdog.crd.ge.com (Christopher R Volpe) (09/14/90)

In article <1990Aug24.191615.27944@usenet.ins.cwru.edu>,
boysko@dsrgsun.CES.CWRU.Edu (Glenn Boysko) writes:
|>Is it possible to get the filename of a sourced file (either Csh or Sh) while
|>the file is being sourced?
|>
|>For example, can you have a sourced file echo its pathname?  Something like:
|>
|>	% source <some_path_name>
|>
|>	I am <some_path_name>

Try the following: Create file "stest" containing the following:
  set line=`history 1`
  echo "My name is " $line[3]
  echo "My first arg is " $line[4]
  
%source stest arg1
My name is  stest
My first arg is  arg1
%

I'm sure you'll want to make it more robust, but that's the 
general idea.            
==================
Chris Volpe
G.E. Corporate R&D
volpecr@crd.ge.com

barmar@think.com (Barry Margolin) (09/15/90)

In article <11888@crdgw1.crd.ge.com> volpe@underdog.crd.ge.com (Christopher R Volpe) writes:
>In article <1990Aug24.191615.27944@usenet.ins.cwru.edu>,
>boysko@dsrgsun.CES.CWRU.Edu (Glenn Boysko) writes:
>|>Is it possible to get the filename of a sourced file (either Csh or Sh) while
>|>the file is being sourced?
>Try the following: Create file "stest" containing the following:
>  set line=`history 1`
>  echo "My name is " $line[3]
>  echo "My first arg is " $line[4]

This will only work for scripts sourced interactively.  If stest is sourced
from another sourced script then it will show the outer script name.  And
if it is sourced from a script executed in a subshell then there won't be
any history maintained for that shell.
--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar