3 ms·
What do you recommend, to not get intro trouble if there are spaces or newlines in the file names?
by ufo 4y ago
What do you recommend, to not get intro trouble if there are spaces or newlines in the file names?
- kevinmgranger 4y agoDon't use a shell script.
- ufo 4y agoDo you mean, always pass the list directly to the next script via function calls, without writing it to an intermediate file / pipeline?
- plonk 4y agoYes, use the list argument to Python’s subprocess.run for example. It’s much easier to not mess up if your arguments don’t get parsed by a shell before getting passed.
- kevinmgranger 4y agoI'm being flippant, because shell scripts are so inherently error prone they're to be avoided for critical stuff like this. If you _absolutely_ must use a shell script: 0. Use shellcheck, which will warn you about many of the below issues: https://www.shellcheck.net/ https://www.shellcheck.net/ 1. understand how quoting and word splitting work: https://mywiki.wooledge.org/Quotes https://mywiki.wooledge.org/Quotes 2. if piping files to other programs, using `-print0` or equivalent (or even better, if using something like find, its built in execution options): https://mywiki.wooledge.org/UsingFind https://mywiki.wooledge.org/UsingFind 3. Beware the pitfalls (especially something like parsing `ls`): https://mywiki.wooledge.org/BashPitfalls https://mywiki.wooledge.org/BashPitfalls (warning: the community around that wiki can be pretty toxic, just keep that in mind when foraying into it.)
- marcosdumay 4y agoTry not to delete stuff with Bash. This is the most reliable way. Bash has a few niceties for error handling, but if you are using them, you would probably fare better in another language. If you do insist on Bash, quote everything, and use the "${var}" syntax instead of "$var". Also, make sure you handle every single possible error.
- ricardobeat 4y ago`set -e` will abort on any error, anywhere in the pipeline. It’s a must for any critical script.