Hello everyone,
My quest for the answer to "Can I determine if my stdout redirected"
lead me to /proc. Is there some good documentation of this FS? I'm
wondering about use cases for the various information exported.
For the answer, in case people wonder, here it is:
"""
[EMAIL PROTECTED]:/tmp/testbash$ cat fd_report.sh
#!/bin/bash
declare STDOUT="$(readlink /proc/$$/fd/1)"
# Redirect stdout to stderr for the whole run.
# (redirecting is fine but we still need to report status to user).
exec >&2
if [ -f "$STDOUT" ]; then
echo "My stdout is redirected to: $STDOUT"
else
echo "Running normal, stdout is at: $STDOUT"
fi
file "$STDOUT"
"""
Example run:
"""
[EMAIL PROTECTED]:/tmp/testbash$ ./fd_report.sh
Running normal, stdout is at: /dev/pts/3
/dev/pts/3: character special (136/3)
[EMAIL PROTECTED]:/tmp/testbash$ ./fd_report.sh >/tmp/file
My stdout is redirected to: /tmp/file
/tmp/file: empty
"""
Please note that this will not work for when stdout is redirected to
other device then a "file" (char device, fifo and co.)
"""
[EMAIL PROTECTED]:/tmp/testbash$ ./fd_report.sh >/dev/null
Running normal, stdout is at: /dev/null
/dev/null: character special (1/3)
"""
I would like to react differently if my output is redirected to stdout
or to a file;
I can think of several ways that might give leads to someone but none
that actually seems "elegent", to but none
Reading stdout redirection target from bash scripts ?
--
Cheers,
Maxim Veksler
"Free as in Freedom" - Do u GNU ?
=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]