I’m trying to run a command line tool from within a Shell in a desktop app. I’m on a Mac.
The tool is definitely installed on my Mac in my PATH. The tool is called rtl_433.
In the macOS Terminal if I run:
If I put this code in the Opening event of my app:
Var sh As New Shell
sh.ExecuteMode = Shell.ExecuteModes.Synchronous
Var result As String = sh.Result ' bin/ls
result = sh.Result ' Empty. Shell returns error 1
The Shell can’t find rtl_433. The execute command is working though because it finds the ls tool which is bundled with macOS.
Does anyone have any code that will run a CLI from a Shell? I’m guessing this has something to do with the incorrect PATH in Xojo’s Shell class but I’m not sure how to address it.
When you log into your account, it gets a lot of information from the environment. When you run a shell script, or run a shell from xojo, you don’t have all that information. Both shell scripts and xojo shells have to be more explicit.
FYI, you can use something like this on Mac or Linux to get the path to the utility you want:
Private Function FindCliApp(cli As String) As FolderItem
dim f as FolderItem
#if TargetMacOS or TargetLinux then
dim sh as new shell
sh.Execute "/bin/bash -lc " + ShellQuote("which " + cli.Trim)
if sh.ErrorCode = 0 then
dim path as string = sh.Result
path = ReplaceLineEndings(path, EndOfLine).Trim
path = path.NthField(EndOfLine, path.CountFields(EndOfLine))
f = new FolderItem(path, FolderItem.PathTypeNative)
Private Function ShellQuote(s As String) As String
s = s.ReplaceAll("'", "'\''")
s = "'" + s + "'"