Track fallback attempts across jobs

A job runner models primary and backup processing with the Bash functions below. Each function increments its own call counter, then succeeds exactly when that counter is divisible by its stated divisor. A successful function returns exit status 0. Automatic exit on failure is disabled, and functions execute in the current shell.


Shell
primary_calls=0
backup_calls=0
completed=0

primary() {
    (( primary_calls += 1 ))
    (( primary_calls % 3 == 0 ))
}

backup() {
    (( backup_calls += 1 ))
    (( backup_calls % 2 == 0 ))
}

for job in a b c d e f g; do
    if primary || backup; then
        (( completed += 1 ))
    fi
done


Determine the total backup calls, the number of completed jobs, and the number of unsuccessful jobs. A job is unsuccessful when its if body does not execute. Give all three counts as exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Bash Control Structures Questions: