How to check if stdin is /dev/null from the shell?
up vote
8
down vote
favorite
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
.linux shell devices stdin
|
show 1 more comment
up vote
8
down vote
favorite
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
.linux shell devices stdin
2
Do you need to know if it's/dev/null
, or just that it's not a tty?
– roaima
Nov 26 at 15:05
The output of{ 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?
– Isaac
Nov 26 at 17:11
I need (actually "needed") to know if the standard input was the null device.
– Sylvain Leroux
Nov 26 at 17:19
2
I am trying to find my way in a (very poorly designed!) industrial system. And it sometimes maps input of worker utility to the null device, or, some other times, to the actual hardware device. In both cases using the same code, but with different major/minor dev numbers. We are trying to track when (and why!) it sometimes chose to use one or the other. For now, thestat
solution is the only one working.
– Sylvain Leroux
Nov 26 at 17:23
"So, the example on your EDIT doesn't really reproduce the problem that you describe, or: does it?" It does. Input in redirected from the null device. Which is usually accessed through/dev/null
, but not necessary. You can "alias" is withmknod
s illustrated in my example.
– Sylvain Leroux
Nov 26 at 17:33
|
show 1 more comment
up vote
8
down vote
favorite
up vote
8
down vote
favorite
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
.linux shell devices stdin
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
.linux shell devices stdin
linux shell devices stdin
edited Nov 26 at 15:59
asked Nov 26 at 15:02
Sylvain Leroux
421211
421211
2
Do you need to know if it's/dev/null
, or just that it's not a tty?
– roaima
Nov 26 at 15:05
The output of{ 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?
– Isaac
Nov 26 at 17:11
I need (actually "needed") to know if the standard input was the null device.
– Sylvain Leroux
Nov 26 at 17:19
2
I am trying to find my way in a (very poorly designed!) industrial system. And it sometimes maps input of worker utility to the null device, or, some other times, to the actual hardware device. In both cases using the same code, but with different major/minor dev numbers. We are trying to track when (and why!) it sometimes chose to use one or the other. For now, thestat
solution is the only one working.
– Sylvain Leroux
Nov 26 at 17:23
"So, the example on your EDIT doesn't really reproduce the problem that you describe, or: does it?" It does. Input in redirected from the null device. Which is usually accessed through/dev/null
, but not necessary. You can "alias" is withmknod
s illustrated in my example.
– Sylvain Leroux
Nov 26 at 17:33
|
show 1 more comment
2
Do you need to know if it's/dev/null
, or just that it's not a tty?
– roaima
Nov 26 at 15:05
The output of{ 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?
– Isaac
Nov 26 at 17:11
I need (actually "needed") to know if the standard input was the null device.
– Sylvain Leroux
Nov 26 at 17:19
2
I am trying to find my way in a (very poorly designed!) industrial system. And it sometimes maps input of worker utility to the null device, or, some other times, to the actual hardware device. In both cases using the same code, but with different major/minor dev numbers. We are trying to track when (and why!) it sometimes chose to use one or the other. For now, thestat
solution is the only one working.
– Sylvain Leroux
Nov 26 at 17:23
"So, the example on your EDIT doesn't really reproduce the problem that you describe, or: does it?" It does. Input in redirected from the null device. Which is usually accessed through/dev/null
, but not necessary. You can "alias" is withmknod
s illustrated in my example.
– Sylvain Leroux
Nov 26 at 17:33
2
2
Do you need to know if it's
/dev/null
, or just that it's not a tty?– roaima
Nov 26 at 15:05
Do you need to know if it's
/dev/null
, or just that it's not a tty?– roaima
Nov 26 at 15:05
The output of
{ 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?– Isaac
Nov 26 at 17:11
The output of
{ 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?– Isaac
Nov 26 at 17:11
I need (actually "needed") to know if the standard input was the null device.
– Sylvain Leroux
Nov 26 at 17:19
I need (actually "needed") to know if the standard input was the null device.
– Sylvain Leroux
Nov 26 at 17:19
2
2
I am trying to find my way in a (very poorly designed!) industrial system. And it sometimes maps input of worker utility to the null device, or, some other times, to the actual hardware device. In both cases using the same code, but with different major/minor dev numbers. We are trying to track when (and why!) it sometimes chose to use one or the other. For now, the
stat
solution is the only one working.– Sylvain Leroux
Nov 26 at 17:23
I am trying to find my way in a (very poorly designed!) industrial system. And it sometimes maps input of worker utility to the null device, or, some other times, to the actual hardware device. In both cases using the same code, but with different major/minor dev numbers. We are trying to track when (and why!) it sometimes chose to use one or the other. For now, the
stat
solution is the only one working.– Sylvain Leroux
Nov 26 at 17:23
"So, the example on your EDIT doesn't really reproduce the problem that you describe, or: does it?" It does. Input in redirected from the null device. Which is usually accessed through
/dev/null
, but not necessary. You can "alias" is with mknod
s illustrated in my example.– Sylvain Leroux
Nov 26 at 17:33
"So, the example on your EDIT doesn't really reproduce the problem that you describe, or: does it?" It does. Input in redirected from the null device. Which is usually accessed through
/dev/null
, but not necessary. You can "alias" is with mknod
s illustrated in my example.– Sylvain Leroux
Nov 26 at 17:33
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
18
down vote
accepted
On linux, you can do it with:
stdin_is_dev_null(){ test "`stat -Lc %t:%T /dev/stdin`" = "`stat -Lc %t:%T /dev/null`"; }
On a linux without stat(1) (eg. the busybox on your router):
stdin_is_dev_null(){ ls -Ll /proc/self/fd/0 | grep -q ' 1, *3 '; }
On *bsd:
stdin_is_dev_null(){ test "`stat -f %Z`" = "`stat -Lf %Z /dev/null`"; }
On systems like *bsd and solaris, /dev/stdin
, /dev/fd/0
and /proc/PID/fd/0
are not "magical" symlinks as on linux, but character devices which will switch to the real file when opened. A stat(2) on their path will return something different than a fstat(2) on the opened file descriptor.
This means that the linux example will not work there, even with GNU coreutils installed. If the versions of GNU stat(1) is recent enough, you can use the -
argument to let it do a fstat(2) on the file descriptor 0, just like the stat(1) from *bsd:
stdin_is_dev_null(){ test "`stat -Lc %t:%T -`" = "`stat -Lc %t:%T /dev/null`"; }
It's also very easy to do the check portably in any language which offers an interface to fstat(2), eg. in perl
:
stdin_is_dev_null(){ perl -e 'exit((stat STDIN)[6]!=(stat "/dev/null")[6])'; }
1
As the device type of/dev/null
is1:3
, you could test for that immediately.
– RudiC
Nov 26 at 15:55
12
FWIW the question was not about shell syntax. For what is concerning my issue, if the answer had only pointed me to thestat
command I would have already been quite satisfied. I don't see the point in starting an editing war between`
and$(
supporters. Personally, I prefer$(...)
and there are some POSIX rationales in favor of that syntax (pubs.opengroup.org/onlinepubs/9699919799/xrat/… ) But I don't see the point in downvoting an otherwise correct answer for something not related with the question.
– Sylvain Leroux
Nov 26 at 17:06
2
Could someone explain why$( ... )
is preferred over backticks in the context of this answer?
– user1717828
Nov 26 at 21:35
"what bug does that fix"? It doesn't fix a bug. The$(..)
style nests more easily and AIUI is the POSIX preferred approach. I certainly hadn't intended to start any form of editing war, preferring to comment with a suggestion rather than change your excellent answer.
– roaima
Nov 27 at 9:03
@user1717828 see here and here and here for some clarity.
– roaima
Nov 27 at 9:07
add a comment |
up vote
15
down vote
On Linux, to determine whether standard input is redirected from /dev/null
, you can check whether /proc/self/fd/0
has the same device and inode as /dev/null
:
if [ /proc/self/fd/0 -ef /dev/null ]; then echo yes; else echo no; fi
You can use /dev/stdin
instead of /proc/self/fd/0
.
If you want to check whether standard input is redirected from the null device, you need to compare major and minor device numbers, for example using stat
(see also mosvy’s answer):
if [ "$(stat -Lc %t:%T /dev/stdin)" = "$(stat -Lc %t:%T /dev/null)" ]; then echo yes; else echo no; fi
or, if you don’t care about this being Linux-specific,
if [ "$(stat -Lc %t:%T /dev/stdin)" = "1:3" ]; then echo yes; else echo no; fi
2
-ef, True if FILE1 and FILE2 refer to the same device and inode
– Rui F Ribeiro
Nov 26 at 15:13
very cool.bash -c 'ls -l /proc/self/fd/0 /dev/null; [[ /proc/self/fd/0 -ef /dev/null ]] && echo dev null'
then do that again with</dev/null
. +1
– glenn jackman
Nov 26 at 17:20
1
The/dev/stdin
being a symlink to the original file is specific to Linux, so all those solutions are Linux-specific anyway. Thestat
-based ones require GNU or busyboxstat
. With recent versions of GNUstat
, you can usestat -
to do afstat()
on fd 0 which would then work on non-Linux systems.
– Stéphane Chazelas
Nov 26 at 18:48
@Stéphane the last part is exactly the situation the OP ran into, which is why I wrote up both solutions, distinguishing between/dev/null
and the null device.
– Stephen Kitt
Nov 26 at 19:25
Oops, missed that.
– Stéphane Chazelas
Nov 26 at 20:50
add a comment |
up vote
3
down vote
Portably, to check that stdin is the null
device (open on /dev/null
or not (like a copy of /dev/null
)), with zsh
(whose stat
builtin predates both GNU and FreeBSD stat
by the way (not IRIX' though))):
zmodload zsh/stat
if [ "$(stat +rdev -f 0)" = "$(stat +rdev /dev/null)" ]; then
echo stdin is open on the null device
fi
(note that it doesn't say if the file descriptor was open in read-only, write-only or read+write mode).
To check that it's open on the current /dev/null
file specifically (not /some/chroot/dev/null
for instance), on Linux only (where /dev/stdin
is implemented as a symlink to the file open on fd 0 instead of a special device which when open acts like a dup(0)
in other systems):
if [ /dev/stdin -ef /dev/null ]; then
echo stdin is open on /dev/null
fi
On non-Linux, you can try:
if sh -c 'lsof -tad0 -p"$$" /dev/null' > /dev/null 2>&-; then
echo stdin is open on /dev/null
fi
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
18
down vote
accepted
On linux, you can do it with:
stdin_is_dev_null(){ test "`stat -Lc %t:%T /dev/stdin`" = "`stat -Lc %t:%T /dev/null`"; }
On a linux without stat(1) (eg. the busybox on your router):
stdin_is_dev_null(){ ls -Ll /proc/self/fd/0 | grep -q ' 1, *3 '; }
On *bsd:
stdin_is_dev_null(){ test "`stat -f %Z`" = "`stat -Lf %Z /dev/null`"; }
On systems like *bsd and solaris, /dev/stdin
, /dev/fd/0
and /proc/PID/fd/0
are not "magical" symlinks as on linux, but character devices which will switch to the real file when opened. A stat(2) on their path will return something different than a fstat(2) on the opened file descriptor.
This means that the linux example will not work there, even with GNU coreutils installed. If the versions of GNU stat(1) is recent enough, you can use the -
argument to let it do a fstat(2) on the file descriptor 0, just like the stat(1) from *bsd:
stdin_is_dev_null(){ test "`stat -Lc %t:%T -`" = "`stat -Lc %t:%T /dev/null`"; }
It's also very easy to do the check portably in any language which offers an interface to fstat(2), eg. in perl
:
stdin_is_dev_null(){ perl -e 'exit((stat STDIN)[6]!=(stat "/dev/null")[6])'; }
1
As the device type of/dev/null
is1:3
, you could test for that immediately.
– RudiC
Nov 26 at 15:55
12
FWIW the question was not about shell syntax. For what is concerning my issue, if the answer had only pointed me to thestat
command I would have already been quite satisfied. I don't see the point in starting an editing war between`
and$(
supporters. Personally, I prefer$(...)
and there are some POSIX rationales in favor of that syntax (pubs.opengroup.org/onlinepubs/9699919799/xrat/… ) But I don't see the point in downvoting an otherwise correct answer for something not related with the question.
– Sylvain Leroux
Nov 26 at 17:06
2
Could someone explain why$( ... )
is preferred over backticks in the context of this answer?
– user1717828
Nov 26 at 21:35
"what bug does that fix"? It doesn't fix a bug. The$(..)
style nests more easily and AIUI is the POSIX preferred approach. I certainly hadn't intended to start any form of editing war, preferring to comment with a suggestion rather than change your excellent answer.
– roaima
Nov 27 at 9:03
@user1717828 see here and here and here for some clarity.
– roaima
Nov 27 at 9:07
add a comment |
up vote
18
down vote
accepted
On linux, you can do it with:
stdin_is_dev_null(){ test "`stat -Lc %t:%T /dev/stdin`" = "`stat -Lc %t:%T /dev/null`"; }
On a linux without stat(1) (eg. the busybox on your router):
stdin_is_dev_null(){ ls -Ll /proc/self/fd/0 | grep -q ' 1, *3 '; }
On *bsd:
stdin_is_dev_null(){ test "`stat -f %Z`" = "`stat -Lf %Z /dev/null`"; }
On systems like *bsd and solaris, /dev/stdin
, /dev/fd/0
and /proc/PID/fd/0
are not "magical" symlinks as on linux, but character devices which will switch to the real file when opened. A stat(2) on their path will return something different than a fstat(2) on the opened file descriptor.
This means that the linux example will not work there, even with GNU coreutils installed. If the versions of GNU stat(1) is recent enough, you can use the -
argument to let it do a fstat(2) on the file descriptor 0, just like the stat(1) from *bsd:
stdin_is_dev_null(){ test "`stat -Lc %t:%T -`" = "`stat -Lc %t:%T /dev/null`"; }
It's also very easy to do the check portably in any language which offers an interface to fstat(2), eg. in perl
:
stdin_is_dev_null(){ perl -e 'exit((stat STDIN)[6]!=(stat "/dev/null")[6])'; }
1
As the device type of/dev/null
is1:3
, you could test for that immediately.
– RudiC
Nov 26 at 15:55
12
FWIW the question was not about shell syntax. For what is concerning my issue, if the answer had only pointed me to thestat
command I would have already been quite satisfied. I don't see the point in starting an editing war between`
and$(
supporters. Personally, I prefer$(...)
and there are some POSIX rationales in favor of that syntax (pubs.opengroup.org/onlinepubs/9699919799/xrat/… ) But I don't see the point in downvoting an otherwise correct answer for something not related with the question.
– Sylvain Leroux
Nov 26 at 17:06
2
Could someone explain why$( ... )
is preferred over backticks in the context of this answer?
– user1717828
Nov 26 at 21:35
"what bug does that fix"? It doesn't fix a bug. The$(..)
style nests more easily and AIUI is the POSIX preferred approach. I certainly hadn't intended to start any form of editing war, preferring to comment with a suggestion rather than change your excellent answer.
– roaima
Nov 27 at 9:03
@user1717828 see here and here and here for some clarity.
– roaima
Nov 27 at 9:07
add a comment |
up vote
18
down vote
accepted
up vote
18
down vote
accepted
On linux, you can do it with:
stdin_is_dev_null(){ test "`stat -Lc %t:%T /dev/stdin`" = "`stat -Lc %t:%T /dev/null`"; }
On a linux without stat(1) (eg. the busybox on your router):
stdin_is_dev_null(){ ls -Ll /proc/self/fd/0 | grep -q ' 1, *3 '; }
On *bsd:
stdin_is_dev_null(){ test "`stat -f %Z`" = "`stat -Lf %Z /dev/null`"; }
On systems like *bsd and solaris, /dev/stdin
, /dev/fd/0
and /proc/PID/fd/0
are not "magical" symlinks as on linux, but character devices which will switch to the real file when opened. A stat(2) on their path will return something different than a fstat(2) on the opened file descriptor.
This means that the linux example will not work there, even with GNU coreutils installed. If the versions of GNU stat(1) is recent enough, you can use the -
argument to let it do a fstat(2) on the file descriptor 0, just like the stat(1) from *bsd:
stdin_is_dev_null(){ test "`stat -Lc %t:%T -`" = "`stat -Lc %t:%T /dev/null`"; }
It's also very easy to do the check portably in any language which offers an interface to fstat(2), eg. in perl
:
stdin_is_dev_null(){ perl -e 'exit((stat STDIN)[6]!=(stat "/dev/null")[6])'; }
On linux, you can do it with:
stdin_is_dev_null(){ test "`stat -Lc %t:%T /dev/stdin`" = "`stat -Lc %t:%T /dev/null`"; }
On a linux without stat(1) (eg. the busybox on your router):
stdin_is_dev_null(){ ls -Ll /proc/self/fd/0 | grep -q ' 1, *3 '; }
On *bsd:
stdin_is_dev_null(){ test "`stat -f %Z`" = "`stat -Lf %Z /dev/null`"; }
On systems like *bsd and solaris, /dev/stdin
, /dev/fd/0
and /proc/PID/fd/0
are not "magical" symlinks as on linux, but character devices which will switch to the real file when opened. A stat(2) on their path will return something different than a fstat(2) on the opened file descriptor.
This means that the linux example will not work there, even with GNU coreutils installed. If the versions of GNU stat(1) is recent enough, you can use the -
argument to let it do a fstat(2) on the file descriptor 0, just like the stat(1) from *bsd:
stdin_is_dev_null(){ test "`stat -Lc %t:%T -`" = "`stat -Lc %t:%T /dev/null`"; }
It's also very easy to do the check portably in any language which offers an interface to fstat(2), eg. in perl
:
stdin_is_dev_null(){ perl -e 'exit((stat STDIN)[6]!=(stat "/dev/null")[6])'; }
edited Nov 27 at 1:07
answered Nov 26 at 15:32
mosvy
5,1991323
5,1991323
1
As the device type of/dev/null
is1:3
, you could test for that immediately.
– RudiC
Nov 26 at 15:55
12
FWIW the question was not about shell syntax. For what is concerning my issue, if the answer had only pointed me to thestat
command I would have already been quite satisfied. I don't see the point in starting an editing war between`
and$(
supporters. Personally, I prefer$(...)
and there are some POSIX rationales in favor of that syntax (pubs.opengroup.org/onlinepubs/9699919799/xrat/… ) But I don't see the point in downvoting an otherwise correct answer for something not related with the question.
– Sylvain Leroux
Nov 26 at 17:06
2
Could someone explain why$( ... )
is preferred over backticks in the context of this answer?
– user1717828
Nov 26 at 21:35
"what bug does that fix"? It doesn't fix a bug. The$(..)
style nests more easily and AIUI is the POSIX preferred approach. I certainly hadn't intended to start any form of editing war, preferring to comment with a suggestion rather than change your excellent answer.
– roaima
Nov 27 at 9:03
@user1717828 see here and here and here for some clarity.
– roaima
Nov 27 at 9:07
add a comment |
1
As the device type of/dev/null
is1:3
, you could test for that immediately.
– RudiC
Nov 26 at 15:55
12
FWIW the question was not about shell syntax. For what is concerning my issue, if the answer had only pointed me to thestat
command I would have already been quite satisfied. I don't see the point in starting an editing war between`
and$(
supporters. Personally, I prefer$(...)
and there are some POSIX rationales in favor of that syntax (pubs.opengroup.org/onlinepubs/9699919799/xrat/… ) But I don't see the point in downvoting an otherwise correct answer for something not related with the question.
– Sylvain Leroux
Nov 26 at 17:06
2
Could someone explain why$( ... )
is preferred over backticks in the context of this answer?
– user1717828
Nov 26 at 21:35
"what bug does that fix"? It doesn't fix a bug. The$(..)
style nests more easily and AIUI is the POSIX preferred approach. I certainly hadn't intended to start any form of editing war, preferring to comment with a suggestion rather than change your excellent answer.
– roaima
Nov 27 at 9:03
@user1717828 see here and here and here for some clarity.
– roaima
Nov 27 at 9:07
1
1
As the device type of
/dev/null
is 1:3
, you could test for that immediately.– RudiC
Nov 26 at 15:55
As the device type of
/dev/null
is 1:3
, you could test for that immediately.– RudiC
Nov 26 at 15:55
12
12
FWIW the question was not about shell syntax. For what is concerning my issue, if the answer had only pointed me to the
stat
command I would have already been quite satisfied. I don't see the point in starting an editing war between `
and $(
supporters. Personally, I prefer $(...)
and there are some POSIX rationales in favor of that syntax (pubs.opengroup.org/onlinepubs/9699919799/xrat/… ) But I don't see the point in downvoting an otherwise correct answer for something not related with the question.– Sylvain Leroux
Nov 26 at 17:06
FWIW the question was not about shell syntax. For what is concerning my issue, if the answer had only pointed me to the
stat
command I would have already been quite satisfied. I don't see the point in starting an editing war between `
and $(
supporters. Personally, I prefer $(...)
and there are some POSIX rationales in favor of that syntax (pubs.opengroup.org/onlinepubs/9699919799/xrat/… ) But I don't see the point in downvoting an otherwise correct answer for something not related with the question.– Sylvain Leroux
Nov 26 at 17:06
2
2
Could someone explain why
$( ... )
is preferred over backticks in the context of this answer?– user1717828
Nov 26 at 21:35
Could someone explain why
$( ... )
is preferred over backticks in the context of this answer?– user1717828
Nov 26 at 21:35
"what bug does that fix"? It doesn't fix a bug. The
$(..)
style nests more easily and AIUI is the POSIX preferred approach. I certainly hadn't intended to start any form of editing war, preferring to comment with a suggestion rather than change your excellent answer.– roaima
Nov 27 at 9:03
"what bug does that fix"? It doesn't fix a bug. The
$(..)
style nests more easily and AIUI is the POSIX preferred approach. I certainly hadn't intended to start any form of editing war, preferring to comment with a suggestion rather than change your excellent answer.– roaima
Nov 27 at 9:03
@user1717828 see here and here and here for some clarity.
– roaima
Nov 27 at 9:07
@user1717828 see here and here and here for some clarity.
– roaima
Nov 27 at 9:07
add a comment |
up vote
15
down vote
On Linux, to determine whether standard input is redirected from /dev/null
, you can check whether /proc/self/fd/0
has the same device and inode as /dev/null
:
if [ /proc/self/fd/0 -ef /dev/null ]; then echo yes; else echo no; fi
You can use /dev/stdin
instead of /proc/self/fd/0
.
If you want to check whether standard input is redirected from the null device, you need to compare major and minor device numbers, for example using stat
(see also mosvy’s answer):
if [ "$(stat -Lc %t:%T /dev/stdin)" = "$(stat -Lc %t:%T /dev/null)" ]; then echo yes; else echo no; fi
or, if you don’t care about this being Linux-specific,
if [ "$(stat -Lc %t:%T /dev/stdin)" = "1:3" ]; then echo yes; else echo no; fi
2
-ef, True if FILE1 and FILE2 refer to the same device and inode
– Rui F Ribeiro
Nov 26 at 15:13
very cool.bash -c 'ls -l /proc/self/fd/0 /dev/null; [[ /proc/self/fd/0 -ef /dev/null ]] && echo dev null'
then do that again with</dev/null
. +1
– glenn jackman
Nov 26 at 17:20
1
The/dev/stdin
being a symlink to the original file is specific to Linux, so all those solutions are Linux-specific anyway. Thestat
-based ones require GNU or busyboxstat
. With recent versions of GNUstat
, you can usestat -
to do afstat()
on fd 0 which would then work on non-Linux systems.
– Stéphane Chazelas
Nov 26 at 18:48
@Stéphane the last part is exactly the situation the OP ran into, which is why I wrote up both solutions, distinguishing between/dev/null
and the null device.
– Stephen Kitt
Nov 26 at 19:25
Oops, missed that.
– Stéphane Chazelas
Nov 26 at 20:50
add a comment |
up vote
15
down vote
On Linux, to determine whether standard input is redirected from /dev/null
, you can check whether /proc/self/fd/0
has the same device and inode as /dev/null
:
if [ /proc/self/fd/0 -ef /dev/null ]; then echo yes; else echo no; fi
You can use /dev/stdin
instead of /proc/self/fd/0
.
If you want to check whether standard input is redirected from the null device, you need to compare major and minor device numbers, for example using stat
(see also mosvy’s answer):
if [ "$(stat -Lc %t:%T /dev/stdin)" = "$(stat -Lc %t:%T /dev/null)" ]; then echo yes; else echo no; fi
or, if you don’t care about this being Linux-specific,
if [ "$(stat -Lc %t:%T /dev/stdin)" = "1:3" ]; then echo yes; else echo no; fi
2
-ef, True if FILE1 and FILE2 refer to the same device and inode
– Rui F Ribeiro
Nov 26 at 15:13
very cool.bash -c 'ls -l /proc/self/fd/0 /dev/null; [[ /proc/self/fd/0 -ef /dev/null ]] && echo dev null'
then do that again with</dev/null
. +1
– glenn jackman
Nov 26 at 17:20
1
The/dev/stdin
being a symlink to the original file is specific to Linux, so all those solutions are Linux-specific anyway. Thestat
-based ones require GNU or busyboxstat
. With recent versions of GNUstat
, you can usestat -
to do afstat()
on fd 0 which would then work on non-Linux systems.
– Stéphane Chazelas
Nov 26 at 18:48
@Stéphane the last part is exactly the situation the OP ran into, which is why I wrote up both solutions, distinguishing between/dev/null
and the null device.
– Stephen Kitt
Nov 26 at 19:25
Oops, missed that.
– Stéphane Chazelas
Nov 26 at 20:50
add a comment |
up vote
15
down vote
up vote
15
down vote
On Linux, to determine whether standard input is redirected from /dev/null
, you can check whether /proc/self/fd/0
has the same device and inode as /dev/null
:
if [ /proc/self/fd/0 -ef /dev/null ]; then echo yes; else echo no; fi
You can use /dev/stdin
instead of /proc/self/fd/0
.
If you want to check whether standard input is redirected from the null device, you need to compare major and minor device numbers, for example using stat
(see also mosvy’s answer):
if [ "$(stat -Lc %t:%T /dev/stdin)" = "$(stat -Lc %t:%T /dev/null)" ]; then echo yes; else echo no; fi
or, if you don’t care about this being Linux-specific,
if [ "$(stat -Lc %t:%T /dev/stdin)" = "1:3" ]; then echo yes; else echo no; fi
On Linux, to determine whether standard input is redirected from /dev/null
, you can check whether /proc/self/fd/0
has the same device and inode as /dev/null
:
if [ /proc/self/fd/0 -ef /dev/null ]; then echo yes; else echo no; fi
You can use /dev/stdin
instead of /proc/self/fd/0
.
If you want to check whether standard input is redirected from the null device, you need to compare major and minor device numbers, for example using stat
(see also mosvy’s answer):
if [ "$(stat -Lc %t:%T /dev/stdin)" = "$(stat -Lc %t:%T /dev/null)" ]; then echo yes; else echo no; fi
or, if you don’t care about this being Linux-specific,
if [ "$(stat -Lc %t:%T /dev/stdin)" = "1:3" ]; then echo yes; else echo no; fi
edited Nov 26 at 22:03
answered Nov 26 at 15:08
Stephen Kitt
161k24357433
161k24357433
2
-ef, True if FILE1 and FILE2 refer to the same device and inode
– Rui F Ribeiro
Nov 26 at 15:13
very cool.bash -c 'ls -l /proc/self/fd/0 /dev/null; [[ /proc/self/fd/0 -ef /dev/null ]] && echo dev null'
then do that again with</dev/null
. +1
– glenn jackman
Nov 26 at 17:20
1
The/dev/stdin
being a symlink to the original file is specific to Linux, so all those solutions are Linux-specific anyway. Thestat
-based ones require GNU or busyboxstat
. With recent versions of GNUstat
, you can usestat -
to do afstat()
on fd 0 which would then work on non-Linux systems.
– Stéphane Chazelas
Nov 26 at 18:48
@Stéphane the last part is exactly the situation the OP ran into, which is why I wrote up both solutions, distinguishing between/dev/null
and the null device.
– Stephen Kitt
Nov 26 at 19:25
Oops, missed that.
– Stéphane Chazelas
Nov 26 at 20:50
add a comment |
2
-ef, True if FILE1 and FILE2 refer to the same device and inode
– Rui F Ribeiro
Nov 26 at 15:13
very cool.bash -c 'ls -l /proc/self/fd/0 /dev/null; [[ /proc/self/fd/0 -ef /dev/null ]] && echo dev null'
then do that again with</dev/null
. +1
– glenn jackman
Nov 26 at 17:20
1
The/dev/stdin
being a symlink to the original file is specific to Linux, so all those solutions are Linux-specific anyway. Thestat
-based ones require GNU or busyboxstat
. With recent versions of GNUstat
, you can usestat -
to do afstat()
on fd 0 which would then work on non-Linux systems.
– Stéphane Chazelas
Nov 26 at 18:48
@Stéphane the last part is exactly the situation the OP ran into, which is why I wrote up both solutions, distinguishing between/dev/null
and the null device.
– Stephen Kitt
Nov 26 at 19:25
Oops, missed that.
– Stéphane Chazelas
Nov 26 at 20:50
2
2
-ef, True if FILE1 and FILE2 refer to the same device and inode
– Rui F Ribeiro
Nov 26 at 15:13
-ef, True if FILE1 and FILE2 refer to the same device and inode
– Rui F Ribeiro
Nov 26 at 15:13
very cool.
bash -c 'ls -l /proc/self/fd/0 /dev/null; [[ /proc/self/fd/0 -ef /dev/null ]] && echo dev null'
then do that again with </dev/null
. +1– glenn jackman
Nov 26 at 17:20
very cool.
bash -c 'ls -l /proc/self/fd/0 /dev/null; [[ /proc/self/fd/0 -ef /dev/null ]] && echo dev null'
then do that again with </dev/null
. +1– glenn jackman
Nov 26 at 17:20
1
1
The
/dev/stdin
being a symlink to the original file is specific to Linux, so all those solutions are Linux-specific anyway. The stat
-based ones require GNU or busybox stat
. With recent versions of GNU stat
, you can use stat -
to do a fstat()
on fd 0 which would then work on non-Linux systems.– Stéphane Chazelas
Nov 26 at 18:48
The
/dev/stdin
being a symlink to the original file is specific to Linux, so all those solutions are Linux-specific anyway. The stat
-based ones require GNU or busybox stat
. With recent versions of GNU stat
, you can use stat -
to do a fstat()
on fd 0 which would then work on non-Linux systems.– Stéphane Chazelas
Nov 26 at 18:48
@Stéphane the last part is exactly the situation the OP ran into, which is why I wrote up both solutions, distinguishing between
/dev/null
and the null device.– Stephen Kitt
Nov 26 at 19:25
@Stéphane the last part is exactly the situation the OP ran into, which is why I wrote up both solutions, distinguishing between
/dev/null
and the null device.– Stephen Kitt
Nov 26 at 19:25
Oops, missed that.
– Stéphane Chazelas
Nov 26 at 20:50
Oops, missed that.
– Stéphane Chazelas
Nov 26 at 20:50
add a comment |
up vote
3
down vote
Portably, to check that stdin is the null
device (open on /dev/null
or not (like a copy of /dev/null
)), with zsh
(whose stat
builtin predates both GNU and FreeBSD stat
by the way (not IRIX' though))):
zmodload zsh/stat
if [ "$(stat +rdev -f 0)" = "$(stat +rdev /dev/null)" ]; then
echo stdin is open on the null device
fi
(note that it doesn't say if the file descriptor was open in read-only, write-only or read+write mode).
To check that it's open on the current /dev/null
file specifically (not /some/chroot/dev/null
for instance), on Linux only (where /dev/stdin
is implemented as a symlink to the file open on fd 0 instead of a special device which when open acts like a dup(0)
in other systems):
if [ /dev/stdin -ef /dev/null ]; then
echo stdin is open on /dev/null
fi
On non-Linux, you can try:
if sh -c 'lsof -tad0 -p"$$" /dev/null' > /dev/null 2>&-; then
echo stdin is open on /dev/null
fi
add a comment |
up vote
3
down vote
Portably, to check that stdin is the null
device (open on /dev/null
or not (like a copy of /dev/null
)), with zsh
(whose stat
builtin predates both GNU and FreeBSD stat
by the way (not IRIX' though))):
zmodload zsh/stat
if [ "$(stat +rdev -f 0)" = "$(stat +rdev /dev/null)" ]; then
echo stdin is open on the null device
fi
(note that it doesn't say if the file descriptor was open in read-only, write-only or read+write mode).
To check that it's open on the current /dev/null
file specifically (not /some/chroot/dev/null
for instance), on Linux only (where /dev/stdin
is implemented as a symlink to the file open on fd 0 instead of a special device which when open acts like a dup(0)
in other systems):
if [ /dev/stdin -ef /dev/null ]; then
echo stdin is open on /dev/null
fi
On non-Linux, you can try:
if sh -c 'lsof -tad0 -p"$$" /dev/null' > /dev/null 2>&-; then
echo stdin is open on /dev/null
fi
add a comment |
up vote
3
down vote
up vote
3
down vote
Portably, to check that stdin is the null
device (open on /dev/null
or not (like a copy of /dev/null
)), with zsh
(whose stat
builtin predates both GNU and FreeBSD stat
by the way (not IRIX' though))):
zmodload zsh/stat
if [ "$(stat +rdev -f 0)" = "$(stat +rdev /dev/null)" ]; then
echo stdin is open on the null device
fi
(note that it doesn't say if the file descriptor was open in read-only, write-only or read+write mode).
To check that it's open on the current /dev/null
file specifically (not /some/chroot/dev/null
for instance), on Linux only (where /dev/stdin
is implemented as a symlink to the file open on fd 0 instead of a special device which when open acts like a dup(0)
in other systems):
if [ /dev/stdin -ef /dev/null ]; then
echo stdin is open on /dev/null
fi
On non-Linux, you can try:
if sh -c 'lsof -tad0 -p"$$" /dev/null' > /dev/null 2>&-; then
echo stdin is open on /dev/null
fi
Portably, to check that stdin is the null
device (open on /dev/null
or not (like a copy of /dev/null
)), with zsh
(whose stat
builtin predates both GNU and FreeBSD stat
by the way (not IRIX' though))):
zmodload zsh/stat
if [ "$(stat +rdev -f 0)" = "$(stat +rdev /dev/null)" ]; then
echo stdin is open on the null device
fi
(note that it doesn't say if the file descriptor was open in read-only, write-only or read+write mode).
To check that it's open on the current /dev/null
file specifically (not /some/chroot/dev/null
for instance), on Linux only (where /dev/stdin
is implemented as a symlink to the file open on fd 0 instead of a special device which when open acts like a dup(0)
in other systems):
if [ /dev/stdin -ef /dev/null ]; then
echo stdin is open on /dev/null
fi
On non-Linux, you can try:
if sh -c 'lsof -tad0 -p"$$" /dev/null' > /dev/null 2>&-; then
echo stdin is open on /dev/null
fi
answered Nov 26 at 19:04
Stéphane Chazelas
297k54561907
297k54561907
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484228%2fhow-to-check-if-stdin-is-dev-null-from-the-shell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Do you need to know if it's
/dev/null
, or just that it's not a tty?– roaima
Nov 26 at 15:05
The output of
{ 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?– Isaac
Nov 26 at 17:11
I need (actually "needed") to know if the standard input was the null device.
– Sylvain Leroux
Nov 26 at 17:19
2
I am trying to find my way in a (very poorly designed!) industrial system. And it sometimes maps input of worker utility to the null device, or, some other times, to the actual hardware device. In both cases using the same code, but with different major/minor dev numbers. We are trying to track when (and why!) it sometimes chose to use one or the other. For now, the
stat
solution is the only one working.– Sylvain Leroux
Nov 26 at 17:23
"So, the example on your EDIT doesn't really reproduce the problem that you describe, or: does it?" It does. Input in redirected from the null device. Which is usually accessed through
/dev/null
, but not necessary. You can "alias" is withmknod
s illustrated in my example.– Sylvain Leroux
Nov 26 at 17:33