Is it possible to pass command line arguments to a function from a bourne script to allow getopts to handle them.
The rest of my script is nicely packed in a function, but it starts to look like I have to move the processing of arguments into the main logic.
The following describes how it is written, but it does not work:
processArgs ()
{
while getopts j: f: arg
do
echo "$ {arg} - $ {OPTARG}"
case "$ {arg}" in
j) if [-z "$ {filename}"]; then
job_number = $ OPTARG
else
echo "Filename $ {filename} already set."
echo "Job number $ {OPTARG} will be ignored.
fi ;;
f) if [-z "$ {job_number}"]; then
filename = $ OPTARG
else
echo "Job number $ {job_number} already set."
echo "Filename $ {OPTARG} will be ignored."
fi ;;
esac
done
}
doStuff1
processArgs
doStuff2
Is it possible to define a function so that it can read args scripts? Can this be done in another way? I like the functionality of getopts, but it seems that in this case I will have to sacrifice the beauty of the code to get it.
source share