Shell Script Problem

Hi all newbie here,

I want to execute a shell (dos) command in windows.

If i type the shell command in quotes it works.

s.Execute(“notepad”)

this opens notepad in windows

however if i put a string variable it doesn’t work.

command = “notepad”
s.Execute(command)

I tried adding “” before and after, no good.

Thank you

Did you declare that variable first?

Var command as String
1 Like

yes i did actually. can’t understand why it’s not working on windows 7. i’ll try it on windows 10.

Same result with Windows 10. Works on mac and linux, but not on windows.

If i choose mode = 0 the following command gives Error code: -2

Dim s as new shell
Dim command as string
s.mode = 0
command = "ping localhost"
s.Execute(command)

ping is taking longer than 2 seconds to complete. Set the timeout property to -1.

1 Like

Tried that, not working.

I changed mode to 1. This works:

command = "notepad"
s.Execute(command)

What i am trying to achieve is, query a database for a string, get the string and execute it on shell.

In my MySQL database, there is a column that holds the string value (varchar (1000))
I query it using db.SQLSelect, get the result in a recordset variable, then get the string value using the following code:

dim rs as RecordSet
rs = db.SQLSelect("SELECT * FROM A WHERE B = 0")
command = str(rs.Field("command").StringValue)

Then i want to execute this in shell but it doesn’t work. I can see the string is correct:

s.Mode = 1 
s.Timeout = -1
stdout.WriteLine("Executing " + command)
s.execute(command)

So if i change it to mode 1 and if i type it in quotes like s.execute(“notepad”) it works, however if i try to run it using the string it doesn’t. Could that have something to do with encoding? I mean i can see it’s typed correctly but…

Your code is working for me. Xojo 2020 Release 2 and Windows 10 in the remote Debugger.


Screenshot 2020-11-26 at 11.46.21

You can check the used encoding in the debugger:


How does that look like on your machine?

Might there be an issue with something not trivial then having stored n0tepad instead of notepad in your database? Or more likely a carriage return or something else behind the word notepad, which is not shown while printing it to the console?

Can you output the value in Windows10 via a simple

MessageBox( command )

Perhaps the command is working, but you just don’t have the rights to execute it on this particular machine, as it is not running as an administrator? Then you could compile the app and then run the Programm in admin mode.

1 Like

I have an older version 2019.1.1

Finally i found the solution. I used the following command:

command = command.Trim
s.execute(command)

So there was something that needed trimming.

Thank you all for your time.

1 Like

Great! So I was right with my assumption:

Or more likely a carriage return or something else behind the word notepad, which is not shown while printing it to the console?

My suggestion: using trim is okay if it works, but I would look at the root cause, you probably entered some special character in your database. Why do I suggest this? You are now sort of hiding an issue, via a command. That works, but it might cause you issues in the future and it is kind of “overdosed”.

string.trim is doing nothing else than removing leading and trailing whitespaces

It’s better to check your datasource and correct the issue there, it is nothing else than one or more “whitespaces” :wink:

1 Like