V
V
Vyacheslav2016-08-01 13:54:05
PHP
Vyacheslav, 2016-08-01 13:54:05

How not to close the console (and not to display too much) when calling the PHP-CLI console script?

I want to make a console php script so that when you double-click on a file, the console opens (the Terminal program in Mac OS X), the script displays the result of the work, and the console does not close. But at the same time, so that nothing superfluous and distracting is displayed.
For example, I create a file "Hello"or "Hello.command"with this content:

#! /usr/bin/env php
<?php
echo "\n\nПривет мир!\n\n";

I save the file, set the rights to 755 for it. I double-click, the console is called, and after a moment it closes. Problem: You can't see the result of running the program.
Okay, I read it on the Internet, they advise you to make a setting in the Terminal so that it does not close after the script is certified. We go to the settings: Terminal> Settings> settings> Shell> "When exiting the shell" set "Do not close the window". (Mac OS X 10.8 system)
Trying to run the script. The console opens, now does not close, and displays:
Last login: Mon Aug  1 01:13:40 on ttys004
MyMac:~ user$ /Users/user/folder/bin/test ; exit;

Привет мир!

logout

[Процесс завершен]

How to avoid redundant output? Is it possible to leave only "Hello world!" and everything else in general, so that it doesn’t exist?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav, 2016-08-01
@SlavaAurim

Found a solution. Of course, this is a hack, but it almost looks like it should:

#! /usr/bin/env php
<?php
system('clear');

echo "\n\nПривет мир!\n\n";

echo "\033[37m" , "Нажмите Esc для выхода..."; // светло-серая подсказка  
system("stty -icanon"); // отключение буферизации символов в терминале
while( ord( fread(STDIN, 1)) != 27 ) {} // считываем нажатые символы,  до нажатия Esc
exit;

A
Alexey Skobkin, 2016-08-01
@skobkin

In general, this is not quite the right approach. You need to solve this problem at the terminal level. It's just that what you do on click is initially such a procedure that the terminal window will be open only until the process has completed.
At the PHP level, you can, of course, resort to a hack and, for example, wait for input . But this is conceptually wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question