Challenge - 5 Problems
Debugging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this script snippet?
Consider this bash script snippet that tries to list files and count them:
files=$(ls /tmp) echo $files count=$(echo $files | wc -w) echo $countWhat will be the output if /tmp contains exactly three files named a.txt, b.txt, and c.txt?
Bash Scripting
files=$(ls /tmp) echo $files count=$(echo $files | wc -w) echo $count
Attempts:
2 left
💡 Hint
Think about how command substitution and word splitting work in bash.
✗ Incorrect
The variable 'files' stores the output of 'ls /tmp' as a single string with filenames separated by newlines. When expanded without quotes, newlines become spaces, so 'echo $files' prints them space-separated. 'wc -w' counts words separated by spaces, so it counts 3 words.
🔧 Debug
intermediate2:00remaining
Why does this script fail to count files correctly?
This script tries to count the number of files in /var/log:
count=$(ls /var/log | wc -l) echo "There are $count files."But it sometimes gives wrong counts or errors. Why?
Bash Scripting
count=$(ls /var/log | wc -l)
echo "There are $count files."Attempts:
2 left
💡 Hint
ls lists everything in the directory: files, directories, symlinks, etc. Does it filter to files only?
✗ Incorrect
'ls /var/log' lists all directory entries including files, directories, and symlinks. 'wc -l' counts them all, so it overcounts the number of files. Spaces in names do not affect this since ls outputs one entry per line.
🚀 Application
advanced2:00remaining
Which script correctly counts files including those with spaces?
You want to count all files in /home/user/docs, including those with spaces in names. Which script correctly does this?
Attempts:
2 left
💡 Hint
Think about which command handles spaces safely and counts files correctly.
✗ Incorrect
'find /home/user/docs -type f | wc -l' outputs only regular files, one per line (handling spaces safely), and 'wc -l' counts the lines correctly. Option A uses -print0 (null-separated, no newlines), so wc -l returns 0. Option A uses 'ls' which counts all entries (files + dirs + ...), not just files. Option A uses 'wc -w' which splits lines on spaces, miscounting spaced names.
🧠 Conceptual
advanced2:00remaining
Why is debugging important in scripting automation?
Which statement best explains why debugging saves hours in scripting and automation?
Attempts:
2 left
💡 Hint
Think about the role of debugging in catching mistakes.
✗ Incorrect
Debugging helps identify and fix errors early, which prevents wasting time on bigger issues later. It does not automatically optimize code or replace testing or documentation.
📝 Syntax
expert2:00remaining
What error does this bash script produce?
What error will this script produce?
for file in $(ls /etc/passwd); do echo $file done
Bash Scripting
for file in $(ls /etc/passwd); do echo $file done
Attempts:
2 left
💡 Hint
Think about what 'ls /etc/passwd' outputs and how the for loop processes it.
✗ Incorrect
'ls /etc/passwd' outputs the filename '/etc/passwd' if it exists. The loop iterates over the words in that output (just one), so it prints the filename once. No syntax or runtime error occurs.