aboutsummaryrefslogtreecommitdiff
path: root/bin/ap.sh
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2017-04-05 20:06:39 +1200
committerTom Ryder <tom@sanctum.geek.nz>2017-04-05 20:46:47 +1200
commitc8ab406749124d2e762ad5cf53963070113afd0f (patch)
tree54c9721a06957ebe7098a211eea803b0230c0f5d /bin/ap.sh
parentHandle POSIX correctness in ~/.bash_profile (diff)
downloaddotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.tar.gz
dotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.zip
Apply runtime shebanging to POSIX shell
Diffstat (limited to 'bin/ap.sh')
-rw-r--r--bin/ap.sh33
1 files changed, 33 insertions, 0 deletions
diff --git a/bin/ap.sh b/bin/ap.sh
new file mode 100644
index 00000000..1d6376cc
--- /dev/null
+++ b/bin/ap.sh
@@ -0,0 +1,33 @@
+# Run a program with args read from standard input, prompted if from term
+if [ "$#" -eq 0 ] ; then
+ printf >&2 'ap: Need at least one argument (command name)\n'
+ exit 2
+fi
+
+# Get the command name and shift it off
+cmd=$1
+shift
+
+# Iterate through the remaining args; it's legal for there to be none, but in
+# that case the user may as well just have invoked the command directly
+for arg ; do
+
+ # If this is the first iteration, clear the params away (we grabbed them in
+ # the for statement)
+ if [ -z "$reset" ] ; then
+ set --
+ reset=1
+ fi
+
+ # If stdin is a terminal, prompt with the name of the argument
+ if [ -t 0 ] ; then
+ printf '%s: ' "$arg"
+ fi
+
+ # Note that a whitespace or even empty argument is allowed
+ IFS= read -r val
+ set -- "$@" "$val"
+done
+
+# Execute the command with the given parameters
+exec "$cmd" "$@"