posix-dergutils/primitives/firstargnthargdo

35 lines
699 B
Bash
Executable File

#!/bin/sh
# Takes the first argument, injects it in the nth position and runs the result. If the argument or position is not available, returns 1.
USAGE="Usage: nthargdo [arg] [position] <command> [args ...]"
IFS='
'
if [ "$1" != "" ] && [ "$#" -gt 2 ] ; then
if [ "$2" -le $(( $# - 1 )) ] ; then
ARGUMENT="$1"
shift
POSITION="$1"
shift
if [ "$POSITION" -gt "$#" ] ; then
set -- $@ $ARGUMENT
else
for position in $(seq "$#") ; do
if [ "$position" = "$POSITION" ] ; then
set -- $@ $ARGUMENT
fi
set -- $@ $1
shift
done
fi
"$@"
else
echo "Invalid argument position." >&2
echo "$USAGE"
fi
else
echo "Not enough arguments." >&2
echo "$USAGE"
fi