#80329 - 2001-08-16 10:35 AM
PIPE Command output to an Array ?
|
Anonymous
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
|
|
|
|
#80332 - 2001-10-13 06:43 PM
Re: PIPE Command output to an Array ?
|
Les
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
|
|
|
|
#80336 - 2001-10-15 02:55 AM
Re: PIPE Command output to an Array ?
|
Shawn
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
|
|
|
|
#80339 - 2001-10-15 09:28 PM
Re: PIPE Command output to an Array ?
|
Les
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
|
|
|
|
#80343 - 2001-10-18 03:28 PM
Re: PIPE Command output to an Array ?
|
Anonymous
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
|
|
|
|
#80346 - 2001-10-18 06:55 PM
Re: PIPE Command output to an Array ?
|
Anonymous
Anonymous
Unregistered
|
JochenREXX 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
|
|
|
|
Moderator: Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 496 anonymous users online.
|
|
|