Challenge - 5 Problems
Loop Master
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 when run in a directory with files a.txt, b.txt, and c.log?
Consider this bash script:
for file in *.txt; do echo "$file"; doneWhat will it print?
Bash Scripting
for file in *.txt; do echo "$file"; done
Attempts:
2 left
💡 Hint
The wildcard *.txt matches only files ending with .txt.
✗ Incorrect
The loop iterates over files matching the pattern *.txt, so it prints only a.txt and b.txt, not c.log or the literal '*.txt'.
💻 Command Output
intermediate2:00remaining
What does this script output if the directory contains subdirectories dir1 and dir2?
Given this script:
for entry in *; do if [ -d "$entry" ]; then echo "$entry"; fi; doneWhat will it print?
Bash Scripting
for entry in *; do if [ -d "$entry" ]; then echo "$entry"; fi; done
Attempts:
2 left
💡 Hint
The test -d checks if the entry is a directory.
✗ Incorrect
The loop lists all entries, but only prints those that are directories, so it prints dir1 and dir2 only.
📝 Syntax
advanced2:00remaining
Which option correctly loops over all .log files and prints their names?
Choose the correct bash loop syntax to print all .log files in the current directory.
Attempts:
2 left
💡 Hint
Bash uses 'for var in list; do ... done' syntax.
✗ Incorrect
Option A uses correct bash syntax. Options B, C, and D use invalid syntax causing errors.
🚀 Application
advanced3:00remaining
How to count the number of files (not directories) in the current directory using a loop?
You want to count files only (exclude directories). Which script correctly does this?
Attempts:
2 left
💡 Hint
Use -f to test if an entry is a regular file.
✗ Incorrect
Option C counts only files. B counts directories, C counts all entries, D counts all existing entries including directories.
🔧 Debug
expert3:00remaining
Why does this script fail to print filenames with spaces correctly?
Script:
What is the problem?
for file in $(ls *.txt); do echo "$file"; done
What is the problem?
Bash Scripting
for file in $(ls *.txt); do echo "$file"; done
Attempts:
2 left
💡 Hint
Command substitution splits output by spaces and newlines.
✗ Incorrect
Using $(ls *.txt) splits filenames on spaces, breaking names with spaces into multiple words. Using 'for file in *.txt' is safer.