Process: write to its stdin and capture its stdout

New to Gambas? Post your questions here. No question is too silly or too simple.
Post Reply
Carotino
Posts: 3
Joined: Tuesday 26th December 2023 5:40pm

Process: write to its stdin and capture its stdout

Post by Carotino »

Hello to everybody.

I have this problem.
I need to Exec a command, writing to its standard input, then capturing what it writes to stdout.
I've been bouncing back and forth in the Gambas documentation wiki, without much success.

Dim proc As Process
proc = Exec [...] For Read Write

' this seems to work
proc.Begin()
Print #proc, "something"
proc.Send()
 
proc.Wait()

' Error: the stream is closed
If Lof(proc) Then myVar = Read #proc, Lof(proc)

'This also doesn't work: the stream is closed
For Each ostrega In proc.Lines
    Print ostrega    
  Next


There is some code here in the wiki https://gambaswiki.org/wiki/lang/open which seems to align with the notion of "read events" I found here and there, which involves the definition of a sub called NAMEOFFILE_Read that is seemingly associated in a magic way to the file with the same name. But it looks like I cannot define a sub inside a sub, so I'm at a loss.

Any help is greatly appreciated.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Process: write to its stdin and capture its stdout

Post by vuott »

Carotino wrote: Tuesday 26th December 2023 6:09pm
    Print ostrega    
...ma sei Veneto ?
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Process: write to its stdin and capture its stdout

Post by vuott »

Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Carotino
Posts: 3
Joined: Tuesday 26th December 2023 5:40pm

Re: Process: write to its stdin and capture its stdout

Post by Carotino »

vuott wrote: Tuesday 26th December 2023 9:50pm
Carotino wrote: Tuesday 26th December 2023 6:09pm
    Print ostrega    
...ma sei Veneto ?
LOL hai ragione, in realtà sono bergamasco, di solito uso nomi sensati per le variabili ma per l'appunto ero arrivato all'ostrega:)
Carotino
Posts: 3
Joined: Tuesday 26th December 2023 5:40pm

Re: Process: write to its stdin and capture its stdout

Post by Carotino »

Well, congrats for your research!
By the way I understood what I previously referred to as "magical", that is Event names.

In the end I think I'll end up to use something to the likes of

Shell "Echo something | mySoftware" To myVariable


or, which I thinks should be better in the long run, I'll use temporary files in /dev/shm to pass data around.

It still baffles me that such a trivial task is so overly convoluted

Thanks very much
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: Process: write to its stdin and capture its stdout

Post by vuott »

Carotino wrote: Tuesday 26th December 2023 11:00pm I'll use temporary files in /dev/shm to pass data around.
For passing data from one program to another, you can also see these pages:

https://www.gambas-it.org/wiki/index.ph ... mma_Gambas
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1579
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Process: write to its stdin and capture its stdout

Post by BruceSteers »

Carotino wrote: Tuesday 26th December 2023 11:00pm Well, congrats for your research!
By the way I understood what I previously referred to as "magical", that is Event names.

In the end I think I'll end up to use something to the likes of

Shell "Echo something | mySoftware" To myVariable


or, which I thinks should be better in the long run, I'll use temporary files in /dev/shm to pass data around.

It still baffles me that such a trivial task is so overly convoluted

Thanks very much
Here is a simple method to use piping with your program..


Private $sPipedString As String  ' this will be the piped text from stdin

Static Public Sub Main()

  $sPipedString = GetStdio()

' If user typed something like echo "this text" | myProgram.gambas in a terminal
' now $sPipedString will be "this text"

End

Static Public Sub GetStdio() As String

  Dim SourceBuffer As String = ""

  If Not Lof(File.In) Then Return

  While Not Eof(File.in)
    SourceBuffer &= File.In.ReadLine() & "\n"
  Wend

  Return SourceBuffer

End

If at first you don't succeed , try doing something differently.
BruceS
Post Reply