[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Gettings LINES and COLUMNS from stderr instead of /dev/tty
From: |
Martin Schulte |
Subject: |
Re: Gettings LINES and COLUMNS from stderr instead of /dev/tty |
Date: |
Mon, 18 Jul 2022 22:43:42 +0200 |
Hello Chet,
on 2022-07-18 10:26:05 -0400 you wrote:
> On 7/16/22 11:18 AM, Martin Schulte wrote:
> > Hello,
> >
> > I'm just wondering that bash (reproduced with 5.2-rc1 under Debian 11)
> > seems to determine LINES and COLUMNS from stderr.
>
> ...
> When it's not interactive, all bets are off, and stderr is usually
> a safe default. There's no consistent approach across shells.
At least I lost my bet ;-)
Since I still have the opinion that either stdout or /dev/tty should be asked
to set the variables (because it's the "natural assumption" that you want to
format what you write there) please consider the following script:
#!/bin/bash
shopt -s checkwinsize
( : ) # set LINES/COLUMNS for first time
while true; do
if ((EPOCHSECONDS%86400==11655)); then
printf -v msg '%S' "It's pi o'clock UTC"
else
printf -v msg '%(%T)T' $EPOCHSECONDS
fi
tput clear ; tput cup 0 $(((COLUMNS-${#msg})/2)) ; echo "$msg"
sleep 1
done
It fails with a "bash: printf: `S': invalid format character" once a day but
I've no chance to catch this error - either it is cleared on the screen or
logging stderr breaks the output.
BTW: tput first tries stderr, then stdout, then stdin, then /dev/tty.
Best regards
Martin