Given a program that requires some parameters, e.g. program -in file.in -out file.out
, what would be the simple-most approach to write a bash script that could be called with or without any of these parameters and use default values for each?
script -in otherfile
would run program -in otherfile -out file.out
,
script -out otherout -furtherswitch
would run program -in file.in -out otherout -furtherswitch
etc.
Best Answer
A default value is easy to define in Bash:
To process your parameters, you can use a simple loop:
Useful reads:
I also recommend using
getopt
instead, because it is able to handle many special cases which would very quickly complicate and clutter your code (Non-trivial example).