0

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.

  • @stoney, that question's answer did not help me. It didn't work. – Saeed Neamati Jan 26 '22 at 11:50
  • If you redirect standard error to /dev/null with 2>/dev/null instead of closing the descriptor (which could make find terminate prematurely), does that help? You also need a wait 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:08
  • @Kusalananda, thank you for your response. No, it didn't help. I updated the question with your modifications. It still does not work. – Saeed Neamati Jan 26 '22 at 12:20
  • So adding wait to the very end, after the loop, does not help? Do the echo 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:35
  • @SaeedNeamati the linked duplicate says (in its accepted answer) to add a "single wait 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
  • Dear Jeff, it was a typo. I added that wait to the update. Please open the question. – Saeed Neamati Jan 26 '22 at 19:46

0 Answers0