"exec source <script>" does not work in tcl

11,968

Solution 1

source in csh, like . in bash, executes a script without spawning a new process.

The effect is that any variable that is set in that script is available in current csh session.

Actually, source is a built-in command of csh, thus not available from tcl exec, and using exec without source would not give the specific source effect.

There is no simple way to solve your problem.

Solution 2

If the script you were given is a cshell script, you can exec it like this:

exec /bin/csh $path_to_script

In effect, this is what the 'source' command does from within an interactive shell. It's not clear whether this is really what you want to do or not (not exactly, but close enough for this discussion).

The reason you can't exec the source command is that exec will only work on executable files (hence the name 'exec'). The source command isn't implemented as an exectuable file, it is a command built-in to the shell. Thus, it can't be exec'd.

If you really feel the need to exec the source command or any other built-in command you can do something like this:

exec /bin/csh -c "source $path_to_script"

In the above example you are execing the c shell, and asking it to run the command "source ". For the specific case of the source command, this doesn't really make much sense.

However, I'm not sure any of this will really do what you expect. Usually if someone says "here's some commands, just do 'source ', it usually just defines some aliases and whatnot to be used from within an interactive shell. Those aliases won't work from within Tcl.

Solution 3

source load the source file

you should do:

source <script path>

If you want to execute it, then you need to call the main proc.

another option would be to do:

exec [info nameofexecutable] <scritp path>

Solution 4

Some confusion here. exec runs a separate program, possibly with arguments. source is not a separate program, it is another Tcl command which reads a file of Tcl commands and executes them, but does not pass arguments. If the other script you are trying to call is written to be run on from the command line, it will expect to find its arguments as a list in variable argv. You can fake this by setting argv to the list of arguments before running source, eg.

set argv {first_arg second_arg}
source script_path

Alternatively you could use exec to start a whole separate Tcl executable and pass it the script and arguments:

exec script_path first_arg second_arg
Share:
11,968
Amir Rachum
Author by

Amir Rachum

Programmer, Board Gamer, Geek. Blog, Resume and Projects: http://amir.rachum.com/blog Twitter: @AmirRachum

Updated on June 14, 2022

Comments

  • Amir Rachum
    Amir Rachum almost 2 years

    I'm trying to call a script in Tcl with the command:

    exec source <script path>
    

    and I get the error

    couldn't execute "source": no such file or directory
    

    How can I call another script from tcl?

    Edit: I am running a command I got from another person in my office. I was instructed to run "source " explicitly with source. So in other words, how would I run any command that would work in cshell, in Tcl?