Shell-To and standard error

Post

Posted
Rating:
#1 (In Topic #1032)
Regular
JumpyVB is in the usergroup ‘Regular’
How to access standard error on shell calls?

Code (gambas)

  1. Public Sub Form_Open()
  2.   Dim Stdout As String
  3.   Shell "ls /root/" To Stdout
  4.   Me.Text = Stdout
  5.   ' Stdout is "" (i.e. empty)
  6.   ' Where do I find the error which should be:
  7.   ' "ls: cannot open directory '/root/': Permission denied"
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’

JumpyVB said

How to access standard error on shell calls?

Code (gambas)

  1. Public Sub Form_Open()
  2.   Dim Stdout As String
  3.   Shell "ls /root/" To Stdout
  4.   Me.Text = Stdout
  5.   ' Stdout is "" (i.e. empty)
  6.   ' Where do I find the error which should be:
  7.   ' "ls: cannot open directory '/root/': Permission denied"


First use 2>&1 in the shell command to redirect stderr to stdout then use Process.LastValue to check for error value in last run process

Code (gambas)

  1. Public Sub Form_Open()
  2.   Dim Stdout As String
  3.  
  4.  ' First use 2>&1 to direct stderr to stdout
  5.  
  6.   Shell "ls /root/ 2>&1" To Stdout
  7.  
  8.   If Process.LastValue Then
  9.    ' there was an error value so display the output text and Return
  10.   Message.Error(Stdout)
  11.   Return  
  12.  
  13.  
  14.   ' if error above then i did Return so if here there was no error and Stdout is no longer empty and should be a file list.
  15.  

Note:  only use this if using To or Wait with the Shell/Exec command so the process is not asynchronous and finishes before checking Process.LastValue or you may check the value of the previously run process.
Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
JumpyVB is in the usergroup ‘Regular’
Thank you Bruce.
Online now: No Back to the top
1 guest and 0 members have just viewed this.