Bash Positional Parameters Question

In a Bash script I used getopts command to let a user does something regards to the selected options. The question is: How do you find out what is the name of the file that user inserted in the command line like the following:

The good part is this file is always the last argument in the command line and there won't be any switches after the file. Also, the name of the file may change and it is not fixed!

This is from tldp.org - based on your question history I would strongly advise you to read:

Advanced Bash-Scripting Guide

You can download a pdf from there. It is all free.

This is from the getopts page. Among lots of other things it explains getopts use of the : character

while getopts ":abcde:fg" Option
# Initial declaration.
# a, b, c, d, e, f, and g are the options (flags) expected.
# The : after option 'e' shows it will have an argument passed with it.
do
case $Option in
a ) # Do something with variable 'a'.
b ) # Do something with variable 'b'.
...
e) # Do something with 'e', and also with $OPTARG,
# which is the associated argument passed with option 'e'.
...
g ) # Do something with variable 'g'.
esac
done

$OPTARG would be the value of -t filename where "t:" exists in the the format string.
There is also an example of how to handle trailing arguments that are not options.

1 Like

Thanks so much. it was the greatest help ever! I got what I wanted. The source that you introduced was a fantastic bash scripting source.
Cheers