My Bash idiom was immature. Scripting in Bash apparently requires some different habits than entering commands interactively on the command line does. For example, it requires that one terminate command-line options using '--'. It requires that one prefer printf or <<< rather than echo in some circumstances. It requires a clear understanding of the eval builtin. It requires lots of things.
In general, Bash has ways of escaping various possible misinterpretations of text, for example via the aforementioned '--' and eval, not to mention quoting. Most of these ways are unfamiliar to Bash command-line users (or at any rate, they were unfamiliar to me), so they must be learned—and, once learned, must frequently be employed. Coming from C++, I had not been used to escaping everything, like this:
Code: Select all
run_by_batch wget_insist "'"'-nc -x -nH --cut-dirs=4 -P '"'"'"'"'"'"'"$TARGET/pool"'"'"'"'"'"'"' --'"'"
Bash lacks programming support for long scripts, but support is for scripts longer than about 3000 lines. A decent programmer can work without support up to that length, so lack of support was not my problem. My problem was immature Bash idiom. Bash scales poorly when idiom is immature.
I suppose that there is no good remedy for immature scripting idiom other than writing a few thousand lines and failing at it, while occasionally seeking advice. The various advice others have given below has helped.
ORIGINAL QUESTION
In your opinion, how practical is it to write complex Bash shell scripts that handle offbeat filenames and pathnames like, say, 'My Files/-Preferences:(3)'?
I realize that it is possible to write such scripts, but every time one mixes various quoting and expansion mechanisms like (say) echo ${X}{1,2} | sed "s/${A}/${B%$C}/" or uses eval or the like, Bash—or various command-line tools invoked from Bash—seems to remind one how much it dislikes offbeat filenames. The script on which I am now working is a moderate 1500 lines or so in length, yet I have spent so many hours tracking down obscure bugs due to quoting and expansion issues that I am thinking of having the script simply reject all unconventional pathnames.
Some users won't like that.
Meanwhile, I feel that I am fighting Bash. I would instead like to work with Bash, and I would prefer not to give up on Bash just because some users have ported Microsoft-style file-naming habits to Debian.
Is there an easy way to do this? What am I missing, please?