aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-20 16:47:37 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-20 16:47:37 +1200
commitfa2f423345776d8a99dae604f89e5b0ba76ceabb (patch)
tree8e1d83f9ea24ce519246b52921cc66350e39bdd0
parentRemove readv() and readz() (diff)
downloaddotfiles-fa2f423345776d8a99dae604f89e5b0ba76ceabb.tar.gz
dotfiles-fa2f423345776d8a99dae604f89e5b0ba76ceabb.zip
Port fnl() to POSIX sh script fnl(1)
No real compelling reason to make it a shell function in the first place.
-rw-r--r--README.markdown4
-rw-r--r--bash/bashrc.d/fnl.bash41
-rwxr-xr-xbin/fnl21
-rw-r--r--man/man1/fnl.116
4 files changed, 39 insertions, 43 deletions
diff --git a/README.markdown b/README.markdown
index 1c205261..40556ef1 100644
--- a/README.markdown
+++ b/README.markdown
@@ -207,8 +207,6 @@ in `sh/shrc.d` to be loaded by any POSIX interactive shell. Those include:
There are a few other little tricks defined for other shells, mostly in
`bash/bashrc.d`:
-* `fnl()` runs a command and saves its output and error into temporary files,
- defining variables with the filenames in them.
* `keep()` stores ad-hoc shell functions and variables.
* `path()` manages the contents of `PATH` conveniently.
* `prompt()` sets up my interactive prompt.
@@ -396,6 +394,8 @@ Installed by the `install-bin` target:
`~/.local/bin`, for personal scripting snippets.
* `fgscr(1)` finds Git repositories in a directory root and scrubs them with
`gscr(1)`.
+* `fnl(1)` runs a command and saves its output and error into temporary files,
+ printing their paths and line counts
* `gms(1)` runs a set of `getmailrc` files; does much the same thing as the
script `getmails` in the `getmail` suite, but runs the requests in parallel
and does up to three silent retries using `try(1)`.
diff --git a/bash/bashrc.d/fnl.bash b/bash/bashrc.d/fnl.bash
deleted file mode 100644
index 1f543dbf..00000000
--- a/bash/bashrc.d/fnl.bash
+++ /dev/null
@@ -1,41 +0,0 @@
-# Run a command and push its stdout and stderr into temporary files, printing
-# the names of the files once done, and saving them into two variables. Return
-# the exit status of the command.
-#
-# $ fnl grep foo /bar
-# declare -p fnl_stdout="/tmp/fnl.xQmhe/stdout"
-# declare -p fnl_stderr="/tmp/fnl.xQmhe/stderr"
-#
-fnl() {
-
- # Must be called with at least one command argument
- if ! (($#)) ; then
- printf 'bash: %s: usage: %s COMMAND [ARG1 ...]\n' \
- "$FUNCNAME" "$FUNCNAME" >&2
- return 2
- fi
-
- # Try to stop infinitely recursive calls
- if [[ $1 == "$FUNCNAME" ]] ; then
- printf 'bash: %s: Cannot nest calls\n' \
- "$FUNCNAME" >&2
- return 2
- fi
-
- # Create a temporary directory or bail
- local dirname
- dirname=$(mktd "$FUNCNAME") || return
-
- # Run the command and save its exit status
- local ret
- "$@" >"$dirname"/stdout 2>"$dirname"/stderr
- ret=$?
-
- # Note these are *not* local variables
- # shellcheck disable=SC2034
- fnl_stdout=$dirname/stdout fnl_stderr=$dirname/stderr
- declare -p fnl_std{out,err}
-
- # Return the exit status of the command, not the declare builtin
- return "$ret"
-}
diff --git a/bin/fnl b/bin/fnl
new file mode 100755
index 00000000..d27d1f90
--- /dev/null
+++ b/bin/fnl
@@ -0,0 +1,21 @@
+#!/bin/sh
+# Run a command and save its output and error to temporary files
+
+# Check we have at least one argument
+if [ "$#" -eq 0 ] ; then
+ printf >&2 'fnl: Command needed\n'
+ return 2
+fi
+
+# Create a temporary directory; note that we *don't* clean it up on exit
+dir=$(mktd fnl) || exit
+
+# Run the command; keep its exit status
+"$@" >"$dir"/stdout 2>"$dir"/stderr
+ret=$?
+
+# Run wc(1) on each of the files
+wc -- "$dir"/*
+
+# Exit with the wrapped command's exit status
+exit "$ret"
diff --git a/man/man1/fnl.1 b/man/man1/fnl.1
new file mode 100644
index 00000000..a578dbf4
--- /dev/null
+++ b/man/man1/fnl.1
@@ -0,0 +1,16 @@
+.TH FNL 1 "August 2016" "Manual page for fnl"
+.SH NAME
+.B fnl
+\- list the biggest files in the given directory
+.SH SYNOPSIS
+.B fnl
+command arg1 ...
+.SH DESCRIPTION
+.B fnl
+runs the command specifies in its arguments, writing any stdout and stderr to
+separate temporary files created with mktd(1), and then runs wc(1) over them to
+show their statistics and full paths.
+.SH SEE ALSO
+igex(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>