On Thu, 2 Oct 2025 23:46:30 +0200, Mike Jonkmans wrote:
> > On Tue, 23 Sep 2025 05:07:18 +0200 (CEST), Pourko wrote:
> > > On Thu, 18 Sep 2025 15:26:16 -0400 Chet Ramey wrote:
> > > > There isn't a good way for a shell script to determine whether or not
> > > > it's in the foreground or background, and whether it is in the same
> > > > process group as the terminal.
> > >
> > > Attached is the patch that does it.
> > > [...]
> > > Now...
> > > [ -t 0 ] && [ ! -T 0 ]
> > > ...means we are running in the background.
>
> It is not a common case.
>
> Can't that check be done with something like:
> trap '' SIGTTIN
> if read -rd '' -n 0; then
> echo Foreground
> else
> echo Background
> fi
> trap SIGTTIN
Let me try to explain why your sugestion is not "a good way" for my
partucular use-case:
In my .bashrc I have one function that checks if any keys have been pressed
on the keyboard. If keys have been pressed, my function slurps them all in
in var $KEYS and returns 0. If no keys have been pressed the function
returns 1.
Now, I use that function in all kinds of different scripts, so it is not wise
to mess with traps from inside a function. In this case having [ -T 0 ] would
be very useful.
On Thu, 2 Oct 2025 23:46:30 +0200, Mike Jonkmans wrote:
> > On Tue, 23 Sep 2025 05:07:18 +0200 (CEST), Pourko wrote:
> > > On Thu, 18 Sep 2025 15:26:16 -0400 Chet Ramey wrote:
> > > > There isn't a good way for a shell script to determine whether or not
> > > > it's in the foreground or background, and whether it is in the same
> > > > process group as the terminal.
> > >
> > > Attached is the patch that does it.
> > > [...]
> > > Now...
> > > [ -t 0 ] && [ ! -T 0 ]
> > > ...means we are running in the background.
>
> It is not a common case.
>
> Can't that check be done with something like:
> trap '' SIGTTIN
> if read -rd '' -n 0; then
> echo Foreground
> else
> echo Background
> fi
> trap SIGTTIN
Let me try to explain why your suggestion is not "a good way" for my
particular use-case:
In my .bashrc I have one function that checks if any keys have been pressed
on the keyboard. If keys have been pressed, my function slurps them all in
in var $KEYS and returns 0. If no keys have been pressed the function
returns 1.
Now, I use that function in all kinds of different scripts, so it is not wise
to mess with traps from inside a function. In this case having [ -T 0 ] would
be very useful.