If you need to work on multiple files, you should use an array, rather than a single scalar variable.
Or are you trying to match a single file out of a directory to use? If so, you'll need to explain your requirements in more detail. Give us an example of the directory contents, which file you need to match, and how you want to use it.
In addition, I'd like to offer some general comments regarding your script:
QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS
. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.
It's recommended to use ((..))
for numerical tests, and [[..]]
for string tests and other complex expressions. Don't use the old [..]
test unless you specifically need POSIX-style portability.
Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).
Many people also think that it's more readable to place the "do/then
" keywords on the same line as the "for/while/until/if
" keywords, as it more clearly separates the outside block from the inside block.
for var in <list> ; do
if [[ "$var" == "foo" ]]; then
Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.