Challenge - 5 Problems
File I/O Scripting 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 file read command?
Consider a file named
What will the following command output?
data.txt containing the lines:apple banana cherry
What will the following command output?
head -n 2 data.txt
Bash Scripting
head -n 2 data.txtAttempts:
2 left
💡 Hint
The
head command shows the first lines of a file.✗ Incorrect
The
head -n 2 command outputs the first two lines of the file, which are 'apple' and 'banana'.💻 Command Output
intermediate2:00remaining
What does this file write command do?
What is the result of running this command?
Assuming
echo "Hello World" > greetings.txt
Assuming
greetings.txt did not exist before.Bash Scripting
echo "Hello World" > greetings.txtAttempts:
2 left
💡 Hint
The single > operator writes to a file, creating or overwriting it.
✗ Incorrect
The command creates the file greetings.txt and writes 'Hello World' inside it, overwriting if the file existed.
📝 Syntax
advanced2:00remaining
Which command correctly appends text to a file?
You want to add the line 'Goodbye' to the end of
farewell.txt without deleting existing content. Which command does this?Attempts:
2 left
💡 Hint
Use the operator that adds to the file instead of replacing it.
✗ Incorrect
The double >> operator appends text to the file, preserving existing content.
🔧 Debug
advanced2:00remaining
Why does this script fail to read the file?
This script tries to read
What is the problem?
notes.txt but prints nothing:while read line; do echo $line done < notes.txt
What is the problem?
Bash Scripting
while read line; do
echo $line
done < notes.txtAttempts:
2 left
💡 Hint
Check if the file exists and has content.
✗ Incorrect
If notes.txt is missing or empty, the loop reads nothing and prints nothing.
🚀 Application
expert3:00remaining
How many lines will this script output?
Given a file
log.txt with 100 lines, what is the number of lines output by this script?count=0
while IFS= read -r line; do
if [[ $line == *ERROR* ]]; then
echo "$line"
((count++))
fi
done < log.txt
echo $countBash Scripting
count=0 while IFS= read -r line; do if [[ $line == *ERROR* ]]; then echo "$line" ((count++)) fi done < log.txt echo $count
Attempts:
2 left
💡 Hint
The script prints lines with 'ERROR' and then prints the count of those lines.
✗ Incorrect
The script outputs each line containing 'ERROR' and then prints the total count of such lines on a separate line, resulting in the number of lines containing 'ERROR' plus one.