tags:

views:

597

answers:

5

Hi all, 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?

+1  A: 

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>
Carlos Tasada
See the edit to the question. I have to run the command "source <script> <arg>" as if I were calling it from c-shell.
Amir Rachum
Hi Amir,exec is always going to call an OS executable file. If you need to execute as running from the c-shell or bash or ... you need to use the second option: exec [info nameofexecutable] <script path>If it's not good enought, describe why ;)
Carlos Tasada
I get "couldn't read file "source": no such file or directory"
Amir Rachum
Umm, Amir, what's the result of executing 'info nameofexecutable'?I'm doing this kind of things in my code in a lot of places and it works. Looks like we're having some kind of misunderstanding.
Carlos Tasada
The preceding error was when using 'info nameofexecutable'. I get "couldn't read file "source": no such file or directory".
Amir Rachum
'info nameofexecutable' is supposed to return the name (and path) of your tclshell executable. "couldn't read file" is expected when the file you want to open (or source in this case) is not found.Please try to execute the next script and paste the result, replacing <source script> at the beginning by you real source file.------------set mysource "<source script>"puts "exe: [info nameofexecutable]"puts "Exists source? $mysource -> [file exists $mysource]"source "[info nameofexecutable]" "$mysource"------------
Carlos Tasada
this would run "source <c-shell script> <arg>" with tclsh which is not what I want... Also, where do I put the arguments for the script?
Amir Rachum
A: 

the error speaks for itself. Make sure you give the correct path name, specify full path if necessary. and make sure there is indeed the file exists in that directory

ghostdog74
+4  A: 

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.

Bryan Oakley
+1  A: 

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
Colin Macleod
the second deos assume the script itself has the necessary exec magic at the top to make it executable, otherwise you may need to exec tclsh or wish with it as an argument
jk
Absolutely right thanks, may need: exec tclsh script_path first_arg second_arg
Colin Macleod
+2  A: 

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.

mouviciel