diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-06-03 01:37:36 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-06-03 01:37:50 +1200 |
commit | f095526805fd347c23bf0a9e27609a6949051616 (patch) | |
tree | 7018ddc7c7f5e275daabc410f9f0588dcaabe9da | |
parent | Use full length($0) rather than just length (diff) | |
download | dotfiles-f095526805fd347c23bf0a9e27609a6949051616.tar.gz dotfiles-f095526805fd347c23bf0a9e27609a6949051616.zip |
Add rep(1df)
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | README.markdown | 1 | ||||
-rw-r--r-- | bin/rep.sh | 25 | ||||
-rw-r--r-- | man/man1/chn.1df | 7 | ||||
-rw-r--r-- | man/man1/rep.1df | 15 |
6 files changed, 47 insertions, 3 deletions
@@ -82,6 +82,7 @@ bin/pst.m4 bin/pvi bin/pwg bin/quo +bin/rep bin/rfcf bin/rfcr bin/rfct @@ -149,6 +149,7 @@ BINS = bin/ap \ bin/pvi \ bin/pwg \ bin/quo \ + bin/rep \ bin/rfcf \ bin/rfcr \ bin/rfct \ diff --git a/README.markdown b/README.markdown index bd0b6682..f50583ce 100644 --- a/README.markdown +++ b/README.markdown @@ -521,6 +521,7 @@ Installed by the `install-bin` target: [`plenv`](https://github.com/tokuhirom/plenv), filters out any modules in `~/.plenv/non-cpan-modules`, and updates them all. * `pwg(1df)` generates just one decent password with `pwgen(1)`. +* `rep(1df)` repeats a command a given number of times. * `rgl(1df)` is a very crude interactive `grep(1)` loop. * `shb(1df)` attempts to build shebang lines for scripts from the system paths. diff --git a/bin/rep.sh b/bin/rep.sh new file mode 100644 index 00000000..e53cbac3 --- /dev/null +++ b/bin/rep.sh @@ -0,0 +1,25 @@ +# Repeat a command +self=rep + +# Check arguments. +if [ "$#" -lt 2 ] ; then + printf >&2 '%s: Need a count and a program name\n' "$self" + exit 2 +fi + +# Shift off the repetition count. +c=$1 +shift + +# Check the repetition count looks sane. Zero is fine! +if [ "$c" -lt 0 ] ; then + printf >&2 '%s: Nonsensical negative count\n' "$self" + exit 2 +fi + +# Run the command the specified number of times. Stop immediately as soon as a +# run fails. +while [ "${n=1}" -le "$c" ] ; do + "$@" || exit + n=$((n+1)) +done diff --git a/man/man1/chn.1df b/man/man1/chn.1df index 576e5425..5e9c702d 100644 --- a/man/man1/chn.1df +++ b/man/man1/chn.1df @@ -24,11 +24,12 @@ Zero is a valid count; in this case the input is passed untouched to output: $ chn 0 quo < msg Hello! .P -Don't confuse this with simply repeating a command. This happens to work: +Don't confuse this with simply repeating a command--use rep(1df) for that.. +This happens to do what you might expect: .P $ chn 5 sync .P -But this will not do what you expect: +But this won't: .P $ chn 5 echo foo .SH CAVEATS @@ -42,6 +43,6 @@ There's almost certainly a better way to do this, fixing one or both of the above issues, and possibly even in shell; maybe with curlier file descriptor logic to save unneeded open(2) syscalls. I smell `eval` usage on the horizon. .SH SEE ALSO -maybe(1df), try(1df) +maybe(1df), rep(1df), try(1df) .SH AUTHOR Tom Ryder <tom@sanctum.geek.nz> diff --git a/man/man1/rep.1df b/man/man1/rep.1df new file mode 100644 index 00000000..8971f392 --- /dev/null +++ b/man/man1/rep.1df @@ -0,0 +1,15 @@ +.TH REP 1df "August 2017" "Manual page for rep" +.SH NAME +.B rep +\- run a command repeatedly +.SH USAGE +.B rep +COUNT +COMMAND [ARG1...] +.SH DESCRIPTION +Run the given command the specified number of times. Zero is a valid count; +nothing happens. +.SH SEE ALSO +chn(1df), maybe(1df), try(1df), watch(1) +.SH AUTHOR +Tom Ryder <tom@sanctum.geek.nz> |