[Gambas-user] auto detect pipe |

BB adamnt42 at gmail.com
Wed Nov 1 14:16:11 CET 2023


On 1/11/23 11:05 pm, Bruce Steers wrote:
> i have made a command that supports piping using Application_Read()
>
> Static Public Sub Application_Read()
>
>   $bReading = True
>
>   While Lof(Last)
>     $aArgs[0] = Last.ReadLine()
>     ProcessString()
>     If Not Eof(Last) And If Not $bNewLine Then Print ""
>   Wend
>
>   $bReading = False
>
> End
>
> it works but i have added an arg -p that if i use -p then the pipe is 
> read.
> Eg.
> echo "This text" | mycommand -p
>
> can i detect if a pipe is to be used or not automatically?
>
> Respects
> BruceS
>
My rusting brain tells me that I doubt you have much joy, as pipes are 
entirely the matter of the shell. I did find this though, on stackoverflow

In a pure POSIX shell,

|if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi |

returns "terminal", because the output is sent to your terminal, whereas

|(if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi) | cat |

returns "not a terminal", because the output of the parenthetic element 
is piped to |cat|.

------------------------------------------------------------------------

The |-t| flag is described in man pages as

    -t fd True if file descriptor fd is open and refers to a terminal.

... where |fd| can be one of the usual file descriptor assignments:

  * 0: standard input
    <https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)>
  * 1: standard output
    <https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)>
  * 2: standard error
    <https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)>

b

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20231101/ac0e43a1/attachment.htm>


More information about the User mailing list