aboutsummaryrefslogtreecommitdiff
path: root/bin/apf
blob: 06f7aa6b8a6ff12bd4030fb9a5e4dd553d38afe0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
# Prepend arguments from a file to a command call
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[@]}" "$@"