Library › Coding AI › Software developers
Fix a shell loop that breaks on filenames with spaces
Practice: Setting limits and rules. About 5 minutes.
About this exercise
The thumbnail job breaks on photos whose names contain spaces. It runs under /bin/sh on Alpine (BusyBox ash), so the fix must be POSIX sh: no [[ ]], no arrays, no bash-only syntax. Loop over a quoted glob instead of ls, skip the loop body when nothing matches with [ -e "$f" ] || continue, and keep the same convert options and the thumb- output names. New rules from ops: skip any photo that already has a thumbnail, using [ -e "$DEST/thumb-$name" ] && continue, and after each convert print made: $name. Return only the loop in exactly 1 code block of at most 10 lines, with under 25 words of text.
Common mistakes when you ask AI for this
- Getting bash. The AI may fix it with arrays, [[ ]] or a bash shebang, but this container has no bash. Try: Add: "POSIX sh only (BusyBox ash): no [[ ]], no arrays, no bash."
- No log line. Ops greps the job log for each thumbnail, and the AI won't print one unless asked. Try: Add: "After each convert, echo \"made: $name\"."
- Using find or ls again. find | while read still breaks on some names, and ls output was the bug in the first place. Try: Say: "Loop over a quoted glob: for f in \"$SRC\"/*.jpg; quote every variable."
- Forgetting the empty folder. With no matches, the glob stays literal and convert runs on '*.jpg'. Try: Add: "Start the loop body with [ -e \"$f\" ] || continue."
- Missing the ops rule. The AI can't know existing thumbnails should be skipped. Try: Add: "Skip a photo if \"$DEST/thumb-$name\" already exists."
- A rewritten script. The AI reprints the whole script, changes options or adds logging. Try: Add: "Only the loop, same convert options, at most 10 lines, no text."
What a strong prompt includes
- Names the shell
- Asks for a quoted glob
- Asks for the log line
- Skips existing thumbnails
- Asks for only the loop
Related exercises
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)
- Add labels to a signup form without rewriting the page (Coding AI · High school students)