#DEFINE FLAME ON
to see code that's done like this, just because I wind up having to maintain it!
As the Perl community likes to say, "TMTOWTDI = There's More Than One Way To Do It
." Therefore, since you have so many programming-environments to choose from in Linux, try to choose the best
way. That is, the one that:
- ... describes the essential problem that is to be solved in the most general way
- ... is obvious to your colleagues and successors
- ... is the most maintainable.
Think about it: if you devise a solution that uses substrings to parse a filename like, say, backup_file.336.txt
, everything might work just fine until, one day, months later,
the one-thousandth backup file is created. The original solution, which certainly "worked" for 999 files, abruptly parses "1000" as either "100" or "000" and breaks.
it "crashes and burns," but more likely, it will fail silently,
perhaps writing-over important files.
Now, under great pressure and in a tremendous rush, your successor/colleague, "the maintainer," is desperately trying to find the bug. First s/he must locate the file in question, then s/he must understand what it is doing. Then, s/he must somehow fix it. Will s/he be happy, or will s/he be muttering epithets about your ancestry?
"A shell script" is not the right tool for this job. Since you have so
many general-purpose programming languages at your beck and call, all of them just a #!shebang
away, you should use one of them.
In the Perl community, for instance, a regular expression
would quite-instinctively be used to solve this problem, because it is a general-purpose solution. The regex /^backup_file\.([0-9]+)\.txt$/
will match all
such files of this format and will extract "sequence of one-or-more digits" from the middle of it. It's reasonably obvious (with just a little practice) to "see" what the pattern is supposed to be doing, and it expresses a generalized
solution to the problem. This makes a huge difference in maintainability.
When you write software, "write it to last two dozen years," because it probably will (even if you don't).