2 ms·
Your script fails if the files referenced by $f contain white spaces or a newline. You should script defensively, for when this is run on Windows or elsewhere
by chasil 5y ago
Your script fails if the files referenced by $f contain white spaces or a newline.
You should script defensively, for when this is run on Windows or elsewhere [maybe Android] that will break this assumption.
Newlines can also be part of a filename. For extreme rigor, use find -print0 and xargs -0.
I have covered some of this here:
https://www.linuxjournal.com/content/parallel-shells-xargs-utilize-all-your-cpu-cores-unix-and-windows https://www.linuxjournal.com/content/parallel-shells-xargs-u...
(Somebody took me to the Hacker News front page for this, and nobody said that I made a mistake.)
- Flimm 5y agoI agree. It also fails if the filename starts with a dash.
- marginalia_nu 5y ago> You should script defensively, for when this is run on Windows or elsewhere [maybe Android] that will break this assumption. I think you are missing the point. I wouldn't run a shellscript on Windows or any other place than the terminal I entered it into. It's an expression of something I want to do at that particular time in that particular directory. The entire point of my post is that if I wished to write something portable and robust, I wouldn't use bash in the first place. That's not what bash is good at. But quick one-offs, holy crap is it good at that.
- chasil 5y agoI've run many Borne/POSIX shell scripts on Windows for things that I've needed to do. The most straightforward POSIX shell on Windows is the Busybox port, which uses the Almquist shell (ash) with many bashisms reintroduced. https://frippery.org/busybox/ https://frippery.org/busybox/
- kwhitefoot 5y ago> fails if the files referenced by $f contain white spaces or a newline. This is said every time Bash scripting is mentioned. Why people can't get into the habit of quoting all variables in Bash is beyond me and the mention of white space makes sense; but how often, if ever, have you encountered a file name that contained a newline? Most of the Unix tools are line oriented by default anyway and are frequently used to manipulate lists of file names and paths.
- chasil 5y agoIt's safe to omit quotes for POSIX shell variables that are initialized as numeric, and you enforce to remain numeric. All else should be quoted, and newlines can happen, primarily from abuse. Do not trust user input! Calling fopen() on a file with every ASCII character will work on POSIX, as long as it does not contain a null or forward slash (/). Coding defensively means that this does not crash your processing.
- kwhitefoot 5y agoI'm not objecting to quoting; I do it even when the variable contains, or should contain, a number. I was objecting to the knee jerk inclusion of warnings against file names including newlines. Has anyone encountered one in the wild that was not itself an error?