diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-16 18:38:49 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-16 18:38:49 +1200 |
commit | 0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1 (patch) | |
tree | 9712aa161c25232e0f6561ee181b82eb42af347a /bin/rndl | |
parent | Put "all" subtargets on their own line (diff) | |
download | dotfiles-0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1.tar.gz dotfiles-0b1c99ffb1c3019e8afb9dcc542fbc5e868baef1.zip |
Template traps in scripts depending on mktd(1)
Diffstat (limited to 'bin/rndl')
-rwxr-xr-x | bin/rndl | 49 |
1 files changed, 0 insertions, 49 deletions
diff --git a/bin/rndl b/bin/rndl deleted file mode 100755 index 2ac3bf47..00000000 --- a/bin/rndl +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/sh -# Print a random line from input - -# If there are no arguments, we're checking stdin; this is more complicated -# than checking file arguments because we have to count the lines in order to -# correctly choose a random one, and two passes means we require a temporary -# file if we don't want to read all of the input into memory (!) -if [ "$#" -eq 0 ] ; then - - # Try to create the temporary directory with mktd(1) safely - td= - cleanup() { - [ -n "$td" ] && rm -fr -- "$td" - if [ "$1" != EXIT ] ; then - trap - "$1" - kill "-$1" "$$" - fi - } - for sig in EXIT HUP INT TERM ; do - # shellcheck disable=SC2064 - trap "cleanup $sig" "$sig" - done - td=$(mktd rndl) || exit - - # We'll operate on stdin in the temp directory; write the script's stdin to - # it with cat(1) - set -- "$td"/stdin - cat >"$td"/stdin -fi - -# Count the number of lines in the input -lc=$(sed -- '$=;d' "$@") || exit - -# If there were none, bail -case $lc in - ''|0) - printf 2>&1 'rndl: No lines found on input\n' - exit 2 - ;; -esac - -# Try to get a random seed from rnds(1) for rndi(1) -seed=$(rnds) - -# Get a random line number from rndi(1) -ri=$(rndi 1 "$lc" "$seed") || exit - -# Print the line using sed(1) -sed -- "$ri"'!d' "$@" |