Operating System - Linux
1753492 Members
4749 Online
108794 Solutions
New Discussion юеВ

How to run Shell script as a source

 
CA1490051
Frequent Advisor

How to run Shell script as a source

Hi all,

I want to run a shell script through another shell script but as a source.

Ex - Test1.sh
#!/usr/bin/sh
source Test2.sh

where my Test2.sh will be
#!/usr/bin/sh
export HAI=hai

If give ./Test1.sh

I says
Source not found .

can any one please suggest a solution for this

thanks in advance
Vikram
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: How to run Shell script as a source

Hi Vikram:

Sourcing (reading) is done thusly:

#!/usr/bin/sh
. ./Test2.sh


That is, you specify a dot, a whitespace (space, tab) and the name of the file that you want to source (read).

See my other example in your first post using a Perl script, too.

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: How to run Shell script as a source

Shalom,

Assuming the code is compatible.

. ./scriptname

dot space ./scriptname

If you are in the same directory

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor

Re: How to run Shell script as a source

... and one very important aspect of a sourced script is that it must not do an exit or a return because the sourced file is part of the foreground process. If you exit the sourced file, you exit the process.
If it ain't broke, I can fix that.
Oviwan
Honored Contributor

Re: How to run Shell script as a source

And as fas as I know you don't have to write #!/usr/bin/sh in your sourced script...
Bob E Campbell
Honored Contributor

Re: How to run Shell script as a source

>And as fas as I know you don't have to >write #!/usr/bin/sh in your sourced >script...

but it doesn't hurt and will change the output of file(1) from "ascii text" to "commands text".
Steven Schweda
Honored Contributor

Re: How to run Shell script as a source

"source file" is the C-shell syntax.
". file" is the Bourne[-like]-shell syntax.

/usr/bin/sh is a Bourne[-like] shell.
James R. Ferguson
Acclaimed Contributor

Re: How to run Shell script as a source

Hi (again):

> Steven: "source file" is the C-shell syntax.

It is also a built-in in the Bash shell ('bash'). There, either the dot syntax or the 'source' builtin can be used to import variables into the current environment.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: How to run Shell script as a source

>Steven: ". file" is the Bourne[-like]-shell syntax.
>/usr/bin/sh is a Bourne[-like] shell.

More precisely, it IS the Posix shell.
(The Bourne shell are not the droids you seek. You should only mention ksh and Posix shell. ;-)