- Pass all parameters (
$*
) passed to script
to program
too
- Check each parameter you interested, if it's already in parameters passed, then ignore it. otherwise use default parameter value
Sample code
interested_parameter_names=(-in -out)
default_parameter_values=(file.in file.out)
program=echo
cmd="$program $*"
for ((index=0; index<${#interested_parameter_names[*]}; index++))
do
param="${interested_parameter_names[$index]}"
default_value="${default_parameter_values[$index]}"
if [ "${*#*$param}" == "$*" ] # if $* not contains $param
then
cmd="$cmd $param $default_value"
fi
done
echo "command line will be:"
echo "$cmd"
echo
echo "execute result:"
$cmd
You can easily add more default parameters/values by add more array element in $interested_parameter_names
and $default_parameter_values
Sample output
$ ./wrapper.sh -in non-default.txt -other-params
command line will be:
echo -in non-default.txt -other-params -out file.out
execute result:
-in non-default.txt -other-params -out file.out
Note
When passing parameters which contains space, it should be escaped by \
, not just quoted them. Example:
./script -in new\ document.txt