diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-02 10:23:40 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-02 10:50:45 +1200 |
commit | e27810d8463826ff7b62431b3c9c2dcf0d844932 (patch) | |
tree | 4cbae4bc82e6e583961901aada44fcdd8c188cec /bin/apf | |
parent | Make bel(1) executable (diff) | |
download | dotfiles-e27810d8463826ff7b62431b3c9c2dcf0d844932.tar.gz dotfiles-e27810d8463826ff7b62431b3c9c2dcf0d844932.zip |
Change apf() to a shell script
Diffstat (limited to 'bin/apf')
-rwxr-xr-x | bin/apf | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/bin/apf b/bin/apf new file mode 100755 index 00000000..2d137c6b --- /dev/null +++ b/bin/apf @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +# Name self +self=apf + +# Require at least two arguments, give usage otherwise +if (($# < 2)) ; then + printf '%s: usage: %s ARGFILE COMMAND [ARGS...]\n' \ + "$self" "$self" >&2 + exit 2 +fi + +# First argument is the file containing the null-delimited arguments +argfile=$1 +shift + +# Check the arguments file makes sense +if [[ ! -e $argfile ]] ; then + printf '%s: %s: No such file or directory\n' \ + "$self" "$argfile" + exit 1 +elif [[ -d $argfile ]] ; then + printf '%s: %s: Is a directory\n' \ + "$self" "$argfile" + exit 1 +elif [[ ! -r $argfile ]] ; then + printf '%s: %s: Permission denied\n' \ + "$self" "$argfile" + exit 1 +fi + +# Read all the null-delimited arguments from the file +declare -a args +while IFS= read -rd '' arg ; do + args[${#args[@]}]=$arg +done < "$argfile" + +# Next argument is the command to run +cmd=$1 +shift + +# Run the command with the retrieved arguments first, then the rest of the +# command line as passed to the function +command "$cmd" "${args[@]}" "$@" |