aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown1
-rwxr-xr-xbin/shb28
-rw-r--r--man/man1/shb.126
3 files changed, 55 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index 5e3eb501..ad4f78d1 100644
--- a/README.markdown
+++ b/README.markdown
@@ -349,6 +349,7 @@ Installed by the `install-bin` target:
* `plmu(1)` retrieves a list of installed modules from
[`plenv`](https://github.com/tokuhirom/plenv), filters out any modules in
`~/.plenv/non-cpan-modules`, and updates them all.
+* `shb(1)` attempts to build shebang lines for scripts from `$PATH`.
* `spr(1)` posts its input to the sprunge.us pastebin.
* `stbl(1)` strips a trailing blank line from the files in its arguments.
* `sue(8)` execs `sudoedit(8)` as the owner of all the file arguments given,
diff --git a/bin/shb b/bin/shb
new file mode 100755
index 00000000..743e5b75
--- /dev/null
+++ b/bin/shb
@@ -0,0 +1,28 @@
+#!/bin/sh
+# Use PATH to build a shebang for a script given on stdin
+self=shb
+
+# Need at least two arguments
+if [ "$#" -lt 2 ] ; then
+ printf >&2 '%s: Need input file and command\n' "$self"
+ exit 1
+fi
+
+# First argument is the script (might be - for stdin), second argument is the
+# name of the interpreter
+scr=$1 intn=$2
+shift 2
+
+# Try and find the path to the interpreter command, bail out if we can't
+if ! intp=$(command -v "$intn") ; then
+ printf >&2 '%s: %s: command not found\n' "$self" "$intn"
+ exit 1
+fi
+
+# Set the positional parameters to the path and any remaining arguments, and
+# squash them together for the shebang line
+set -- "$intp" "$@"
+printf '#!%s\n' "$*"
+
+# Emit the rest of the input
+cat -- "$scr"
diff --git a/man/man1/shb.1 b/man/man1/shb.1
new file mode 100644
index 00000000..64c7e0f9
--- /dev/null
+++ b/man/man1/shb.1
@@ -0,0 +1,26 @@
+.TH SHB 1 "August 2016" "Manual page for shb"
+.SH NAME
+.B shb
+\- insert a shebang line above a file
+.SH SYNOPSIS
+.B shb
+script bash
+.br
+.B shb
+script sed -f
+.br
+command |
+.B shb
+- awk -f
+.SH DESCRIPTION
+.B shb
+uses PATH to find a suitable program with the name given in the second
+argument, forms a "shebang" line from it and any arguments beyond the second,
+and then emits the contents of the first argument ('-' can be used for stdin).
+.P
+This is intended as a minimal way to make portable shebang lines for Makefiles
+or other building or installation frameworks, handling subtleties like sed(1)
+being located in /bin on Linux, but /usr/bin on BSD. It should work with any
+POSIX-compliant sh(1).
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>