I have a shell-script that looks like the following code snippet:
...
export updates=0
processFiles() {
updates=$((updates+1))
}
export -f processFiles
find $path -exec /bin/bash -c "processFiles '{}'" \;
echo $updates
The use is to count the amount of updates, inserts and files to keep. Unfortunately the last echo always prints 0.
I have already tried to use export in the function as well – didn’t work.
Instead of using
find, you can use bash directly, provided your search criteria are simple enough. E.g.,will execute the function
processFileson all files and directories of the current directory (except the hidden ones) with argument the name of each file and directory. If you need to apply it recursively to all files and directories and subdirectories (except the hidden ones), use the globstar shell option:If you just want files, and no directories, insert this in the for loop:
which will continue the loop if
fileis not a file (i.e., if it’s something else, like a directory).To match only files in a directory obtained by expanding the variable
path, write your for loop thus:(or
"$path"/**if you’re using globstar and want recursion).If you need more complex search criteria, you can always use
extgloboption. If you need to also include the hidden files, use thedotgloboption.You’ll find more info about bash in the reference manual, and in particular these sections:
One last note: If you really don’t like this 100% bash solution and if you still want to do something along the script in your OP, then don’t use bash variables, but use a (temporary) file instead to store the values you need to pass from one process to another. This is a cheap IPC method that works pretty well, but that is more difficult to implement safely.