,pause_after.sh
1#!/bin/sh
2#
3# E.g. `tmux split ,pause_after echo hello` instead of figuring out how to
4# configure `tmux` to leave the pane open.
5BGREEN=$(printf '\033[01;32m')
6BYELLOW=$(printf '\033[01;33m')
7RESET=$(printf '\033[00m')
8# To make this work with `dash`, see https://unix.stackexchange.com/a/322213/261949
9# https://matrix.to/#/!YLTeaulxSDauOOxBoR:matrix.org/$Oe7jh_BPAc5gjqrj3CYsk8i4XaaMPsl4MKOC34oBmy8?via=matrix.org&via=gitter.im&via=matrix.protektwar.net
10# sh -c 'trap "trap : EXIT; echo aa" INT EXIT; cat'
11#
12# dash and bash behave differently on '( trap "echo ^C" INT; cat; ); trap - INT; echo end'
13# They behave the same without parentheses. I'm quite confused by this.
14# Or this hack of my invention. Not sure why it works, but it seems to work on dash+bash
15# Could try removing the subshell, but then would need to check that inner ctrl-c doesn't
16# break outer pipeline.
17trap : INT
18(
19 # exit 130 only matters in bash, and only if there were more commands after "$@".
20 trap "printf ' %sAborted by ^C.%s\n' \"$BYELLOW\" \"$RESET\"; exit 130" INT
21 "$@"
22)
23ERRCODE=$?
24trap - INT
25printf "%s\`%s\` exited with error code %s. Press Enter to continue, ^C/^D to abort/fail.%s " \
26 "$BGREEN" "$1" "$ERRCODE" "$RESET"
27read -r __unused || (echo "(Got EOF or ^D)"; false)