0
0
Bash Scriptingscripting~20 mins

Parallel execution patterns in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parallel Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of background jobs with wait
What will be the output of this script when run in a bash shell?
Bash Scripting
echo "Start"
(sleep 1; echo "Job 1 done") &
(sleep 2; echo "Job 2 done") &
wait
echo "All jobs finished"
AStart\nAll jobs finished\nJob 1 done\nJob 2 done
BStart\nJob 1 done\nJob 2 done\nAll jobs finished
CJob 1 done\nJob 2 done\nStart\nAll jobs finished
DStart\nJob 2 done\nJob 1 done\nAll jobs finished
Attempts:
2 left
💡 Hint
Remember that 'wait' pauses the script until all background jobs finish.
📝 Syntax
intermediate
1:30remaining
Correct syntax for running commands in parallel
Which option correctly runs three commands in parallel and waits for all to finish?
Acmd1 & cmd2 & cmd3 & wait &
Bcmd1 & cmd2 & cmd3 wait
Ccmd1 & cmd2 & cmd3 & wait
Dcmd1 cmd2 cmd3 & wait
Attempts:
2 left
💡 Hint
Use '&' to run commands in background and 'wait' to pause until all finish.
🔧 Debug
advanced
2:00remaining
Fix the issue with parallel job output mixing
This script runs two background jobs but their outputs sometimes mix on the terminal. What is the main cause?
Bash Scripting
echo "Start"
(sleep 1; echo "Job 1 done") &
(sleep 1; echo "Job 2 done") &
wait
echo "End"
AThe sleep times are different causing output order issues.
BThe 'wait' command is missing causing jobs to overlap.
CThe echo commands are missing quotes causing syntax errors.
DBoth jobs write to stdout simultaneously causing output mixing.
Attempts:
2 left
💡 Hint
Think about how multiple background jobs write to the terminal at the same time.
🚀 Application
advanced
2:30remaining
Limit number of parallel jobs in a loop
You want to run 10 commands in parallel but only allow 3 to run at the same time. Which approach achieves this?
Bash Scripting
for i in {1..10}; do
  some_command $i &
  # limit to 3 parallel jobs
  # ???
done
wait
AUse a counter and 'wait' every 3 jobs: if (( i % 3 == 0 )); then wait; fi
BRun all commands in background without wait to limit jobs
CUse 'sleep 3' inside the loop to delay jobs
DUse 'wait' only after the loop to limit jobs
Attempts:
2 left
💡 Hint
You can pause the loop every few jobs to limit concurrency.
🧠 Conceptual
expert
3:00remaining
Understanding job control and process IDs in parallel scripts
In a bash script, you run multiple background jobs and want to track their process IDs (PIDs) to wait for specific ones. Which method correctly captures and waits for each job's PID?
A
cmd1 &
pid1=$!
cmd2 &
pid2=$!
wait $pid1
wait $pid2
B
cmd1 & pid1=$!
cmd2 & pid2=$!
wait $pid1
wait $pid2
C
pid1=$!; cmd1
pid2=$!; cmd2
wait $pid1
wait $pid2
D
pid1=$!; cmd1 &
pid2=$!; cmd2 &
wait $pid1
wait $pid2
Attempts:
2 left
💡 Hint
The special variable $! holds the PID of the last background job started.