In a bash script, I have a function that does some processing on the argument passed to it. I'd like to set a return value depending on how the processing went. Problem is that the function is called from find ... -exec bash -c func and thus losing the chance of updating a global variable that way, like error_code.
#!/bin/bash
check_file() {
filename=$1
echo -n "$filename ... "
if [ ... ]; then
echo "NOK"
# return/update error_code?
else
echo "OK"
fi
}
export -f check_file
# look for exec binary only
find . -type f -executable -exec bash -c "file -i {} | grep -q 'application/x-executable; charset=binary'" \; -exec bash -c 'check_file "$0"' {} \;
# exit $error_code??
In case error_code were a global variable that the function can update, it would then update it only when processing is 'NOK', as find will call many times the function check_file.
How can I do this with the existing script or perhaps need a different approach?
find ... -exec ...tofind ... | while read ....-execor| while readthat will spawn another shell and the problem remainsfind ... | while read ...you don't have to spawn a shell for each file.killis for.