sh stats.sh songlist
If you have a variable number of arguments, you can use the "$@" variable, which is an array of all the input parameters. This means you can use a for-loop to iteratively process each one, as illustrated in the following example:
for FILE1 in "$@"
sh stats.sh songlist1 songlist2 songlist3
sh stats.sh 'songlist 1' 'songlist 2' 'songlist 3'
Let say you have a script that retrieves information from a database based on specified parameters, such as "username", "date", and "product", and generates a report in a specified "format". Now you want to write your script such that you can pass in these parameters when the script is called. For example, like this:
makereport -u jsmith -p notebooks -d 10-20-2011 -f pdf
while getopts u:d:p:f: option
The colons in the optstring mean that values are required for the corresponding flags. In the above example all flags are followed by a colon: "u:d:p:f:". This means, all flags need a value. If, for example, the "d" and "f" flags were not expected to have a value, the optstring would be "u:dp:f".
A colon at the beginning of the optstring, for example ":u:d:p:f:", has a completely different meaning. It allows you to handle flags that are not represented in the optstring. In that case the value of the "option" variable is set to "?" and the value of "OPTARG" is set to the unexpected flag. The allows you to display a suitable error message informing the user of the mistake.
Arguments that are not preceded by a flag are ignored by getopts. If flags specified in the optstring are not provided when the script is called, nothing happens, unless you specially handle this case in your code. Any arguments not handled by getops can still be captured with the regular $1, $2, etc. variables.