Command Output Redirection

Post Reply
Beeza
Posts: 2
Joined: Monday 09th March 2020 2:02pm

Command Output Redirection

Post by Beeza » Monday 09th March 2020 2:35pm

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

vuott
Posts: 28
Joined: Wednesday 05th April 2017 6:07pm

Re: Command Output Redirection

Post by vuott » Monday 09th March 2020 3:09pm

Public Sub Main()

Dim strUSBDevices As String

   shell "lsusb" To strUSBDevices

Print strUSBDevices

End

User avatar
cogier
Site Admin
Posts: 428
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Command Output Redirection

Post by cogier » Monday 09th March 2020 3:14pm

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

Beeza
Posts: 2
Joined: Monday 09th March 2020 2:02pm

Re: Command Output Redirection

Post by Beeza » Monday 09th March 2020 5:01pm

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

Post Reply