Process: write to its stdin and capture its stdout

Post

Posted
Rating:
#1 (In Topic #1164)
Trainee
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.

Code (gambas)

  1. proc = Exec [...] For Read Write
  2.  
  3. ' this seems to work
  4. proc.Begin()
  5. Print #proc, "something"
  6. proc.Send()
  7.  
  8. proc.Wait()
  9.  
  10. ' Error: the stream is closed
  11. If Lof(proc) Then myVar = Read #proc, Lof(proc)
  12.  
  13. 'This also doesn't work: the stream is closed
  14. For Each ostrega In proc.Lines
  15.     Print ostrega    
  16.   Next
  17.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’

Carotino said

Code (gambas)

  1.     Print ostrega    
  2.  
…ma sei Veneto ?

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
vuott is in the usergroup ‘Regular’

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee

vuott said

Carotino said

Code (gambas)

  1.     Print ostrega    
  2.  
…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:)
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
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

Code (gambas)

  1. Shell "Echo something | mySoftware" To myVariable
  2.  

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
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
vuott is in the usergroup ‘Regular’

Carotino said

 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:

Guide della comunità - Gambas-it.org - Wikipedia

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’

Carotino said

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

Code (gambas)

  1. Shell "Echo something | mySoftware" To myVariable
  2.  

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..

Code (gambas)

  1.  
  2. Private $sPipedString As String  ' this will be the piped text from stdin
  3.  
  4.  
  5.   $sPipedString = GetStdio()
  6.  
  7. ' If user typed something like echo "this text" | myProgram.gambas in a terminal
  8. ' now $sPipedString will be "this text"
  9.  
  10.  
  11.  
  12.   Dim SourceBuffer As String = ""
  13.  
  14.  
  15.     SourceBuffer &= File.In.ReadLine() & "\n"
  16.   Wend
  17.  
  18.   Return SourceBuffer
  19.  
  20.  
  21.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.