Page 1 of 2 12>
Topic Options
#80329 - 2001-08-16 10:35 AM PIPE Command output to an Array ?
Anonymous
Unregistered


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 ]

Top
#80330 - 2001-08-16 03:14 PM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
#80331 - 2001-10-13 02:43 AM Re: PIPE Command output to an Array ?
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
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.
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
#80332 - 2001-10-13 06:43 PM Re: PIPE Command output to an Array ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#80333 - 2001-10-13 07:01 PM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
#80334 - 2001-10-13 07:20 PM Re: PIPE Command output to an Array ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#80335 - 2001-10-13 07:32 PM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
#80336 - 2001-10-15 02:55 AM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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 ]

Top
#80337 - 2001-10-15 08:44 PM Re: PIPE Command output to an Array ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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?

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#80338 - 2001-10-15 09:07 PM Re: PIPE Command output to an Array ?
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
cool!

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

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

Bryce

Top
#80339 - 2001-10-15 09:28 PM Re: PIPE Command output to an Array ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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.

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#80340 - 2001-10-15 09:32 PM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
#80341 - 2001-10-15 09:39 PM Re: PIPE Command output to an Array ?
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
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!

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#80342 - 2001-10-15 09:52 PM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
#80343 - 2001-10-18 03:28 PM Re: PIPE Command output to an Array ?
Anonymous
Unregistered


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 ]

Top
#80344 - 2001-10-18 04:19 PM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
#80345 - 2001-10-18 04:52 PM Re: PIPE Command output to an Array ?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
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 ???

_________________________



Top
#80346 - 2001-10-18 06:55 PM Re: PIPE Command output to an Array ?
Anonymous
Unregistered


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.

Top
#80347 - 2001-10-18 07:05 PM Re: PIPE Command output to an Array ?
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
... and there's even a version shipped with NTreskit.

OLE automation support ... Scary !!!

btw. who is Regina ???


J.

_________________________



Top
#80348 - 2001-10-18 07:23 PM Re: PIPE Command output to an Array ?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
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

Top
Page 1 of 2 12>


Moderator:  Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 514 anonymous users online.
Newest Members
Timothy, Jojo67, MaikSimon, kvn317, kixtarts2025
17874 Registered Users

Generated in 0.077 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org