The entire purpose of running a job asynchronously is to allow you to run other jobs at the same time. If you don't want to run any other jobs at the same time while your find
command is running; don't run it asynchronously.
The two commands:
find . -type f | wc -l > $tot_files_found &
wait ${!}
produce exactly the same output that the single command:
find . -type f | wc -l > $tot_files_found
does, but takes fewer keystrokes and makes less busywork for the shell. The first two commands force the shell to create a subshell to run find
asynchronously; the last command is run in the current shell execution environment without the need to create a subshell environment. The first two commands slow down your system for you and other users on your system.
Note also that every time this script runs, it creates a new temporary file and (from what you have shown us) never uses anything written into it and never removes it. And the name you are using for that temporary file (with three random numbers included in the name and with two of those random numbers adjacent to each other with no separator) increases the chance that two different invocations of your script will accidentally come up with the same name. If that happens, find
will never be run for this invocation of your script.
And, there is no need to create three subshells to include those three random numbers in the name. The command:
tot_files_found=${RANDOM}_find.$CURRENT_USER.$RANDOM$RANDOM
produces exactly the same temporary filename as your current code does but avoids creating three unneeded subshells to set the name.
A more common way to do this (since there can never be two shells running with the same process ID) is to use:
tot_files_found=find.$CURRENT_USER.$$
to lessen the chance of a collision with your various multiple adjacent uses of $RANDOM
. Of course, we might also comment that as far as we can tell from what you have shown us, $CURRENT_USER
will expand to an empty string because it has never been set to anything else. And, since we have no idea how, or if, CURRENT_USER
has been set, any expansion of tot_files_found
when field splitting might occur should be included in a double-quoted string.
From everything that you have said about what you are trying to do, I do not understand why you have a loop at all? The following would seem to be a better fit for your stated goals (which I still do not understand):
tot_files_found=find.$CURRENT_USER.$$
# recursively gets the total number of files
find . -type f | wc -l > "$tot_files_found" |
yad --image="$MD5SUM_ICONS/binary.png" --progress --pulsate \
--progress-text="" --center --title="Getting things ready..." \
--text="Reading files..." --auto-kill --auto-close --no-buttons \
--no-escape --fixed
But, as has been said before, having yad
as the second process in a pipeline when the first process in that pipeline will NEVER write anything into that pipe is not a "normal" way to do things. Whatever output yad
produces has nothing to do with whether or not find
is making any progress; all it can tell us is when find
has terminated. And that could be done more simply with:
tot_files_found=find.$CURRENT_USER.$$
# recursively gets the total number of files
echo 'find is running'
find . -type f | wc -l > "$tot_files_found"
echo 'find is done'
We should probably also include a trap
to remove the temporary file created by this script, but I haven't yet figured out if there might be some reason why you would want to actually know how many files were found instead of just wasting time figuring out how many there are and then exiting without ever looking at the results.