1

I have a PHP CLI script and want to execute an interactive bash command (e.g. less huge-file.txt) from that script and get the same view (with less e.g. the navigation controls) as if I had started it directly from the terminal.

The normal system() call is not enough because they don't pause and just return all the output at once (in the case of e.g. less).

Sense is that I have a CLI script that organises several tasks. Some of them use complex bash commands and I just want to invoke the bash scripts from PHP but get the original terminal behaviour.

4
  • that's nice. Good luck with that (even though it's not possible unless you're running PHP in cli mode)... Did you have a programming question? This site is for questions, not a place to dump your to-do/requirements lists. Commented Mar 1, 2016 at 21:37
  • 1
    @MarcB I thought more generic questions are better? I was struggling to find a good and short solution, so I thought I would be good to share my findings ans also ask for better ones? Commented Mar 1, 2016 at 22:02
  • @thatotherguy I have a CLI script and don't need any web access. I reworded the question to make that more clear. Commented Mar 1, 2016 at 22:06
  • Ah ok, then never mind Commented Mar 1, 2016 at 22:11

2 Answers 2

2
proc_open( 'less huge-file.txt', array( STDIN, STDOUT, STDERR), $pipes);

This calls the command and passes all controls etc. through, so that there is no distinction to a normal less huge-file.txt.

Still a bit clunky but a lot shorter compared to other examples I could find.

Sign up to request clarification or add additional context in comments.

2 Comments

@hek2mgl Hm, maybe it depends on the environment? For me with PHP 7 CLI on Mac OS 10.9.5 it works fine.
I've used php5.6 for the test. I'll give php7 a try in the evening .
0

Yes it is possible. I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS

I doubt you are really looking to script using less (that will get ugly, head / tail / sed /awk are your friends in text manipulation), but getting a real shell behavior is very possible.

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', false);

//open file in less. $return1 will contain the view and the first part of the file you opened. The second argument is needed to delimit the return since less will not end in a shell prompt.
$return1  = $shell->exeCmd('less /path/to/huge-file.txt', 'huge-file.txt');

//quit less, you must do this or the shell will hang on the less prompt and will have to be forcefully terminated.
$return2  = $shell->exeCmd('q');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.