On Tue May 14 11:11:33 2024 <cont...@strahinja.org> wrote: > When `while ... read ...` idiom is used, it is advisable to clear IFS > to turn off field splitting, and use -r to avoid interpretation of > backslash sequences in the input: > > while IFS= read -r dir; do # ... > > Back to parsing the output of ls(1) (also applicable to parsing the > output of find(1), or globs), there is an indepth analysis of the > problem at [1]. The accepted answer concludes that perhaps shell > command language is not the right tool for the job, and a more > sophisticated language should be used instead. While I don't agree with > the author's choice of Python, any language supporting opendir(3), > readdir(3) or equivalent functions will suffice. > > [1]: > https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead >
Let's start for what the first answer in that forum put in bold: Bourne shell is a bad language. It should not be used for anything complicated, unless extreme portability is more important than any other factor (e.g. autoconf). If you analyze that statement, depending on the case, it may have sense or not at all. I learned about the convenience of adding the '-r' option in the "while read" loop many years ago when I was writing a script to convert roff to html, the problem aroused with the backslash in roff comments (.\"). When I post an example of a shell script in some forum or mailing list I post an sketch, assuming others will use it as a example and write themselves their own solution. Who won't be able to overcome issues like the above are those who aren't familiar or trained in that language in particular. For me (as I think it should be for any unix user) shell scripting is mainly the way a "use" the computer, it's not a "programmers" language, something you use to write whole applications of the kind "Push this button and relax, I'll do the job". I have a hundred of dirty shell scripts in my ~/bin directory that if you examine them you'll find many dumb errors, but mainly they do the job. I don't know anything about python (I don't like it), but I bet that if you analyze python, or C or Perl, you'll also find inconsistencies you'll have to workaround as with shell scripting. Nothing is perfect when you see it in detail. Many years ago I posted in some linux forum an example of a shell script to blacklist IPs in a web-mail server. My intention was encouraging users to not follow the MSWin approach, I mean downloading some 3rd party tool instead of learning what the system already has to offer. And I remember myself proposing and giving solutions with rsync in that same forum to someone asking for a mirror capable synchronizing tool. This, and *learning to do things by yourself* (even if your program isn't as good the one you download or isn't good at at all), are the fundamental tendencies I always defend since ARE THE REASON OF EXISTENCE OF FOSS. Immediately, some "experts" started to point me "holes" in my script, when what I posted was just a sketch. In that case disregarding shell scripting wasn't useful as an argument since they were proposing fail2ban.sh (a clear example of using shell scripting for something complicated. ;-)) Summarizing, my motivation was triggered by the topic of the thread and the way it was raised by the OP. -- Walter