Return information from a Shell

New to FreeBASIC? Post your questions here.
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Return information from a Shell

Post by KenHorse »

I need to execute a shell command (from within FreeBASIC of course) that returns multiple lines of data but it seems I can only have an exit code returned?
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Return information from a Shell

Post by fzabkar »

Can you redirect the output of the shell command to a file, and then read the file in FB?
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Re: Return information from a Shell

Post by KenHorse »

I'd rather not as I'd be writing to an SD card and I need to call this routine pretty frequently (at least several times a minute)
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Return information from a Shell

Post by speedfixer »

I use Linux, but someone can advise that shell/console commands work as well in Windows.

Code: Select all

dim as long xnum
dim as string


xfnum = freefile()
open pipe "uname -n" for input as #xfnum
input #xfnum, csysname
close xfnum

print csysname
david
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Return information from a Shell

Post by Vortex »

Creating a Child Process with Redirected Input and Output :

https://docs.microsoft.com/en-us/window ... and-output
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Re: Return information from a Shell

Post by KenHorse »

Linux (Raspian Buster) not Windoze
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Return information from a Shell

Post by grindstone »

Redirecting the console's output is quite simple:

As an example, first compile this by the name of "TestShell":

Code: Select all

Print "Hello World"
Then, as the calling program, compile and run this in the same directory:

Code: Select all

Print "Shell response: ";
Shell "TestShell.exe 2>&1"
Sleep
This works on both Windows and Linux (also have a look at https://www.tutorialspoint.com/unix/uni ... ctions.htm)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Return information from a Shell

Post by jj2007 »

It works, Grindstone, but I suspect OP wants to do more with the returned stuff. Like e.g.

Code: Select all

MyString=ShellString("ProcessMyData.exe /quickly")
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Return information from a Shell

Post by grindstone »

jj2007 wrote:It works, Grindstone, but I suspect OP wants to do more with the returned stuff. Like e.g.

Code: Select all

MyString=ShellString("ProcessMyData.exe /quickly")
In this case I would recommend to use "OPEN PIPE" like speedfixer suggested.
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Re: Return information from a Shell

Post by KenHorse »

Correct. I need to parse several lines of the returned data from the called program.
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Re: Return information from a Shell

Post by KenHorse »

For reference, here's what the called program returns when run by itself:

Variable listing for node 1100:
RPT_ETXKEYED=0
RPT_RXKEYED=0
RPT_TXKEYED=0
RPT_NUMLINKS=2
RPT_LINKS=2,T1101,T1102
RPT_NUMALINKS=2
RPT_ALINKS=2,1101TU,1102TU
RPT_AUTOPATCHUP=0
-- 8 variables

When I try the open pipe idea (I had to make changes in order for it to compile):

Code: Select all

dim csysname as string
dim xfnum as long

xfnum = freefile()
open pipe "/usr/sbin/asterisk -rx 'rpt showvars 1100'" for input as #xfnum
input #xfnum, csysname
close #xfnum

print csysname
This is what it returns (I have to hit ENTER in order to step through it. I suspect open pipe can't deal with CR/LF)

^[[34;1R
^[[34;1R
Variable listing for node 1100:
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Return information from a Shell

Post by paul doe »

KenHorse wrote:...
This is what it returns (I have to hit ENTER in order to step through it. I suspect open pipe can't deal with CR/LF)

^[[34;1R
^[[34;1R
Variable listing for node 1100:
Use line input instead of input, and poll until there's no more data left:

Code: Select all

dim csysname as string
dim xfnum as long

xfnum = freefile()
open pipe "pipe-out" for input as #xfnum

do while( not eof( xfnum ) )
  line input #xfnum, csysname
  ? csysname
loop

close #xfnum

sleep()
Here, pipe-out is just a process that spits the info you gave above.
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Re: Return information from a Shell

Post by KenHorse »

Still have to step through the first 2 lines (hit ENTER). Looks like some high order ASCII?

(I changed the print line to: ? "Data From ASL " ; csysname)

^[[23;1R
^[[23;1R
Data From ASL Variable listing for node 1100:
Data From ASL RPT_TXKEYED=0
Data From ASL RPT_NUMLINKS=2
Data From ASL RPT_LINKS=2,T1102,T1101
Data From ASL RPT_NUMALINKS=2
Data From ASL RPT_ALINKS=2,1102TU,1101TU
Data From ASL RPT_ETXKEYED=0
Data From ASL RPT_RXKEYED=0
Data From ASL RPT_AUTOPATCHUP=0
Data From ASL -- 8 variables
paul doe
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Return information from a Shell

Post by paul doe »

KenHorse wrote:Still have to step through the first 2 lines (hit ENTER). Looks like some high order ASCII?
...
Most likely control characters. You may want to either filter them, or not outputting them at all.
KenHorse
Posts: 56
Joined: Jan 27, 2012 0:08

Re: Return information from a Shell

Post by KenHorse »

I have no control over the binary I'm calling (/usr/sbin/asterisk).

I can filter the stuff but the question I have is, as it requires hitting "ENTER" to step past the 1st 2 returns "^[[23;1R
^[[23;1R), how do I tell my program to step past those lines?
Post Reply