0
0
Bash Scriptingscripting~20 mins

Portable scripting (POSIX compliance) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
POSIX Shell Scripting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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"
ANumber of files ending with .sh in the current directory
BAlways outputs 0
CTotal number of files in the current directory
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint
Look at the pattern matching inside the case statement and the variable increment.
📝 Syntax
intermediate
2: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
AMissing spaces around the = operator inside [ ]
BMissing then keyword after the if condition
CNo syntax error; script is valid POSIX shell
DMissing closing bracket ] in the if condition
Attempts:
2 left
💡 Hint
Check spacing and keywords carefully in POSIX shell if statements.
🔧 Debug
advanced
2: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"
AThe command substitution syntax $(...) is not POSIX compliant
BSome POSIX systems do not have awk installed by default
CThe single quotes inside awk cause syntax errors in POSIX shell
DThe echo command is not POSIX compliant
Attempts:
2 left
💡 Hint
Consider the availability of external commands on minimal POSIX systems.
🧠 Conceptual
advanced
2: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?
Awhile IFS= read -r line; do echo "$line"; done < file.txt
Bcat file.txt | while read line; do echo "$line"; done
Cwhile read -r line; do echo "$line"; done < file.txt
Dfor line in $(cat file.txt); do echo "$line"; done
Attempts:
2 left
💡 Hint
Consider how to preserve leading/trailing spaces and backslashes in lines.
🚀 Application
expert
2: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}"
Aarchive.tar
Barchive.tar.gz
CSyntax error due to unsupported parameter expansion
Darchive
Attempts:
2 left
💡 Hint
The % operator in parameter expansion removes the shortest matching suffix.