Re: Pipe command output to listbox

From: Kevin Walzer (sw_at_wordtech-software.com)
Date: 09/06/04


Date: Mon, 06 Sep 2004 17:52:10 -0400


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Cameron,

You're right, it seems I'm trying to bite off too much here. I'm still
learning Tcl/Tk, coming to it from AppleScript and basic shell
scripting, and trying to string together processes and variables and
pipe the output correctly into a listbox may be too complex right now.
Perhaps I need to learn the building blocks better.

So I'm backing up, and still having problems.

I've put together a simple script in Tcl to run another command-line
process, write its output to a text file, and open the file:

#! /usr/bin/tclsh

cd ~/Desktop
exec {which port | port list all > foo.txt}
ls -a
exec {open foo.txt}

This script changes the working directory to my desktop, runs
DarwinPort's port list all command (lists all available ports), writes
the output to foo.txt, lists all files in the directory, and opens
foo.txt. (I'm working on a Mac; "open" is an old NextStep command that
launches the default program for a particular file extension, in this
case Text Editor.)

Running this script from the command line, I get an error message:

Kevin-Walzers-Computer:~ kevin$ tclsh ~/Desktop/pipe1.tcl
couldn't execute "which port | port list all > foo.txt": no such file
or directory
~ while executing
"exec {which port | port list all > foo.txt}"
~ (file "/Users/kevin/Desktop/pipe1.tcl" line 4)

What makes me think I'm losing my mind is that when I each command
interactively in tclsh, they execute fine. I cd'd to the desktop. I
located the port command via which, then ran port list all and wrote the
input to foo.txt. Then I opened foo.txt in Text Editor.

So...

I'm lost. I'm not a complete beginner to scripting, and I think I'm
executing exec correctly. The port command is on my path. So I'm
completely baffled by this error message.

Thanks for any help you can give!

Kevin

Cameron Laird wrote:

| In article <opsdwbgldkiu73r8@eflinux>, Lars E. <ja@til.sj> wrote:
|
|>On Sun, 05 Sep 2004 19:47:29 -0400, Kevin Walzer
|><sw@wordtech-software.com> wrote:
|
| .
| .
| .
|
|>>I'm trying to write a script where I run a command ("port list all,"
|>>which lists all installed DarwinPorts packages) and pipe it to a Tk
|>>listbox.
|>>
|>>Here's my code:
|>>
|>>#define the procedure
|>>
|>>Proc ListPackages { packages } {
|>>
|>>~ set packages [catch { open "| port list all |& cat "}]
|>>~ $packages insert
|>>}
|>>
|>>
|>>#menu button code
|>>
|>>button .button.listall -border 0 -compound top -image callist16 -text
|>>All -font {-size 10} -command ListPackages -relief flat -overrelief
|>>raised
|>>
|>>
|>># listbox code:
|>>
|>>set f [frame .pane.top]
|>>listbox $f.list -listvariable packages -yscrollcommand "$f.scr set"
|>>
|>>#error message
|>>
|>>wrong # args: should be "ListPackages packages"
|>>~ while executing
|>>"ListPackages"
|>>~ invoked from within
|>>".button.listall invoke"
|>>~ ("uplevel" body line 1)
|>>~ invoked from within
|>>"uplevel #0 [list $w invoke]"
|>>~ (procedure "tk::ButtonUp" line 23)
|>>~ invoked from within
|>>"tk::ButtonUp .button.listall"
|>>~ (command bound to event)
|>
|>Kevin,
|>
|>The error message complains that the number of arguments to ListPackages
|>is wrong.
|>Listpackages expects one argument.
|>Your button's command option calls ListPackages without any arguments.
|
| .
| .
| .
| Indeed.
|
| The problems are deeper than that, moreover. I don't understand
| what you intend ListPackages to do. I recommend you start with
| simpler experiments; perhaps
| if [catch {exec port list all} result] {
| # Perhaps you'll want to edit error messages somewhat.
| }
| puts "The result of the 'port' command is '$result'."
| will be instructive. Eventually
| $f.listbox insert end $result
| might be helpful.

- --
Kevin Walzer, PhD
WordTech Software--Open Source Applications and Packages for OS X
http://www.wordtech-software.com
http://www.smallbizmac.com
http://www.kevin-walzer.com
mailto:sw@wordtech-software.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFBPNwJF6m9qPmThLQRAkMbAJ9GUiYp8QCsJiwj+VtTrjAEGaJK8gCfQyF1
jyrbL8U90+1cP0jtoYX0TD8=
=+poj
-----END PGP SIGNATURE-----