How do you exit a script from within a find piped while loop?
E.g. following exit just exits the piped subshell:
find some_directory -type d|while read dir_name; do
if [ -w "$dir_name" ]; then
exit 1
fi
done
In bash an exit invoked in a subprocess will exit the subprocess but not the parent process.
In OP's code the pipeline - find | while - will run the while (and the subsequent exit invocation) within a subprocess, so if/when the exit 1 is invoked the subprocess will be exited but not the parent process.
To allow the exit to apply at the top level we need to insure the while is not run within a subprocess. Expanding on 123's comment:
while read -r dir_name; do
if [ -w "$dir_name" ]; then
exit 1
fi
done < <(find some_directory -type d)
Note that the <( ) construct ("process substitution") is not available in all shells, or even in bash when it's in sh-compatibility mode (i.e. when it's invoked as sh or /bin/sh). So be sure to use an explicit bash shebang (like #!/bin/bash or #!/usr/bin/env bash) in your script, and don't override it by running the script with the sh command.
done < <(find some_directory -type d), probs wanna usewhile IFS= read -r dir_nameto handle dodge filenames just incase as well.find some_directory -type d ! -perm -u+wwill give you all non-writable directories in directoryfind some_directory -type d ! -perm -u+w | egrep '.*'The return status will be 0 when something is found, and non-zero otherwise. Alternatively you may play withwc -lor if you need to process files then just add-exec {} \;piece tofindcommand