Challenge - 5 Problems
POSIX Shell Scripting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of a POSIX-compliant shell script snippet
What is the output of this POSIX-compliant shell script snippet?
Bash Scripting
count=0 for file in *; do case "$file" in *.sh) count=$((count + 1)) ;; esac done echo "$count"
Attempts:
2 left
💡 Hint
Look at the pattern matching inside the case statement and the variable increment.
✗ Incorrect
The script loops over all files in the current directory and increments count only for files ending with .sh. The echo prints that count.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this POSIX shell script
Which option correctly identifies the syntax error in this POSIX shell script snippet?
Bash Scripting
if [ "$var" = "value" ] then echo "Match" else echo "No match" fi
Attempts:
2 left
💡 Hint
Check spacing and keywords carefully in POSIX shell if statements.
✗ Incorrect
The script uses correct spacing around = inside [ ] and includes then and fi keywords properly.
🔧 Debug
advanced2:00remaining
Why does this POSIX shell script fail to assign variable correctly?
Consider this POSIX shell snippet:
var=$(echo "Hello World" | awk '{print $2}')
echo "$var"
Why might this fail on some POSIX systems?
Bash Scripting
var=$(echo "Hello World" | awk '{print $2}') echo "$var"
Attempts:
2 left
💡 Hint
Consider the availability of external commands on minimal POSIX systems.
✗ Incorrect
While the syntax is POSIX compliant, some minimal POSIX systems might not have awk installed, causing the command to fail.
🧠 Conceptual
advanced2:00remaining
Which command is most portable for reading a file line by line in POSIX shell?
You want to read a file line by line in a POSIX-compliant shell script. Which command is the most portable and reliable?
Attempts:
2 left
💡 Hint
Consider how to preserve leading/trailing spaces and backslashes in lines.
✗ Incorrect
Option A uses IFS= and read -r to prevent trimming spaces and backslash interpretation, making it the most portable and reliable POSIX method.
🚀 Application
expert2:00remaining
Predict the output of this POSIX shell script with parameter expansion
What is the output of this POSIX shell script snippet?
filename="archive.tar.gz"
echo "${filename%.tar.gz}"
Bash Scripting
filename="archive.tar.gz" echo "${filename%.tar.gz}"
Attempts:
2 left
💡 Hint
The % operator in parameter expansion removes the shortest matching suffix.
✗ Incorrect
The expression ${filename%.tar.gz} removes the suffix .tar.gz from the variable, resulting in 'archive'.