f



How tosource sh script/setup file from tcl script?

I have a tcl script that generates sh command file that looks like this:
setenv var1 value1
setenv var2 value2
etc.

i would like to source so all the variables will be set to the terminal window.

any idea's?

i've tried >exec source my_command_file

but it seems that tcl has "source" command as well and its not working...

Thanks,
G
0
Guy
7/31/2014 11:22:19 AM
comp.lang.tcl 23428 articles. 2 followers. Post Follow

3 Replies
775 Views

Similar Articles

[PageSpeed] 51

On Thursday, July 31, 2014 1:22:19 PM UTC+2, Guy wrote:
> I have a tcl script that generates sh command file that looks like this:
>=20
> setenv var1 value1
> setenv var2 value2
>=20
> i would like to source so all the variables will be set to the terminal w=
indow.

Note that the above syntax is rather that of the "other" shell family: csh,=
 not sh.

  SH-Family:   var1=3Dvalue1
 =20
  CSH-Family:  setenv var1 value1

Anyway, whatever the shell, the principle to keep in mind is that no child =
process (like a tclsh) will touch your environment. The only thing a child =
can do is spit out some text like the above to stdout, and then your shell =
will "swallow" that text and eval them as shell commands.=20

It can do so either directly with 'eval' (both csh and sh):

     eval `child-spitting-out-text`

or indirectly through a file with 'source' (csh) or '.' (sh):

     child-spitting-out-text > mytmp
     source mytmp ;# CSH case
     . mytmp      ;# SH case

A typical shell-spitting tool will likely have a command-line switch to cho=
ose for which family to generate output:=20

     child-spitting-out-text -sh
     child-spitting-out-text -csh

Lastly, note that the sh idiom 'var1=3Dvalue1' does set an internal shell v=
ariable, but does not automatically "export" it to the environment of later=
 children of the shell. It may or not matter to you, depending on the way t=
hose vars are used thereafter. Anyway, to force the export, either issue th=
e command "set -a" once before issuing the "." command, or make sure that t=
he lines look like

     var1=3Dvalue1 ; export var1

or (for modern shells like Bash, but not for true old Bourne shell)

     export var1=3Dvalue1

Bottom line: as you can see, Tcl's [source] does not play any role in all t=
his. Again, the Tcl child's only job is to output the text.

HTH,

-Alex
0
Alexandre
7/31/2014 5:35:32 PM
On Thursday, July 31, 2014 8:35:32 PM UTC+3, Alexandre Ferrieux wrote:
> On Thursday, July 31, 2014 1:22:19 PM UTC+2, Guy wrote:
>=20
> > I have a tcl script that generates sh command file that looks like this=
:
>=20
> >=20
>=20
> > setenv var1 value1
>=20
> > setenv var2 value2
>=20
> >=20
>=20
> > i would like to source so all the variables will be set to the terminal=
 window.
>=20
>=20
>=20
> Note that the above syntax is rather that of the "other" shell family: cs=
h, not sh.
>=20
>=20
>=20
>   SH-Family:   var1=3Dvalue1
>=20
>  =20
>=20
>   CSH-Family:  setenv var1 value1
>=20
>=20
>=20
> Anyway, whatever the shell, the principle to keep in mind is that no chil=
d process (like a tclsh) will touch your environment. The only thing a chil=
d can do is spit out some text like the above to stdout, and then your shel=
l will "swallow" that text and eval them as shell commands.=20
>=20
>=20
>=20
> It can do so either directly with 'eval' (both csh and sh):
>=20
>=20
>=20
>      eval `child-spitting-out-text`
>=20
>=20
>=20
> or indirectly through a file with 'source' (csh) or '.' (sh):
>=20
>=20
>=20
>      child-spitting-out-text > mytmp
>=20
>      source mytmp ;# CSH case
>=20
>      . mytmp      ;# SH case
>=20
>=20
>=20
> A typical shell-spitting tool will likely have a command-line switch to c=
hoose for which family to generate output:=20
>=20
>=20
>=20
>      child-spitting-out-text -sh
>=20
>      child-spitting-out-text -csh
>=20
>=20
>=20
> Lastly, note that the sh idiom 'var1=3Dvalue1' does set an internal shell=
 variable, but does not automatically "export" it to the environment of lat=
er children of the shell. It may or not matter to you, depending on the way=
 those vars are used thereafter. Anyway, to force the export, either issue =
the command "set -a" once before issuing the "." command, or make sure that=
 the lines look like
>=20
>=20
>=20
>      var1=3Dvalue1 ; export var1
>=20
>=20
>=20
> or (for modern shells like Bash, but not for true old Bourne shell)
>=20
>=20
>=20
>      export var1=3Dvalue1
>=20
>=20
>=20
> Bottom line: as you can see, Tcl's [source] does not play any role in all=
 this. Again, the Tcl child's only job is to output the text.
>=20
>=20
>=20
> HTH,
>=20
>=20
>=20
> -Alex

Thanks Alex.

i think i didnt understand exactly what to do.

lets say that my tcl script generates a file called "my_csh_command" that l=
ooks like this:
setenv aaa val_a
setenv bbb val_b

i can modify the tcl script so it will export:
puts "setenv aaa val_a"
puts "setenv bbb val_b"

i would like that when my tcl script is done these variables will be set in=
 the terminal (which is tcsh).


Thanks,
G

0
Guy
8/5/2014 11:38:05 AM
On Tuesday, August 5, 2014 1:38:05 PM UTC+2, Guy wrote:
> 
> lets say that my tcl script generates a file called "my_csh_command" that looks like this:
> 
> setenv aaa val_a
> setenv bbb val_b
> 
> i would like that when my tcl script is done these variables will be set in the terminal (which is tcsh).

Again, there is no way for a child process (the tclsh running your script) to affect the parent's (the shell) environment. You must end up doing:
 
    tclsh foo.tcl > my_csh_command
    source my_csh_command

for the shell's environment to be properly updated.
But you can use a shell alias to automate the two lines above:

    alias update_my_env "tclsh foo.tcl > my_csh_command;source my_csh_command"

-Alex
0
Alexandre
8/5/2014 12:58:07 PM
Reply: