Page 1 of 1

Command Output Redirection

Posted: Monday 9th March 2020 2:35pm
by Beeza
I have an application in which I import Linux system information by piping the output of a Linux command to a file, then reading the contents of the file into my application e.g

dim strCommand as String
dim strUSBDevices as string
dim myFile as File

strCommand = "lsusb > usb_devices.txt"
shell strCommand

myFile = open "usb_devices.txt" for input
line input #myFile, strUSBDevices

myFile.close

Obviously, you would expect the command to return more than one USB device, so the file would have multiple lines, but you get the point.

My question is this: Is there a way to redirect the output of the Linux command to write directly to a string variable declared in the application, thereby removing the need for the file?

Thanks

Beeza

Re: Command Output Redirection

Posted: Monday 9th March 2020 3:09pm
by vuott
Public Sub Main()

Dim strUSBDevices As String

   shell "lsusb" To strUSBDevices

Print strUSBDevices

End

Re: Command Output Redirection

Posted: Monday 9th March 2020 3:14pm
by cogier
Hi Beeza and welcome to the forum.
Is there a way to redirect the output of the Linux command to write directly to a string variable declared in the application
Yes, you can. In the code below I have redirected the output to the variable 'sTemp'.
Dim sTemp As String

  Shell "lsusb" To sTemp
  Print sTemp
The output on my computer is: -

Code: Select all

Bus 002 Device 002: ID 8087:8000 Intel Corp. 
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:8008 Intel Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 003: ID 0461:4d64 Primax Electronics, Ltd 
Bus 003 Device 002: ID 2a7a:9a18  
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
EDIT. Vuott just beat me to this.

EDIT2

Try this code is a new Graphical Application.
Public Sub Form_Open()

  Dim Gridview1 As Gridview
  Dim sLSUSB As String[]
  Dim sTemp As String
  Dim iRow As Integer

  With Me
    .Arrangement = Arrange.Vertical
    .Padding = 5
    .Title = "USB"
  End With

  With Gridview1 = New GridView(Me)
    .Expand = True
    .Columns.Count = 1
    .Header = GridView.Vertical
  End With

  Shell "lsusb" To sTemp
  sLSUSB = Split(sTemp, gb.NewLine, "", True)

  For iRow = 0 To sLSUSB.Max
    Inc Gridview1.Rows.Count
    Gridview1[iRow, 0].Text = sLSUSB[iRow]
  Next

End

Re: Command Output Redirection

Posted: Monday 9th March 2020 5:01pm
by Beeza
Thanks Vuott and Cogier. I never expected the solution to be that simple. Thanks also for the demo code.

I'll monitor this forum regularly in the hope that I can similarly help somebody else out in due course.

Beeza