diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2017-04-05 20:06:39 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2017-04-05 20:46:47 +1200 |
commit | c8ab406749124d2e762ad5cf53963070113afd0f (patch) | |
tree | 54c9721a06957ebe7098a211eea803b0230c0f5d /bin/apf.sh | |
parent | Handle POSIX correctness in ~/.bash_profile (diff) | |
download | dotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.tar.gz dotfiles-c8ab406749124d2e762ad5cf53963070113afd0f.zip |
Apply runtime shebanging to POSIX shell
Diffstat (limited to 'bin/apf.sh')
-rw-r--r-- | bin/apf.sh | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/bin/apf.sh b/bin/apf.sh new file mode 100644 index 00000000..5e40e9b8 --- /dev/null +++ b/bin/apf.sh @@ -0,0 +1,62 @@ +# Prepend arguments from a file to the given arguments for a command +self=apf + +# Require at least two arguments +if [ "$#" -lt 2 ] ; then + printf >&2 '%s: Need an arguments file and a command\n' "$self" + exit 2 +fi + +# First argument is the file containing the null-delimited arguments +argf=$1 cmd=$2 +shift 2 + +# If there were arguments given on the command line, we need to be careful and +# prepend our ones first +if [ "$#" -gt 0 ] ; then + + # Iterate through any remaining arguments + for carg ; do + + # If this is the first command argument, then before we add it, we'll + # add all the ones from the file first if it exists + if [ -n "$argf" ] ; then + + # Reset the positional parameters + set -- + + # Put our file arguments in first before we continue with the loop + if [ -e "$argf" ] ; then + while IFS= read -r farg ; do + case $farg in + '#'*) continue ;; + *[![:space:]]*) ;; + *) continue ;; + esac + set -- "$@" "$farg" + done < "$argf" + fi + + # Unset the argfile so we don't repeat this bit + unset -v argf + fi + + # Stack the original invocation argument back onto the positional + # parameters + set -- "$@" "$carg" + done + +# If there weren't, we can just read the file and slap them in +elif [ -e "$argf" ] ; then + while IFS= read -r farg ; do + case $farg in + '#'*) continue ;; + *[![:space:]]*) ;; + *) continue ;; + esac + set -- "$@" "$farg" + done < "$argf" +fi + +# Run the command with the changed arguments +exec "$cmd" "$@" |