Troubles with Shell

I was fooling around with the Shell, and fall into a trap.

I fired the Example “Interactive Shell", put my command and get

In my project I get error= 1; or “The Shell is closed…” if I check if it is running.

The Documentation code below works until I modify it (see code below):


Var s As Shell
s = New Shell
#If TargetWindows Then
  s.Execute("dir")
#ElseIf TargetMacOS Or TargetLinux Then
  s.Execute("ls -la > liste.txt") // I add " > liste.txt"
#EndIf
If s.ExitCode = 0 Then
  TextArea1.Value = s.Result
Else
  MessageBox("Error code: " + s.ErrorCode.ToString)
End If

Without my addition ( > liste.txt), the code works fine.

Advices are welcome.

you need the full path to any command.

("ls -la >> liste.txt") you need 2 of them “>>”

It always worked (and still) with only one “>”.

Excepted if Xojo needs 2 ?

With 2 >>:

With the “Interactive Shell” example, the command:


sqlite3 -header -csv ./Users/emile_schwarz/Downloads/Emile/www.w3schools.com.db "select * from Customers;" > ./Users/emile_schwarz/Customers.csv

Works.

But on the Terminal, I do not needed to do that.

M1/Monterey 12.5

The Xojo code below do nothing:


Shell_Cmd = "sqlite3 -header -csv ./Users/emile_schwarz/Downloads/Emile/www.w3schools.com.db ""Select * from Customers;"" > ./Users/emile_schwarz/Customers.csv"
s.Execute(Shell_Cmd)

The Shell_Cmd is exactly the same I use in the Terminal.

A xojo shell is NOT a terminal, the whole terminal profile is missing. You miss the PATH variable and many more environment variables.

You can do however in interactive mode:

// sh needs to be in interactive mode for this to work:
sh.Execute("PATH=" + System.EvironmentVariable("PATH")) // Sets the PATH env variable in the shell
sh.Execute("cd /my/working/dir") // Folder where liste.txt is located
sh.Execute("ls -la >> liste.txt") // exectue the command

One “>” could be enough altough mac uses zsh instead of bash and may be different.

1 Like

Hi Derk,

Thank you for your help.

I’ve made my own code as interactive mode without success (before opening this thread)…
I even pass “full path” for either the data base and the csv files (./Users/ etc.).

I’ve make the changes, run and… nothing…

Definitely, I am cursed !

PS: what is a Xojo Shell ?

The original example from the Shell Documentation stop working when I added:

s.ExecuteMode = Shell.ExecuteModes.Interactive

you need to set s.Canonical = True othwise the line based stuff won’t work
set all this BEFORE calling .Execute

I do not use Control characters, but I try not been stuborn, so the code is:


Var s As Shell
s = New Shell

s.ExecuteMode = Shell.ExecuteModes.Interactive
s.Canonical = True

#If TargetWindows Then
  s.Execute("dir")
#ElseIf TargetMacOS Or TargetLinux Then
  s.Execute("ls -la")
#EndIf
If s.ExitCode = 0 Then
  TextArea1.Value = s.Result
Else
  MessageBox("Error code: " + s.ErrorCode.ToString)
End If

The only addition to the Documentation example (that works as is) is these two lines:


s.ExecuteMode = Shell.ExecuteModes.Interactive
s.Canonical = True

XOJO EXAMPLE USE A TEXTFIELD TO RECEIVE A LIST OF FILES !!!
I changed that to TextArea1…

So what’s the issue? The examples don’t work? then please report it in xojo issues.

What is specificly your problem that’s not working now?

s = New Shell // s must be a property of window or app or such...
s.ExecuteMode = Shell.ExecuteModes.Interactive // < event based
s.Canonical = True // < line based

#If TargetWindows Then
  s.Execute("set PATH=" + System.EnvironmentVariable("PATH")) // set this path env on windows
  s.Execute("dir")
#ElseIf TargetMacOS Or TargetLinux Then
  s.Execute("export PATH=" + System.EnvironmentVariable("PATH")) // set this path env on linux/mac
  s.Execute("ls -la")
#EndIf

// This \/ \/ \/ won't work, you NEED to use the events...
//If s.ExitCode = 0 Then
//  TextArea1.Value = s.Result
//Else
//  MessageBox("Error code: " + s.ErrorCode.ToString)
//End If

The terminal is able to export a TABLE from an sqlite data base to a csv file.

That is where I came from.

Then, because my code does not worked in Xojo, I used one of the documentation examples:
https://documentation.xojo.com/versions/2022r2/api/os/shell.html#sample-code

and here we are.

Sorry to bother you.

I only wanted to share with Xojo code how to export to a .sqlite TABLE to .csv (because Xojo does not implemented that in SQLite Data Base) while I had some minutes to use to do that. Minutes were expanded to hours.

The solution was either:

a. shotdown (all night) and reboot the computer…

b. Use (Ibet on that one):


s.ExecuteMode = Shell.ExecuteModes.Synchronous

I was ready to try Asynchronous too… but today first bet was the good one.

'bye until tomorrow…