Skip to content

Instantly share code, notes, and snippets.

@balupton
Created August 30, 2023 13:24
Show Gist options
  • Save balupton/6eee015345c663d7d7baf83d8e20ce1f to your computer and use it in GitHub Desktop.
Save balupton/6eee015345c663d7d7baf83d8e20ce1f to your computer and use it in GitHub Desktop.
Bash Input Output Testing
# test input/output availability, accurately but noisely
(: </dev/stdin || printf '%s\n' 'cannot read stdin') || :
(: >/dev/stdin || printf '%s\n' 'cannot write stdin') || :
(: </dev/stdout || printf '%s\n' 'cannot read stdout') || :
(: >/dev/stdout || printf '%s\n' 'cannot write stdout') || :
(: </dev/stderr || printf '%s\n' 'cannot read stderr') || :
(: >/dev/stderr || printf '%s\n' 'cannot write stderr') || :
(: </dev/tty || printf '%s\n' 'cannot read tty') || :
(: >/dev/tty || printf '%s\n' 'cannot write tty') || :
# test input/output availability, silently with &>/dev/null, but this causes stdout and stderr to be closed for reading
if ! (: </dev/stdin) &>/dev/null; then printf '%s\n' 'cannot silently read stdin'; fi
if ! (: >/dev/stdin) &>/dev/null; then printf '%s\n' 'cannot silently write stdin'; fi
if ! (: </dev/stdout) &>/dev/null; then printf '%s\n' 'cannot silently read stdout'; fi
if ! (: >/dev/stdout) &>/dev/null; then printf '%s\n' 'cannot silently write stdout'; fi
if ! (: </dev/stderr) &>/dev/null; then printf '%s\n' 'cannot silently read stderr'; fi
if ! (: >/dev/stderr) &>/dev/null; then printf '%s\n' 'cannot silently write stderr'; fi
if ! (: </dev/tty) &>/dev/null; then printf '%s\n' 'cannot silently read tty'; fi
if ! (: >/dev/tty) &>/dev/null; then printf '%s\n' 'cannot silently write tty'; fi
# see is-tty --test for a full fledged test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment