About the Terminal (Shell)

Since you have two eyes
But only one works.

Talking about work, we do not get that in the Terminal.

Yes, you are right, I do not checked in the Execute button is there is some selected code and report the error. But I always select a command to execute.

The provided code is executed with the default mode.

Setting the working folder does not works whatever mode I use.

Now if you think Xojo’s Shell works fine, good, but I still have to see it to believe it (that is the St Thomas in me who speaks).

I nearly forgot: what I want to do with Xojo works fine when I do it with the Terminal.

You, probably. You just forgot to tell us :wink:
Well, that’s not important.

With default mode (and asynchronous mode), you can’t issue several commands to be executed together, that’s the whole point.
Both of them execute a single command in a “brand new” environment.
So, they work like that, with your attempt:

Set working directory.
<new shell environment; resets working directory, among other things>
List working directory (which, as has been reset, is the default directory of the shell)

 and so on.
You have to use mode 3 (interactive) to execute a list of related commands.

Have you looked at the documentation for the Shell class?
You’re doing it wrong in your example project. In interactive mode (the one you must use here), you start by executing the first command (using the Execute method), subsequent commands are then issued using the WriteLine method (no longer the Execute method, used only at the beginning), when you get the result of the current command (so, you send the next command when the current one has finished and has triggered the Completed event). Take a look at the ExecuteMode property of the Shell class, in the language reference.

St-Thomas also had to make correct tests to get correct results :wink:

1 Like

Thank you for your explanation, but I still don’t get it.

Using the same steps, in the Terminal it works, with this project it behave differently. And so, whatever the mode is (Interactive or not).

All I wanted was to use that example as I do in the Terminal.

I read the (far above link) documentation and use it to display the env data; yes, there is a second window (click in “Show Environment”).

I only want to do some basic tasks; the first one is to set the working directory as I do in the Terminal. But, maybe I have to stay with the Terminal, after all.

Last modifications:

The Environment window (High Sierra, is different on M1 / BS).

Let’s think about it the other way around. If you take the Terminal, you can also act like your current Xojo code: that is if you close every Terminal window between each command, like this:
(Terminal window)
cd /users/me/desktop
(Close Terminal window)
(Open new Terminal window)
mkdir “Emile”
(Close Terminal window)
(Open new Terminal window)
ls -l
(Close Terminal window)

Try it that way and you’ll get the same behaviour as with Xojo.

Try this in an empty Xojo project:
Add a new class to the project, name it “CMyShell” and of type “Shell”. Add a property “CurrentStep As Integer” to it.
Add the DataAvailable event to this class and put this:

Select case CurrentStep
case 0 'First command
  me.WriteLine "cd /Users/Me/Desktop/" //Replace Me by your username, of course
case 1 'Second command
  Me.WriteLine "mkdir ""Test"""
else 'Finally
  me.Close
  Return
end Select
CurrentStep=CurrentStep+1

In the main window, add a property: “MyShell As CMyShell”.
In the window’s MouseDown event, put this:

MyShell=New CMyShell
MyShell.ExecuteMode=Shell.ExecuteModes.Interactive
MyShell.Execute "sh"

Do you get the idea?

1 Like

I think so.

In the first (ans following) version of the shared project, I had a window Property that I use all around (I think; it’s hard to remember when I use the project once a day or so, sor some minutes only).

The trick "do something in the Terminal, then close the window, open anther and check what you get” is
 awesome. I never do that before (in the same “session”).

I will read your code later today (it’s 1H40 at night right now, time to bed).

Thank you for your tips and explanations.

Amazing. I posted the solution a few days ago, but it seems not to have been understood. It should be simple enough


That is how you select the working directory, and do everything in the same shell session.

2 Likes

Well, I understood it, but wanted to explain an interactive method (that is, for example, when not all commands are known in the beginning).
I also have to confess that this all-in-one-line format isn’t something I usually dealt, so, by lack if habit, it’s not the path I easily follow.
But I thank you for having posted it; it’s certainly a great way when you have a list of known commands to pass.

I read your mail Michel, but I do not wanted to issue many commands on the same line.

I’m not at home, so I will check later, but your example line returns:

mkdir: zoe: Read-only file system

then:
bash: line 0: cd: zoe: No such file or directory

The working folder is the root’s boot HD folder


Are you using Michel’s example here, or your own with specifying the current directory?
(note that Michel’s example creates a folder as the first command, which may fail with the error you get, since the current folder isn’t set).

That’s expected when the previous command fails.

A guess or you specified it?

sigh ;(

There are two ways to keep the environment: with a single line containing several commands separated by the end of line character (;), or by creating a bash script. The issue with the bash script is that it needs to have the execute bit set with chmod, through yet another shell, after it has been created, and before executing it.

I don’t know any way to keep the working directory between shell sessions. If you need a terminal, your best option is to use the Terminal.

Thank you Michel; I will drop Xojo and go back with the Terminal.

Write the result of a call to pwd into a var and then “cd” into it at the start of the next terminal session. That’s the way that I do it if I have to close and reopen a terminal session in Shell.