On Linux, is there a way for a shell script to check if its standard input is redirected from the null device (1, 3) *, ideally without reading anything?
The expected behavior would be:
./checkstdinnull
-> no
./checkstdinnull < /dev/null
-> yes
echo -n | ./checkstdinnull
-> no
EDIT
mknod secretunknownname c 1 3
exec 6<secretunknownname
rm secretunknownname
./checkstdinnull <&6
-> yes
I suspect I "just" need to read the maj/min number of the input device. But I can't find a way of doing that from the shell.
*No necessary just
/dev/null
, but any null device even if manually created with mknod
.
/dev/null
, or just that it's not a tty?{ readlink -f /dev/stdin; } <&6
for the case where you used exec and removed the node is/root/secretunknownname (deleted)
. As it shows that the file got deleted: Isn't that enough for what you need?stat
solution is the only one working./dev/null
, but not necessary. You can "alias" is withmknod
s illustrated in my example.