| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
set -ueo pipefail |
| 4 |
|
| 5 |
# Callers can test for support via "with-tty true". |
| 6 |
|
| 7 |
usage() { echo 'Usage: with-tty command [arg ...]'; } |
| 8 |
misuse() { usage 1>&2; exit 2; } |
| 9 |
|
| 10 |
case "$OSTYPE" in |
| 11 |
netbsd) exit 2 ;; # https://gnats.netbsd.org/56254 |
| 12 |
esac |
| 13 |
|
| 14 |
# script(1), originating in 3.0BSD, is not specified by POSIX and has |
| 15 |
# varying forms. |
| 16 |
|
| 17 |
# FreeBSD 15: script [-aeFfkqrw] [-t time] [file [command ...]] |
| 18 |
# FreeBSD 14: script [-aeFfkqr] [-t time] [file [command ...]] |
| 19 |
# FreeBSD 13: script [-aefkqr] [-F pipe] [-t time] [file [command ...]] |
| 20 |
# FreeBSD 12: script [-adfkpqr] [-F pipe] [-t time] [file [command ...]] |
| 21 |
# FreeBSD 11: script [-adfkpqr] [-F pipe] [-t time] [file [command ...]] |
| 22 |
# FreeBSD 10: script [-adfkpqr] [-t time] [file [command ...]] |
| 23 |
# FreeBSD 8: script [-akq] [-t time] [file [command ...]] |
| 24 |
# FreeBSD 3.0: script [-a] [-k] [-q] [-t time] [file] [command ...] |
| 25 |
# FreeBSD 1.0: script [-a] [file] |
| 26 |
|
| 27 |
# Linux ?: script [options] [file] [-- command [argument...]] |
| 28 |
# but also "-c, --command command", and -eq |
| 29 |
|
| 30 |
# macos ?: script [-aeFkqr] [-t time] [file [command ...]] |
| 31 |
|
| 32 |
# NetBSD 10: script [-adefpqr] [-c command] [file] |
| 33 |
# NetBSD 6-9: script [-adfpqr] [-c command] [file] |
| 34 |
# NetBSD 5: script [-adpr] [file] |
| 35 |
|
| 36 |
# Try variants; insist on exit with child status/ (-e behavior). |
| 37 |
|
| 38 |
if script -qec true /dev/null; then |
| 39 |
script -qec "$(printf ' %q' "$@")" /dev/null |
| 40 |
elif script -q -c true /dev/null; then |
| 41 |
if script -q -c false /dev/null; then # -e behavior? |
| 42 |
printf '%q ignores child exit status\n' "$(command -v script)" 1>&2 |
| 43 |
exit 2 |
| 44 |
fi |
| 45 |
script -q -c "$(printf ' %q' "$@")" /dev/null |
| 46 |
elif script -q /dev/null true; then |
| 47 |
if script -q /dev/null false; then # -e behavior? |
| 48 |
printf '%q ignores child exit status\n' "$(command -v script)" 1>&2 |
| 49 |
exit 2 |
| 50 |
fi |
| 51 |
script -q /dev/null "$@" |
| 52 |
else |
| 53 |
rc=0 |
| 54 |
cmd="$(command -v script)" || rc=$? |
| 55 |
if test "$rc" -eq 0; then |
| 56 |
printf 'Unsupported script command: %q\n' "$cmd" 1>&2 |
| 57 |
else |
| 58 |
echo 'No script command' 1>&2 |
| 59 |
fi |
| 60 |
exit 2 |
| 61 |
fi |
| 62 |
|
| 63 |
case "$OSTYPE" in |
| 64 |
netbsd) |
| 65 |
# On NetBSD, the top-level script(1) process exits when read() |
| 66 |
# reports EOF on stdin; this happens when running tests with |
| 67 |
# /dev/null as stdin. As a workaround, wait after script, so |
| 68 |
# that the command will have finished. |
| 69 |
sleep 5 |
| 70 |
;; |
| 71 |
esac |