World map

 

BM: Baca dalam Bahasa Melayu

World L4: Remote Kitchens

Loops and exit codes

You will learn: to repeat steps with a for loop, and to read a program's exit code.

Checked with Linux (Debian 13) on 1 Nov 2026

The problem

You are you in /home/you. 2 things here. Services: cron running, ssh running, nginx running.

You are hereyou@kedai:~$

Folder ~

  • check.sh (file)-rwxr-xr-xcan run
  • menu/ (folder)drwxrwxr-xnasi-lemak.txt roti-canai.txt

+ 3 hidden (ls -a)

Services

  • cronrunningstarts at boot
  • sshrunningstarts at boot
  • nginxrunningstarts at boot

The dish checker says one dish is missing. Then echo $? prints 1. What does that 1 mean?

Watch

A loop: Do the same steps for each item: for dish in a b c; do ...; done. repeats steps for each item. Here is check.sh. Tap the line that starts the loop.

Tap the start of the loop.

0 of 1 found

Try it

Type a loop of your own, all on one line.

You are you in /home/you. 2 things here. Services: cron running, ssh running, nginx running.

You are hereyou@kedai:~$

Folder ~

  • check.sh (file)-rwxr-xr-xcan run
  • menu/ (folder)drwxrwxr-xnasi-lemak.txt roti-canai.txt

+ 3 hidden (ls -a)

Services

  • cronrunningstarts at boot
  • sshrunningstarts at boot
  • nginxrunningstarts at boot

Watch

Every program ends with an exit code: a thumbs-up (0) or a number for trouble. $? shows the last one. Sort them.

Drag a card into a box. Or tap the card, then tap the box.

      Predict

      One dish is still missing. You run ./check.sh && echo "Open the shop". What happens?

      Pick one answer.

      Try it

      Add the missing dish, then run the checker again. It should end with 0.

      You are you in /home/you. 2 things here. Services: cron running, ssh running, nginx running.

      You are hereyou@kedai:~$

      Folder ~

      • check.sh (file)-rwxr-xr-xcan run
      • menu/ (folder)drwxrwxr-xnasi-lemak.txt roti-canai.txt

      + 3 hidden (ls -a)

      Services

      • cronrunningstarts at boot
      • sshrunningstarts at boot
      • nginxrunningstarts at boot

      Check

      Build: run the checker, and only if it fails, say “Not yet”.

      Tap the pieces in the right order.

      …

      You did it!

      You can now loop over items and read exit codes.

      Go deeper (optional)

      while [ … ]; do … done loops while a question says yes. systemd reads your exit code too. A timer’s service shows failed when its script ends with non-zero. So end scripts with a meaningful number.

      Real mission

      1. for f in *.txt; do wc -l "$f"; done
      2. ls nope; echo $?

      0 of 2 steps done · Your ticks are saved.