diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-03 11:43:13 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-03 11:43:13 +1200 |
commit | 3e1906b43550efdc4cd18226b03bbd5d11c83433 (patch) | |
tree | 70ee9ead23528df6220e820ba0bce57fbaeec57a /bin/rndl | |
parent | Add mktd(1) (diff) | |
download | dotfiles-3e1906b43550efdc4cd18226b03bbd5d11c83433.tar.gz dotfiles-3e1906b43550efdc4cd18226b03bbd5d11c83433.zip |
Have rndl(1) use a tempfile
Diffstat (limited to 'bin/rndl')
-rwxr-xr-x | bin/rndl | 14 |
1 files changed, 11 insertions, 3 deletions
@@ -1,8 +1,16 @@ #!/bin/sh -# Print a random line from argument files +# Print a random line from input +td= +cleanup() { + rm -fr -- "$td" +} +for sig in EXIT HUP INT TERM ; do + trap cleanup "$sig" +done if [ "$#" -eq 0 ] ; then - printf >&2 'rndl: Need at least one filename\n' - exit 1 + td=$(mktd rndl) || exit + set -- "$td"/stdin + cat >"$td"/stdin fi lc=$(sed -- '$=;d' "$@") || exit ri=$(rndi 1 "$lc") || exit |