How to determine if CLI script output is output to another command?

Possible duplicate:
Can PHP determine if it starts from a cron job or from the command line?

I am trying to determine if the STDOUT resource is being transferred on the command line of the PHP script to another command or not in order to display tabular data accordingly (if the output is directly to the terminal, it looks good to have table cells wrapped in borders +-----+ , but not trying to verify this data with awk or another command.)

Having received this answer , I tried to check the STDOUT resource with stream_get_meta_data() and all the other functions that I can find in the PHP manual that works with streams, but in each case the resource looks exactly the same, regardless of whether it is connected to another process or not.

Is there any way to find this in PHP?

+4
source share
1 answer

You can use posix_isatty , assuming you have a POSIX extension (maybe you are doing this):

 posix_isatty(STDOUT) 

If this is true, then you do not go to the channel. isatty is a common method for this in C programs and others.

Please note that this does not verify that it outputs to the channel specifically, whether it is simply output to the interactive terminal (there is more than one channel).

+4
source

Source: https://habr.com/ru/post/1433479/


All Articles