It is an omission, but a simple workaround is to do something like:
echo 'foreach x {a b c} {puts $x}' | tclsh
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 :)
> 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
> 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
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!
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
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.