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

script on tclsh command line?

26 views
Skip to first unread message

bill...@alum.mit.edu

unread,
Jan 26, 2007, 1:29:09 AM1/26/07
to
I am wondering whether it has ever been considered adding an option to
tclsh to allow short
scripts to be supplied on the command line and whether this would cause
a problem that does
not occur to me. This came up when I created a library and wanted to
have the Makefile
add the library to the package index. The only way to do this as far as
I know is to
put the pkg_mkIndex command into a file and have make feed the file to
tclsh. While this isn't
all that difficult, it is a bit cumbersome and adds one more thing that
can go wrong (e.g.
if the script file goes astray).

Colin Macleod

unread,
Jan 26, 2007, 3:05:49 AM1/26/07
to
On 26 Jan, 06:29, billpo...@alum.mit.edu wrote:
> I am wondering whether it has ever been considered adding an option to
> tclsh to allow short
> scripts to be supplied on the command line

It is an omission, but a simple workaround is to do something like:
echo 'foreach x {a b c} {puts $x}' | tclsh

suchenwi

unread,
Jan 26, 2007, 3:40:15 AM1/26/07
to

On 26 Jan., 09:05, "Colin Macleod" <colin.macl...@tesco.net> wrote:
> It is an omission, but a simple workaround is to do something like:
> echo 'foreach x {a b c} {puts $x}' | tclsh

I'm not sure whether it's an omission, or a design decision - "argv
belongs to the Tcl script" (wish is different there). The `echo`
solution can also be sugared a little bit (this is in Cygwin bash), if
you need it more than once:

$ tcl() { echo $* | tclsh; }
$ tcl 'puts "hello world"'
hello world
$ tcl 'puts [expr sqrt(2)]'
1.41421356237

Make sure to single-quote the script, as bash has some different ideas
of syntax :)

Stephan Kuhagen

unread,
Jan 26, 2007, 3:50:47 AM1/26/07
to
suchenwi wrote:

> solution can also be sugared a little bit (this is in Cygwin bash), if
> you need it more than once:
>
> $ tcl() { echo $* | tclsh; }
> $ tcl 'puts "hello world"'
> hello world
> $ tcl 'puts [expr sqrt(2)]'
> 1.41421356237

I wrote the following for the same purpose as a bash script, allowing to
give it more than one script-snipped to eval:

---
#!/bin/sh
#\
exec tclsh "$0" "$@"

foreach arg $argv {
catch {eval $arg} result
if {$result != ""} {
puts $result
}
}
---

I think, this is one of my most useful bash-scripts, always have a
Tcl-snipped-evaluator at hand:

$ tcl 'foreach number {1 2 3} { puts [expr sin($number)] }' 'puts "Hello
Bash from Tcl!"'
0.841470984808
0.909297426826
0.14112000806
Hello Bash from Tcl!
$

Love that...
Regards
Stephan

suchenwi

unread,
Jan 26, 2007, 4:43:16 AM1/26/07
to
Does the more modern invocation voodoo work for you?
#!/usr/bin/env tclsh

Stephan Kuhagen

unread,
Jan 26, 2007, 4:50:14 AM1/26/07
to
suchenwi wrote:

> Does the more modern invocation voodoo work for you?
> #!/usr/bin/env tclsh

Yes, of course. But for two reasons I prefer the other one:
1. I like it, because I'm used to it... (strong reason, I know...) ;-)
2. Sometimes there are systems, that have env not in /usr/bin or they do not
have it at all... /bin/sh can't fail, it's on every Unix-like OS.

Regards
Stephan

Cameron Laird

unread,
Jan 26, 2007, 9:49:24 AM1/26/07
to
In article <1169798749.3...@v45g2000cwv.googlegroups.com>,

As a supplement to all the advice Colin and Richard provide,
let me observe that it is possible to use the Expect executable
to achieve the result. I illustrate with an example

expect -c 'set a [expr 2 + 7.5]; puts XXX$a'

Note that Expect can be regarded as a fully functional Tcl
interpreter--because it is!

Glenn Jackman

unread,
Jan 28, 2007, 8:30:14 AM1/28/07
to

This got me thinking, and lead to http://wiki.tcl.tk/17599

tcl -e 'foreach x {a b c} {puts $x}'

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

bill...@alum.mit.edu

unread,
Jan 28, 2007, 6:35:28 PM1/28/07
to

On Jan 28, 5:30 am, Glenn Jackman <gle...@ncf.ca> wrote:
>http://wiki.tcl.tk/17599
>
> tcl -e 'foreach x {a b c} {puts $x}'

Yes, that's what I wanted. But, with reference to the wiki article
headline, lest I be associated with the writers of unreadable code and
users of unspeakable tricks, it wasn't Perl that I had in mind but
Awk.

0 new messages