Originally Posted by shivaa
@David the H: Where are you? Please take some time on this thread.
Thanks for the vote of confidence. Sorry it takes so long to come around, but have so many other brands in the fire now that I only have time and energy to come here every few days.
Frankly, I don't see all that much to comment on that hasn't been mentioned already. You seem to have done rather well on your own. I can only clarify what's been covered so far.
A lot of this does seem to be a problem with quoting. Quotemarks have a couple of functions. First, they indicate to the shell what should be considered a single, contiguous token, and second, it protects any shell-reserved characters from being interpreted, so that they get passed to the final command as-is.
(Actually, they're really the same, since whitespace has a special shell meaning.)
When you use double quotes around a variable, it protects the expanded contents from word-splitting and globbing expansion. This is especially important if it contains a filename.
As for globbing patterns, note that find
does its own globbing, and you have to protect the globbing from the shell so that find
gets it unaltered.
It's also possible to use backslash escapes for the same purpose, which is fine when you have only one or two characters to escape, but enclosing a longer string in quotes is usually easier to read and understand, since it creates a clear visual grouping. A good rule of thumb is simply to enclose the longest string possible, and to use single quotes with simple text and double quotes when there are variables or other substitutions involved.
As for the OP find
command, I don't see anything really wrong with it, other than ensuring that the strings are properly protected.
find "$BASE_DIRECTORY" -path '*/error/*' -type f -not -name 'SS*' -not -name 'AA*' -not -name 'PQ*' -mmin +60 -print >> "$ERR0R_FILES"
In any case, it seems to me that the real key to the discussion is found in post #4, where the actual goal was apparently described; to find files that exist only in the '/input/' directories. For that, I think the -regex
option would be a better choice, as simple globbing cannot limit the matches well enough.
find "$BASE_DIRECTORY" -type f -regex '.*/input/[^/]*' -mmin +60 -print >> "$ERR0R_FILES"
Finally, as for brackets around variables, in the end it's really a matter of personal style, but I certainly recommend not using them when unneeded. They provide nothing but exercise for the fingers, while also cluttering up the code and make it harder to read, leading to an increased chance of mistakes. I went through a phase myself where I thought I should use them all the time, but after a while I realized it wasn't worth the extra effort.
See here for more on recommended formatting practice:
Scripting With Style
Re: post #20, just leave the else
section out entirely (best), or use the ":
Re: post #22, pipefail
just changes the behavior of the exit code produced when a pipeline of commands is run. I don't see any use for it here.
Edit: I almost forgot to add my standard links to how the shell processes arguments. When you fully understand the concept behind these half of your scripting problems should disappear.