T
T
trofimovfedor2021-05-05 21:17:42
PHP
trofimovfedor, 2021-05-05 21:17:42

How not to end a command line session when using shell_exec()?

I need to develop a command line on a site for Windows. When going through the directories through "cd" and executing a new command, all progress is lost and I return to the working directory. Is it possible to somehow not end the session and stay in the previously selected directory?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Koryukov, 2021-05-05
@MadridianFox

No way.
Each call to shell_exec spawns a new shell process (bash or cmd) that inherits environment variables from the current php process.
The current directory is the state of the shell that is lost between calls to shell_exec.
Depending on the task being solved, you either need to launch a separate cmd process and send commands to it, or remember the current state and add the setting of this state to each shell_exec call, for example, it’s trite to insert cd before each command like this:

$cwd = "/path/to/dir";
shell_exec("cd {$cwd}; ls");
shell_exec("cd {$cwd}; chmod +x banana.sh");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question