I have this script to pull all of the git repositories on my machine:
#!/bin/bash
find / -type d -name .git 2>&- |
while read gitFolder; do
if [[ $gitFolder == "/Temp/" ]]; then
continue;
fi
if [[ $gitFolder == "/Trash/" ]]; then
continue;
fi
if [[ $gitFolder == "/opt/" ]]; then
continue;
fi
parent=$(dirname $gitFolder);
echo "";
echo $parent;
git -C $parent pull && echo "Got $parent" &
done
As you can see, to increase the speed, I let each git pull
of each repository be done in a subshell, using &
at the end.
This, however, puzzles the user. Because what he sees is that he runs this command, and then he sees the command gets executed very fast, and he has no clue that a lot of subshell commands are running.
He starts typing the next commands and all subshells print their messages one after the other messing his commands and he has to wait to make sure no subshell has remained.
And if the Internet becomes slow, things get more complicated.
How can I either:
- Make sure all subshells are executed, print a success message in green, and let the user know that he's good to go?
- Wait for all subshells and do not return the command-line back to the user, to prevent him from writing his next command?
Update:
I changed the script to the following, but it still does not wait:
#!/bin/bash
find / -type d -name .git 2>/dev/null |
while read gitFolder; do
if [[ $gitFolder == "/Temp/" ]]; then
continue;
fi
if [[ $gitFolder == "/Trash/" ]]; then
continue;
fi
if [[ $gitFolder == "/opt/" ]]; then
continue;
fi
parent=$(dirname $gitFolder);
echo "";
echo $parent;
(git -C $parent pull && echo "Got $parent") &
done
wait
echo "Got all"
I redirected the error from 2>&-
to 2>/dev/null
and I also enclosed the git command and its related echo inside a pair of parentheses.
/dev/null
with2>/dev/null
instead of closing the descriptor (which could makefind
terminate prematurely), does that help? You also need await
at the end of the script. If you need each job to output something, you need to make that happen in the background jobs, e.g. with(git ...; echo "done") &
. – Kusalananda Jan 26 '22 at 12:08wait
to the very end, after the loop, does not help? Do theecho
calls in the loop output the correct things? It's generally not clear from your question what the code actually does when it runs (if it outputs anything or has the intended effect or anything else). – Kusalananda Jan 26 '22 at 12:35wait
with no arguments at the end", which I don't see in your updated question. I'm responding to your moderator flag by declining it, since opening & closing of questions is generally done by the community in reaction to your question. I see no need to unlink the duplicate at this point. If it really doesn't solve your problem, edit your post -- being careful to understand the points from the linked answer -- and explain what is still wrong. – Jeff Schaller Jan 26 '22 at 18:33