Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using "cd" in Tcl Script?

2,237 views
Skip to first unread message

Andreas Schwind

unread,
Mar 7, 1996, 3:00:00 AM3/7/96
to
Hi,

: Hi Andreas, you don't have to use 'exec cd' for Tcl has it's own
: built in 'cd'.

: You just have to do a 'cd /home1'
This won't do it, because my script don't know if the command it has to
execute is 'cd' or anything else.
The user could define his own commands with parameters like

tar cvf #DIR #TARGET

Later he has to substitute #DIR and #TARGET into correct arguments for the
tar command. So far, so good and my script is doing well.
But if the user is defining something like

cd /home1/foo; tar cvf #DIR #TARGET
it wouldn't executed correctly.

In my Script the commandstring which should be executed is stored in a
variable "cmd" and first i execute it using
eval exec $cmd > Output.tmp

Because this don't works correct i use now
set res [eval exec "/bin/csh -c \"$cmd\""]
set fileID [open Output.tmp a+]
puts $fileID $res
close $fileID

As far as i tested it this works fine, but i'm not really happy with this
solution. So, does anybody know a better way to do it?

Thx for any help.

Bye
--
_______________________________________________________________________
| eMail: s_sc...@ira.uka.de oder
Andreas Schwind | un...@rzstud1.rz.uni-karlsruhe.de
| WWW: http://www.uni-karlsruhe.de/~un4w


John Haxby

unread,
Mar 8, 1996, 3:00:00 AM3/8/96
to
s_sc...@ira.uka.de (Andreas Schwind) wrote:
>Hello,
>
>I have to do something like "cd /home1" in a Tcl Script.
>I tried it with: eval exec cd /home1
>but i've got an error message like
>"couldn't execute "cd": No such file or directory".
>Then i tried it with quoting the command like eval [exec cd /home1],
>but it wouldn't work.

You're trying too hard -- "cd" is (necessarily) a built-in command,
just do

cd /home1

--
John Haxby
These are my opinions, not my employer's.


John Haxby

unread,
Mar 11, 1996, 3:00:00 AM3/11/96
to
s_sc...@ira.uka.de (Andreas Schwind) wrote:
>Hi,
>
>: Hi Andreas, you don't have to use 'exec cd' for Tcl has it's own
>: built in 'cd'.
>
>: You just have to do a 'cd /home1'
>This won't do it, because my script don't know if the command it has to
>execute is 'cd' or anything else.
[snip]

You *have* to make a special case of "cd". Suppose you do this:

exec sh -c {cd /home1}

that'll work in the process will complete correctly, but it won't change
the current directory. The current directory is a property of the process,
you can't ask another process to change your directory (well, other than
by editing /dev/mem :-), a process has to change its own directory, that's
why "cd" is necessarily a built-in for anything that allows changing directory
(shells, Tcl, emacs, etc).

Of course, this mostly applies to UNIX, DOS has a different model for the
current working directory.

0 new messages