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

tclsh exits after running the script

33 views
Skip to first unread message

physio...@gmail.com

unread,
Oct 16, 2006, 10:21:06 AM10/16/06
to
Hi,

I have written a smaller shell wrapper script to run small tcl
application. What I want is the control whether the shell quits after
script completion or not. See the script below

------------------------------------------------------------------------------------
script1
=====
#!/bin/sh

# This is a comment to tclsh \
exec tclsh $0 "$@"
package require SpefParser 1.0
namespace import -force ::sto::* ::Runtime::*
set s [new SParser]
$s readS abc.s
------------------------------------------------------------------------------------

Now, what happens, is as soon as the parser completes the tclsh quits.
I also tried using the tcl_interactive variable but it did not help.

Also, if I try 'tclsh script2', it does not work.
------------------------------------------------------------------------------------
script2
=====
package require SpefParser 1.0
namespace import -force ::sto::* ::Runtime::*
set s [new SParser]
$s readS abc.s
------------------------------------------------------------------------------------

Can someone provide a way for doing the same?

Thanks,
Madhur

Bryan Oakley

unread,
Oct 16, 2006, 9:04:44 PM10/16/06
to
physio...@gmail.com wrote:
> Hi,
>
> I have written a smaller shell wrapper script to run small tcl
> application. What I want is the control whether the shell quits after
> script completion or not. See the script below

So, you just want the script to sit there doing nothing?

add something like "vwait forever" as the last line of your script. This
causes the program to enter the event loop. Though, with no events to
process I don't quite see the point.

physio...@gmail.com

unread,
Oct 17, 2006, 12:34:43 AM10/17/06
to
Ohh it seems I did not mention the reason why I want to do so. Once I
have loaded the database in memory I want to work interactively with it
inside the tclsh. Its like an interactive program whenever required.

I hope the reason is clear this time.

with vwait forever, I don't get the shell prompt but the script hangs.
Didn't help. Looks like this is doable only with a C code.

Regards,
Madhur

Stephan Kuhagen

unread,
Oct 17, 2006, 2:45:31 AM10/17/06
to
physio...@gmail.com wrote:

> Hi,
>
> I have written a smaller shell wrapper script to run small tcl
> application. What I want is the control whether the shell quits after
> script completion or not. See the script below
>
>
------------------------------------------------------------------------------------
> script1
> =====
> #!/bin/sh
>
> # This is a comment to tclsh \
> exec tclsh $0 "$@"
> package require SpefParser 1.0
> namespace import -force ::sto::* ::Runtime::*
> set s [new SParser]
> $s readS abc.s

This is a little shell-scripting-hacking, but try this:

Put the following script into your ~/.tclshrc (which is alway executed, when
you start an interactive tcl-shell, see man tclsh):

-------
if {[info exists env(TCL_INIT_FILE)] && [info exists env(TCL_INIT_ARGS)]} {
if {[file exists $env(TCL_INIT_FILE)]} {
source "$env(TCL_INIT_FILE)"
}
catch {tclsh_init_proc "$env(TCL_INIT_ARGS)"}
}
-------

Now, if you have a Tcl script running, and the environment vars
TCL_INIT_FILE and TCL_INIT_ARGS are set, then the Tcl-initialization will
source that file, and call a proc named tclsh_init_proc afterwards, which
should contain all your initialization stuff. The args to this proc are the
args to your script. Your wrapper script mentioned above should then look
like this:

-------
#!/bin/sh
#\
export TCL_INIT_FILE="$0"
#\
export TCL_INIT_ARGS="$@"
#\
exec tclsh

proc tclsh_init_proc {args} {
puts "Do your init-stuff here"
puts "my args are: $args"
set ::example_var "Try to reach this var from the interactive shell"
}
-------

When you execute this script, it will be first sourced by the tclsh-init,
defining tclsh_init_proc, then calling tclsh_init_proc and then starting up
the tclsh in interactive mode with all settings done in tclsh_init_proc
known to the interpreter. Try "puts $example_var" in the prompt...

Regards
Stephan


Donal K. Fellows

unread,
Oct 17, 2006, 4:37:43 AM10/17/06
to
physio...@gmail.com wrote:
> I have written a smaller shell wrapper script to run small tcl
> application. What I want is the control whether the shell quits after
> script completion or not.
[...]

> Now, what happens, is as soon as the parser completes the tclsh quits.
> I also tried using the tcl_interactive variable but it did not help.

You're on Unix and have the TclX package installed as well as Tcl? If
so, the answer to your question is TclX's [commandloop] command. It does
exactly what I believe you're looking for.

Donal.

suchenwi

unread,
Oct 17, 2006, 4:54:21 AM10/17/06
to

Donal K. Fellows schrieb:

> You're on Unix and have the TclX package installed as well as Tcl? If
> so, the answer to your question is TclX's [commandloop] command. It does
> exactly what I believe you're looking for.

What you can always do if you want an interactive Tcl prompt after your
script has been sourced: add, in the end of your script, the following
lines:
while 1 {
puts -nonewline "% "; flush stdout
gets stdin line
catch $line res
puts $res
}
To exit this "infinite" loop, obviously type "exit" at the % prompt...
:^)

Ivan Young

unread,
Oct 17, 2006, 5:51:40 AM10/17/06
to
physio...@gmail.com wrote:
> Ohh it seems I did not mention the reason why I want to do so. Once I
> have loaded the database in memory I want to work interactively with it
> inside the tclsh. Its like an interactive program whenever required.
>
> I hope the reason is clear this time.
>
> with vwait forever, I don't get the shell prompt but the script hangs.
> Didn't help. Looks like this is doable only with a C code.
>
> Regards,
> Madhur
Hi,

I find the stdin package useful, for optional interactive debug sessions
with programs that normally just enter vwait. So the final few lines of code
look something like this.


# Enter Event loop
# If the command line option -debug 1 is used
# startup in interactive mode.
if {$Config(debug) } {
package require stdin
Stdin_Start "$taskname % "
} else {
vwait appconf(wait)
}


I think the stdin package comes with the tclhttp server, thanks to
Stephen Uhler.
Its short, so here is the code anyway:

# stdin.tcl
#
# (c) 1997 Sun Microsystems Laboratories (Stephen Uhler)

# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# SCCS: @(#) stdin.tcl 1.6 97/08/21 15:35:14

# The following is an event-driven command loop for testing with tclsh
# with command history:
# !<pattern> run 1st command matching pattern
# !<pattern>/xxx/yyy/ as above, but substitute yyy for xxx

package provide stdin 1.1

set Stdin(maxHistory) 100
append Stdin(history) {}
proc StdinRead {prompt} {
global Stdin
if [eof stdin] {
set Stdin(Wait) 1
fileevent stdin readable {}
return
}
append Stdin(command) [gets stdin]
if {[info complete $Stdin(command)]} {
set s1 ""; set s2 ""
if {[regexp {^!([^/]+)(/([^/]+)/([^/]*))?} $Stdin(command) \
{} history {} s1 s2]} {
set match [lsearch -regexp $Stdin(history) "^ *$history"]
if {$match >= 0} {
set Stdin(command) [lindex $Stdin(history) $match]
if {$s1 != ""} {
catch {regsub -- $s1 $Stdin(command) $s2 Stdin(command)}
}
puts stderr $Stdin(command)
} else {
puts -nonewline "No \"$history\" in history list\n$prompt"
set Stdin(command) ""
flush stdout
return
}
}
if {$Stdin(command) != "" && \
$Stdin(command) != [lindex $Stdin(history) 0]} {
set Stdin(history) [lrange "[list $Stdin(command)] \
$Stdin(history)" 0 $Stdin(maxHistory)]
}
catch {uplevel #0 $Stdin(command)} result
puts -nonewline "$result\n$prompt"
flush stdout
set Stdin(command) ""
} else {
append Stdin(command) \n
}
}

proc Stdin_Start {{prompt "% "}} {
global Stdin
set Stdin(command) ""
puts -nonewline $prompt
flush stdout
fileevent stdin readable [list StdinRead $prompt]
vwait Stdin(wait)
}

proc history {{count 100}} {
global Stdin
puts stderr [join [lrange $Stdin(history) 0 $count] \n]
}

if {[info procs bgerror] == ""} {
proc bgerror {args} {
global errorInfo
Log {} bgerror $errorInfo ; Log_Flush
puts stderr $errorInfo
}
}

---------
Hope that is of some help.
Ivan.

physio...@gmail.com

unread,
Oct 17, 2006, 6:47:55 AM10/17/06
to
Thanks for the help guys !!!!. Since I did not have Tclx on my Unix
systems, I was looking for a solution which is based on native Tcl.
When I searched for [commandloop] on wiki I found a native TCL solution
developed by a tcl expert. It solved my problem, thanks for the help.

Wiki link: http://wiki.tcl.tk/1968

Cameron Laird

unread,
Oct 17, 2006, 10:00:51 AM10/17/06
to
In article <1161059683.5...@m7g2000cwm.googlegroups.com>,

<physio...@gmail.com> wrote:
>Ohh it seems I did not mention the reason why I want to do so. Once I
>have loaded the database in memory I want to work interactively with it
>inside the tclsh. Its like an interactive program whenever required.
>
>I hope the reason is clear this time.
>
>with vwait forever, I don't get the shell prompt but the script hangs.
>Didn't help. Looks like this is doable only with a C code.
.
.
.
Yes and no. <URL: http://wiki.tcl.tk/1968 > will interest you.
0 new messages