Return information from a Shell
Return information from a Shell
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?
Re: Return information from a Shell
Can you redirect the output of the shell command to a file, and then read the file in FB?
Re: Return information from a Shell
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)
-
- Posts: 606
- Joined: Nov 28, 2012 1:27
- Location: CA, USA moving to WA, USA
- Contact:
Re: Return information from a Shell
I use Linux, but someone can advise that shell/console commands work as well in Windows.
david
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
Re: Return information from a Shell
Creating a Child Process with Redirected Input and Output :
https://docs.microsoft.com/en-us/window ... and-output
https://docs.microsoft.com/en-us/window ... and-output
Re: Return information from a Shell
Linux (Raspian Buster) not Windoze
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: Return information from a Shell
Redirecting the console's output is quite simple:
As an example, first compile this by the name of "TestShell":
Then, as the calling program, compile and run this in the same directory:
This works on both Windows and Linux (also have a look at https://www.tutorialspoint.com/unix/uni ... ctions.htm)
As an example, first compile this by the name of "TestShell":
Code: Select all
Print "Hello World"
Code: Select all
Print "Shell response: ";
Shell "TestShell.exe 2>&1"
Sleep
Re: Return information from a Shell
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")
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: Return information from a Shell
In this case I would recommend to use "OPEN PIPE" like speedfixer suggested.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")
Re: Return information from a Shell
Correct. I need to parse several lines of the returned data from the called program.
Re: Return information from a Shell
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):
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:
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
^[[34;1R
^[[34;1R
Variable listing for node 1100:
Re: Return information from a Shell
Use line input instead of input, and poll until there's no more data left: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:
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()
Re: Return information from a Shell
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
(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
Re: Return information from a Shell
Most likely control characters. You may want to either filter them, or not outputting them at all.KenHorse wrote:Still have to step through the first 2 lines (hit ENTER). Looks like some high order ASCII?
...
Re: Return information from a Shell
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?
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?