0

I'm looking for a Linux Shell script where I can Run a block of commands in parallel & save output to a file sequentially.

For example:

for each line in file.txt
{
    Execute A
    Execute B
    Execute C > Save output to output.txt
}

How do we make sure the output.txt has sequential output & the block of commands can be run in parallel.

2
  • Please post bash code here, if you have a question about bash, not some pseudocode. Commented Nov 26, 2021 at 12:47
  • Why so cryptic? What are you actually trying to do? Executing 3 processes for each line of large files is rarely a good way to proceed... Commented Nov 26, 2021 at 23:50

1 Answer 1

4

The wait bash builtin waits for all background jobs. So redirecting their output to a temporary file, waiting until they end, and concatenating the temporary files should be about what you want. Example where we replace Execute A by ( echo "A"; sleep 5 )`:

if tA=$(mktemp); then ( echo "A"; sleep 5 ) > "$tA" & fi
if tB=$(mktemp); then ( echo "B"; sleep 5 ) > "$tB" & fi
if tC=$(mktemp); then ( echo "C"; sleep 5 ) > "$tC" & fi
wait
cat "$tA" "$tB" "$tC" > output.txt
rm -f "$tA" "$tB" "$tC"

As noted by Léa Gris, when creating temporary files with mktemp it is always better to ensure that they are properly deleted even if the process is terminated before it explicitly deletes them. So the following is cleaner:

unset tA tB tC
trap 'rm -f -- "$tA" "$tB" "$tC"' EXIT TERM
if tA=$(mktemp); then ( echo "A"; sleep 5 ) > "$tA" & fi
if tB=$(mktemp); then ( echo "B"; sleep 5 ) > "$tB" & fi
if tC=$(mktemp); then ( echo "C"; sleep 5 ) > "$tC" & fi
wait
cat "$tA" "$tB" "$tC" > output.txt
Sign up to request clarification or add additional context in comments.

2 Comments

Before creating the temp files, register a cleanup trap: unset tA tB tC; trap 'rm -f -- "$tA" "$tB" "$tC"' EXIT TERM
@LéaGris Good point. I added this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.