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

Can I call procedure(s) from commandline?

824 views
Skip to first unread message

Juge

unread,
Apr 16, 2015, 10:58:20 PM4/16/15
to
Assuming I have a tcl program like
#!/usr/local/bin/tclsh
proc HelloWorld {
puts "Hello World"
}
proc GoodbyeWorld {
puts "Goodbye cruelWorld"
}

And I want to call separate procedures from linux command shell. Like ,/program.tcl HelloWorld
I know it would work with switch and using args but is there a direct way?

heinrichmartin

unread,
Apr 17, 2015, 3:23:57 AM4/17/15
to
1. other interpreters might have a -c command line option, e.g. expect.
2. OR add {*}$argv to the end of the script.
3. proc requires 3 arguments...


$ cat > test.tcl
#!/usr/bin/env tclsh
proc HelloWorld {} {
puts "Hello World"
}
proc GoodbyeWorld {} {
puts "Goodbye cruelWorld"
}
{*}$argv
$ chmod +x test.tcl
$ ./test.tcl
$ ./test.tcl HelloWorld
Hello World
$ ./test.tcl GoodbyeWorld
Goodbye cruelWorld
$ ./test.tcl Oops
invalid command name "Oops"
while executing
"{*}$argv"
(file "./test.tcl" line 8)

HTH,
Martin

heinrichmartin

unread,
Apr 17, 2015, 3:29:30 AM4/17/15
to
I did not mention:

proc Say {args} {
puts [join $args]
}

$ ./test.tcl Say oops!
oops!
$ ./test.tcl Say Hello World
Hello World

:-)

Arjen Markus

unread,
Apr 17, 2015, 3:31:20 AM4/17/15
to
Op vrijdag 17 april 2015 09:23:57 UTC+2 schreef heinrichmartin:
Or make a dedicated script that sources the one containing the procedures:

#!/usr/bin/env tclsh
source [lindex $argv 0]
{*}[lrange $argv 1 end]

That makes invoking it a trifle more complicated, but it also means you do not have to add the {*}$argv to each and every one source file. Hm, come to think of it, you might create a small shell script to take care of the "boiler plate" code, something along these lines (untested)

#/bin/sh
tclsh <<EOF
source $1
{*}\[lrange $argv 1 end]
EOF

Regards,

Arjen


Rich

unread,
Apr 17, 2015, 6:07:27 AM4/17/15
to
Juge <jyrki.m...@gmail.com> wrote:
> Assuming I have a tcl program like
> #!/usr/local/bin/tclsh
> proc HelloWorld {
> puts "Hello World"
> }
> proc GoodbyeWorld {
> puts "Goodbye cruelWorld"
> }

Did you type the above, or copy/paste? Because both the proc
definitions above are invalid. You omitted the parameter that defines
the proc's arguments. You need at least a {} (or "") after the name of
the proc above for these to be valid.

> And I want to call separate procedures from linux command shell. Like
> ,/program.tcl HelloWorld I know it would work with switch and using
> args but is there a direct way?

Yes, you just need one more line, provided you fix your syntax errors
above (note, however, if you call a proc that does not exist you'll get
a standard Tcl command not found error message):

Add this as the last line of your script above:

[lindex $argv 0]

Also, the above does not pass any parameters from the command line to
your procs (although your procs presumably take none, so not passing
any is reasonable here).


0 new messages