**DONOTDELETE**
(Lurker)
2001-08-16 10:35 AM
PIPE Command output to an Array ?

I dont know whether this has been discussed before...

Often I want to postprocess some output from a Shell command - Ok, pipe it to a temp file and read it in - thats fine, works.

But what about an external command (KiXpipe say) that could make that output stream available inside a script, this is what I am getting at...

code:
 
SHELL "IPCONFIG /ALL |KIXPIPE arrayname. "
? arrayname.0+" = size of returned array"
? arrayname.1+" = first line"
? arrayname.2+" = and so on"


I just think this would make some of the thorny command output trapping exercises more reliable and elegant.

If I have missed this in the 2001 manual - just shoot me now.

Mark

If this is regarded as a good idea, I cant take credit - REXX does this on NT - its called RXQUEUE or something.

[ 16 August 2001: Message edited by: Mark Antrobus ]

ShawnAdministrator
(KiX Supporter)
2001-08-16 03:14 PM
Re: PIPE Command output to an Array ?

Mark,

I'll second your idea - in fact - I think this is one of the best and most usefull KiXtart suggestions I've ever heard ...

Just imagine - not more kludgy file piping and re-reading ...

-Shawn

MCA
(KiX Supporter)
2001-10-13 02:43 AM
Re: PIPE Command output to an Array ?

Dear,

We agree with Shawn. It is a great idea, but we think that it can cost
a lot of time for implementation.
Reasons:

  • how is it implemented by REXX? We think that the file will be read
    by an internal REXX command and it will read from memory or possible
    an unknown temporary file.
    Reading such information from memory needs special functions. Reading
    from a file is must easier.
  • how much kixpipe handle incorrect lines. f.e. you only get
    lines with a LF instead of CR+LF.

Greetings.


Les
(KiX Master)
2001-10-13 06:43 PM
Re: PIPE Command output to an Array ?

Very interesting... Instead of an external program however, it should be handled internally.

Something like:
$RC = PIPE($MyArray[stdout], SHELL "IPCONFIG /ALL")

Returns:
0 if nothing to pipe
number of elements piped

It would be nice to not be encumbered with file I/O and all the overhead (create, open, read, error check, close, delete) associated with it.

Also, some external commands could be run without %comspec% as it would not be needed for the usual > or | operations.

ShawnAdministrator
(KiX Supporter)
2001-10-13 07:01 PM
Re: PIPE Command output to an Array ?

Les,

Yeah, that's an even better idea ... but while we're waiting for this to be embedded in KiXtart, why can't "someone" write a PIPE() UDF to do exactly this (just like you laid out ... totally doable ... you busy today ? Kinda crappy weather anyway ...

-Shawn

Les
(KiX Master)
2001-10-13 07:20 PM
Re: PIPE Command output to an Array ?

Shawn,
Working for "HoneyDo Construction" this weekend and stealing CPU time right now. Anyhow, I'm no programmer, just an idea man (hair-brained).

It could probably be done with current version but may cost a pound to save a penny, performance wise.

Will watch this thread when I can to see what you may brew up.

ShawnAdministrator
(KiX Supporter)
2001-10-13 07:32 PM
Re: PIPE Command output to an Array ?

Les,

Ahhh yes - you work for the same company that Doc, Jochen and I work for ... Know exactly what you mean my friend ... It's a sad sight when Shawn sneaks quietly back into the house, crawls down the basement stairs (on his belly), and silently tippy-taps on keyboard, checking-in on kixtart.org ...

I'll see what I can home-brew later on ... of course ... it would still involve piping stuff out to a temporary file ... but it would be nice to have it packaged in a single UDF - no ? In fact, if memory serves, there's a tempfile() UDF over at scriptlogic that might fit in quite nicely here ...

l8r

-Shawn

ShawnAdministrator
(KiX Supporter)
2001-10-15 02:55 AM
Re: PIPE Command output to an Array ?

Hmmm.... nothing like having a nice dinner, then sitting down and doing a little kixtart koding, eh Les ? Don't know about you, but I never get tired of Turkey leftovers ... burrrppp ... excuse me ...

Anyway .... here'a a rather hastily written, over-wraught, tortured start to this endeavor ... the UDF is called PIPE() and it goes like this ...

$output = pipe("net user")

for each $line in output
?"output"
next

code:

break on


$output = pipe("net user")


for each $line in $output
?"$line"
next


exit


function pipe($command)
;
; function: pipe() - Submits a command to %comspec% and pipes output to internal array
;
; example:
;
; $array = pipe("ddd")
;
; remarks:
;
; Do not specify any redirection of stdout or stderr in $command string
; Blank (null) lines are not included in output ( i think )
;
dim $i,$fn,$chunk
dim $array[9] ; init dim of working array
$chunk = ubound($array) ; size of realloc chunk
$fn = "%temp%\pipe.tmp" ; temp file for output
shell '%comspec% /c $command >"$fn" 2>nul'
if open(1,"$fn") = 0 ; open temp file
$i=0 ; used to index entire array
$line = readline(1) ; get a line
while not @error ; while not EOF
if $line ; skip whitespace
$array[$i] = $line ; stuff it
if $i = ubound($array) ; time to grow the array ?
redim preserve $array[$i+$chunk] ; yup - grow arraysize by $chunk
endif
$i=$i+1 ; inc index count
endif
$line = readline(1) ; get next line
loop
$=close(1) ; close 'er up
redim preserve $array[$i] ; trim back array to actual size
$pipe = $array ; pass it back to caller
endif
endfunction


-Shawn

[ 15 October 2001: Message edited by: Shawn ]

Les
(KiX Master)
2001-10-15 08:44 PM
Re: PIPE Command output to an Array ?

Shawn,
Code runs off your fingers like words from the lips of a poet in love!

I guess, if you want to use the $array, you need to use GLOBAL instead of DIM.

And what's this REDIM stuff my friend... I thought you said you weren't a big user of it. Looks like you have a firm handle on it. Will you be adding it to your
K2K1/UDF - Anyone want to play with 2D & 3D arrays ? UDF?

Bryce
(KiX Supporter)
2001-10-15 09:07 PM
Re: PIPE Command output to an Array ?

cool!

No need to GLOBAL $array, because the PIPE() UDF returns the value of $array to the caller.

$return = pipe("dir c:\*.exe")

Bryce

Les
(KiX Master)
2001-10-15 09:28 PM
Re: PIPE Command output to an Array ?

Sorry Bryce, perhaps my novice programming is showing... If I add the following to the script, it errors.
code:

break on

$output = pipe("net user")

for each $line in $output
?"$line"
next

For $X = 0 To ubound($array)
? $array[$X]
Next
get $

function pipe($command)
...


But, if I change DIM $array[9] to GLOBAL $array[9], it works.

ShawnAdministrator
(KiX Supporter)
2001-10-15 09:32 PM
Re: PIPE Command output to an Array ?

There was a young man from Nantucket
Who always redirected shell to Bit Bucket
He said "PIPE() is quite funky"
"But redimming can be chunky"
"I'm going back to flatfiles, so fu*k it."

-Shawn

Les
(KiX Master)
2001-10-15 09:39 PM
Re: PIPE Command output to an Array ?

Yep, yep, yep... It's my amateur programming showing through... Did not recognize that $output was an array for lack of []. So this works:
code:

For $X = 0 To ubound($output)
? $output[$X]
Next

Shawn,
Stick to programming... it's what you do best!

ShawnAdministrator
(KiX Supporter)
2001-10-15 09:52 PM
Re: PIPE Command output to an Array ?

ok

Well - if anything - this UDF gives us a sampler of how tasty a builtin function like PIPE() could be ... very handy me thinx ...

-Shawn

**DONOTDELETE**
(Lurker)
2001-10-18 03:28 PM
Re: PIPE Command output to an Array ?

I didnt realise this thread had woken up again...

Shawn,
Nice UDF - Ill use it...
Nice Doggerel too ...

Les,
I know what you mean - I think it was implemented in REXX as an external command to 'partition' it from the real language.
But integrating it would give the advantage of losing the %comspec% as you say.

MCA,
In Rexx there is a concept of a REXX Datastack - which is shareable from any REXX program. Programs can add items into it in a LIFO or FIFO fashion, This is how programs tend to share large pieces of information between subroutines (in REXX subroutines can be internal and external - the external ones cannot access variables of the parent).
Thus you can have a bunch of Rexx programs running (in parallel) and using the stack as a kind of bus to share info.
Now clearly maintaining this is no trivial deal - but it does mean that its possible to write extrenal programs and such which can very easily share data with REXX programs.

As Shawn commented - it does make housekeeping temp files etc much easier- and makes for elegant coding (as does Shawns UDF).

Its also very fast.

Mark

[ 18 October 2001: Message edited by: Mark Antrobus ]

ShawnAdministrator
(KiX Supporter)
2001-10-18 04:19 PM
Re: PIPE Command output to an Array ?

Mark,

Yeah, it's funny how threads can just re-awaken, then twist and turn in different directions ...

But your talk about the REXX datastack got me to thinking (a dangerous thing) ... I'm wondering if it's possible to use the Windows CLIPBOARD as a data staging area. I think there are reskit-like programs for piping DOS command output to the clipboard. Then, if one could use COM to read this data back into the script ... hmmm .... kinda like this (concept only) :

shell 'cmd /c dir c:\*.exe | clipbrd.exe'
$clipbrd = createobject("?")
for each $line in $clipbrd.buffer
?"$line"
next

Something like that anyway. Thing is, I haven't done any research on this and I'm not too sure if there is a clipboard object ??? Just throwing this out as a concept ... using the clipboard in this fashion would sure be handy ...

-Shawn

JochenAdministrator
(KiX Supporter)
2001-10-18 04:52 PM
Re: PIPE Command output to an Array ?

whoops ...

totally missed this thread !
Shawn - Neaaaat udf !!! Even much more I like your limerick, you're a poet in disguise
This comes closer than anything to a shell->KiX piping solution I saw lately ! Ok, still some file handling , but we getting used to !

Clipboard abuse as container for shell output ??? would be really a workaround ... But I apprehend that would be not that simple

Jochen

btw ... what is REXX ???

**DONOTDELETE**
(Lurker)
2001-10-18 06:55 PM
Re: PIPE Command output to an Array ?

Jochen

REXX is quite similar to Kix, its an interpreted language, good string handling, good OS interfaces etc.

I think its been around 15+ years, started on VM and MVS (IBM Mainframes) and has been fairly poular amongst the IBM Mainframe Dinosaurs (like myself )

Nowadays it runs on most platforms (DOS,Windows anything etc) and free versions are available for most of them.

We converted to kix because of its superior Windows OS interfaces, plus its is clearly being developed and REXX has pretty much stabilised.

Mark.

JochenAdministrator
(KiX Supporter)
2001-10-18 07:05 PM
Re: PIPE Command output to an Array ?

... and there's even a version shipped with NTreskit.

OLE automation support ... Scary !!!

btw. who is Regina ???


J.

ShawnAdministrator
(KiX Supporter)
2001-10-18 07:23 PM
Re: PIPE Command output to an Array ?

Does the OLE support look familiar at all ?

w32CreateObject
w32ReleaseObject
w32CallFunc, w32CallProc
w32GetProperty
w32PutProperty
w32GetObject
w32GetSubObj

Like with KiXtart 2001, I think REXX is long over-due for a make-over eh ?

-Shawn

cj
(MM club member)
2001-10-19 01:36 AM
Re: PIPE Command output to an Array ?

Maybe both camps should merge and we can call it RiX

The NTReskit util is called CLIP and is used like this...

Clip version 1.1. Copyright 1997 Microsoft Corporation.

Command-line output redirector.
Clip.exe redirects command-line output to the Windows Clipboard.

Examples:
---------
dir | clip
Places a copy of the current directory listing into the Windows clipboard.

clip < readme.txt
Places a copy of the text from readme.txt into the Windows clipboard.


as for COM calls to retreive the info... Let me get back to you.

Shawn, great UDF.

cj

ShawnAdministrator
(KiX Supporter)
2001-10-19 04:18 AM
Re: PIPE Command output to an Array ?

RiX - LOL LOL LOL

Hey ceej, are you back or what ? Every now and then you pop back into the board and I think "hey cj's back" - then you fart-off for another 2 to 3 weeks - whazzup my brother ? Work been keeping you busy ? Family ? - How's little Frances ?
The Wife ?

-Shawn

Les
(KiX Master)
2001-10-19 05:02 AM
Re: PIPE Command output to an Array ?

cj,
Check your rear view mirror... I'm 100 behind you and with this totally useless post, make that 99.

Learning from the master, Shawn, currently at 1745.

Looks like maybe we're a step closer to no file I/O and the overhead...

**DONOTDELETE**
(Lurker)
2001-10-19 03:02 PM
Re: PIPE Command output to an Array ?

Rix - Isn't that what Australians call their Dog ?

**DONOTDELETE**
(Lurker)
2001-10-25 08:07 PM
Re: PIPE Command output to an Array ?

No Mark, that's what New Zealanders call their dog.