Based on this answer I wrote some simple optional argument parser
#!/bin/bash
TEST_PATH=pluto
if [ $# -eq 0 ]; then
echo "Usage:"
echo "./run.sh [--valgrind|-v] [--test|-t] <mesh.dat> <csv_points_file>"
else
. config.sh
while [[ "$#" -gt 0 ]]; do
case $1 in
-v|--valgrind) VALGRIND="valgrind --track-origins=yes"; shift ;;
-t|--test) TEST="mickey goofy" shift ;;
*) ARGS+=($1) shift ;;
esac
shift
echo $TEST
done
echo ${VALGRIND} ${TEST_PATH} "${ARGS[*]:-$TEST}"
fi
Except when executing ./somescript.sh -v
the output is correct, while with the -t
argument nothing except the correct value of TEST_PATH
The expected behavior was meant to be
$./somescript.sh -t
pluto mickey goofy
$./somescript.sh daffy duck
pluto daffy duck
What am I doing wrong?