How can I find out whether my program is running interactively?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I find out whether my program is running interactively?

Dirk Laurie-2
I want my program to print out a message when io.stdout is a terminal,
but not when it is a disk file.  Current solution: write it to
io.stderr, not io.stdout.  Is there a better way?

Dirk

Reply | Threaded
Open this post in threaded view
|

Re: How can I find out whether my program is running interactively?

Rena
On Fri, Nov 4, 2011 at 00:07, Dirk Laurie <[hidden email]> wrote:
> I want my program to print out a message when io.stdout is a terminal,
> but not when it is a disk file.  Current solution: write it to
> io.stderr, not io.stdout.  Is there a better way?
>
> Dirk
>
>

Common solutions include using stderr if stdout is likely not to be a
terminal, or providing an argument to write to a file instead of
stdout (instead of redirecting), or using isatty() to check. I recall
there being a way to check whether your code is running in the Lua
console by checking the call traceback or some such, but I don't think
that's what you mean by running interactively.

--
Sent from my toaster.

Reply | Threaded
Open this post in threaded view
|

Re: How can I find out whether my program is running interactively?

Luiz Henrique de Figueiredo
In reply to this post by Dirk Laurie-2
> I want my program to print out a message when io.stdout is a terminal,
> but not when it is a disk file.  Current solution: write it to
> io.stderr, not io.stdout.  Is there a better way?

Under Unix, you can write to /dev/tty.

Reply | Threaded
Open this post in threaded view
|

Re: How can I find out whether my program is running interactively?

T T-2
On 4 November 2011 09:25, Luiz Henrique de Figueiredo
<[hidden email]> wrote:
>> I want my program to print out a message when io.stdout is a terminal,
>> but not when it is a disk file.  Current solution: write it to
>> io.stderr, not io.stdout.  Is there a better way?
>
> Under Unix, you can write to /dev/tty.

and on win32 to con or conout$.

Reply | Threaded
Open this post in threaded view
|

Re: How can I find out whether my program is running interactively?

Elias Barrionovo

On 4 November 2011 09:25, Luiz Henrique de Figueiredo
<[hidden email]> wrote:
>> I want my program to print out a message when io.stdout is a terminal,
>> but not when it is a disk file.  Current solution: write it to
>> io.stderr, not io.stdout.  Is there a better way?
>
> Under Unix, you can write to /dev/tty.

Wouldn't it remove the user's possibility of doing io redirections (not sure how those are actually called)? Things like ' lua myscript.lua >> mylog.txt ' ?

--
NI!