Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detecting if std{in,out} is being futzed with #133

Open
Notgnoshi opened this issue Dec 27, 2022 · 0 comments
Open

Detecting if std{in,out} is being futzed with #133

Notgnoshi opened this issue Dec 27, 2022 · 0 comments
Labels
bash enhancement Improve or enhance existing content or functionality linux

Comments

@Notgnoshi
Copy link
Owner

Notgnoshi commented Dec 27, 2022

if [[ ! -t 1 ]]; then
    echo "STDOUT not attached to a TTY" >&2
fi

# if [[ -p /dev/stdout ]]; then
#     echo "STDOUT attached to a pipe" >&2
# fi

# if [[ ! -t 1 && ! -p /dev/stdout ]]; then
#     echo "STDOUT attached to a redirection" >&2
# fi

if [[ ! -t 0 ]]; then
    echo "STDIN not attached to a TTY" >&2
fi

# TODO: Hangs if stdin is empty?
# echo -e "\tifne"
# ifne -n echo "STDIN empty"

combined with

echo "============================================================"
echo "No plumbing"
echo "============================================================"
./stdio.sh

echo "============================================================"
echo "Pipe to stdin"
echo "============================================================"
echo "foo from stdin" | ./stdio.sh

echo "============================================================"
echo "Heredoc to stdin"
echo "============================================================"
./stdio.sh <<EOF
    foo from heredoc
EOF

echo "============================================================"
echo "Pipe from stdout"
echo "============================================================"
./stdio.sh | cat

echo "============================================================"
echo "Redirect from stdout"
echo "============================================================"
./stdio.sh >/dev/null

echo "============================================================"
echo "Redirect from stdout to stdout"
echo "============================================================"
./stdio.sh >/dev/stdout
./stdio.sh >/dev/tty

gives

============================================================
No plumbing
============================================================
============================================================
Pipe to stdin
============================================================
STDIN not attached to a TTY
============================================================
Heredoc to stdin
============================================================
STDIN not attached to a TTY
============================================================
Pipe from stdout
============================================================
STDOUT not attached to a TTY
============================================================
Redirect from stdout
============================================================
STDOUT not attached to a TTY
============================================================
Redirect from stdout to stdout
============================================================
@Notgnoshi Notgnoshi converted this from a draft issue Dec 27, 2022
@Notgnoshi Notgnoshi added enhancement Improve or enhance existing content or functionality bash linux labels Dec 27, 2022
@Notgnoshi Notgnoshi moved this from Post Improvements to New Posts in Website Jun 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bash enhancement Improve or enhance existing content or functionality linux
Projects
Status: New Posts
Development

No branches or pull requests

1 participant