Command Output Redirection

Post

Posted
Rating:
#1 (In Topic #381)
Trainee
 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
Online now: No Back to the top

Post

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

Code (gambas)

  1. Public Sub Main()
  2.  
  3. Dim strUSBDevices As String
  4.  
  5.    shell "lsusb" To strUSBDevices
  6.  
  7. Print strUSBDevices
  8.  

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
Avatar
Guru
cogier is in the usergroup ‘Guru’
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'.

Code (gambas)

  1. Dim sTemp As String
  2.  
  3.   Shell "lsusb" To sTemp
  4.   Print sTemp
  5.  

The output on my computer is: -

Code

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.

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   Dim Gridview1 As Gridview
  4.   Dim sLSUSB As String[]
  5.   Dim sTemp As String
  6.   Dim iRow As Integer
  7.  
  8.   With Me
  9.     .Arrangement = Arrange.Vertical
  10.     .Padding = 5
  11.     .Title = "USB"
  12.  
  13.   With Gridview1 = New GridView(Me)
  14.     .Expand = True
  15.     .Columns.Count = 1
  16.     .Header = GridView.Vertical
  17.  
  18.   Shell "lsusb" To sTemp
  19.   sLSUSB = Split(sTemp, gb.NewLine, "", True)
  20.  
  21.   For iRow = 0 To sLSUSB.Max
  22.     Inc Gridview1.Rows.Count
  23.     Gridview1[iRow, 0].Text = sLSUSB[iRow]
  24.   Next
  25.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Trainee
 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
Online now: No Back to the top
1 guest and 0 members have just viewed this.