4 ms·
Why do you serialize and then deserialize the list of files? Even if you do disable the splitting on reparsing the list, why don't you just do this: find
by 1718627440 10mo ago
Why do you serialize and then deserialize the list of files? Even if you do disable the splitting on reparsing the list, why don't you just do this:
find . -type f -name '*.mp3' -exec some command {} ';'
That sounds just unnecessary and bug prune. Unless you target some odd platform where find doesn't support '-exec', but it is even in POSIX. I think due to your use of process substitution, your code has a higher chance of being unportable, so why do you want to complicate your code?
Stuff like that is why I personally prefer the man pages to random websites.
Honestly for something as complex as a shell (which describes both a language, an editor, and an implementation) the man page is surprisingly short. (6418 lines for me) I have just found the section on process substitution in <1min, without even using the search, just by reading. I looked it up, because I didn't knew the name of that syntax, so I needed to actually look for what I wanted and I do not use the man page of bash often.
- ndsipa_pomu 10mo agoIt's to deal with problematic filenames (e.g. containing a newline) and enables setting of variables within the current process. Putting multiple commands into the "-exec" of find is possible, but looks horrible. To be fair, my example didn't use multiple commands, so your version is okay for simple processing. Don't get me wrong, the BASH man page is great, it's just that I prefer to work with examples. For reference, here's the Greg's Wiki explanation: https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29 https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_....
- 1718627440 10mo agoOn my computer touch "$(echo -en 'a\nb')" find . -type f -name '*.mp3' -exec bash -c "echo '{}'; ls '{}'" ';' works just fine, but maybe it doesn't work everywhere. If you don't like how the multiple commands look like, you can always write it like this: find . -type f -name '*.mp3' -exec bash -c " echo '{}' ls '{}' " ';'
- ndsipa_pomu 10mo agoYep, you can chain multiple commands with find's "-exec", but I'm not a fan of it myself. I suspect setting variables in the current process is trickier though. (Very minor nitpick, it should be 'a\nb.mp3' to be included, but that does work fine) Incidentally, ShellCheck isn't happy with that although I don't follow their reasoning: find . -type f -name '*.mp3' -exec bash -c " ^-- SC2156 (warning): Injecting filenames is fragile and insecure. Use parameters. https://www.shellcheck.net/wiki/SC2156 https://www.shellcheck.net/wiki/SC2156
- 1718627440 10mo ago> although I don't follow their reasoning I think it is sound. Imagine what happens when the filename contains: ' && shutdown now && '.mp3
- ndsipa_pomu 10mo agoOf course that makes sense now. Anyhow here's an example of how I would use the while loop and process substitution in a BASH script: declare -i file_count=0 while IFS= LC_ALL=C read -r -d '' file; do file_count+=1 printf "file: %s\n" "${file}" done < <(find . -type f -name '*.mp3' -print0) printf "Processed %d files\n" "${file_count}" I think that'd be tricky to do using just a find/-exec command.
- 1718627440 10mo agoI see, but now you are essentially operating on multiple files, at once, so the serialization makes some sense. Although for just this, I wouldn't write the operation in bash at all: find . -type f -name '*.mp3' | wc -l Honestly I don't really view the shell / filesystem interface as a security boundary. I use the shell mainly for (automation of) interactive use, so any screwup due to e.g. quoting issues is my own fault, maybe even of using stupid filenames. Shell is a great language to connect streams of different programs into each other, not so much for doing any work. If I do that, I would reach for C.