I'm trying to make a Bash script strictly POSIX-compliant, i.e. removing any potential "Bashisms" by using checkbashisms -px ${script_filename}. In the given file, I walk through a directory using find and then pipe each file path to read using \0 as a delimiter by using -print0 in order to be able to handle filenames containing newline characters:
find . -print0 | while read -d $'\0' inpath
do
echo "Reading path \"${inpath}\"."
done
However, checkbashisms doesn't like this because the option -d isn't strictly POSIX-compliant:
possible bashism in ... line n (read with option other than -r)
How can I write equivalent code which is POSIX-compliant, i.e. to read output from find using a non-newline delimiter?
Without -d option, read builtin cannot read null terminated data.
You can do this in find + xargs:
find . -mindepth 1 -print0 | xargs -0 sh -c 'for f; do echo "Reading path \"$f\""; done' _
Or if you don't mind spawning a shell for each file use just find:
find . -mindepth 1 -exec sh -c 'echo "Reading path \"$1\""' - {} \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With