0
0
Bash Scriptingscripting~20 mins

Why file I/O is core to scripting in Bash Scripting - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File I/O Scripting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this file read command?
Consider a file named 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.txt
Acherry
B
banana
cherry
C
apple
banana
cherry
D
apple
banana
Attempts:
2 left
💡 Hint
The head command shows the first lines of a file.
💻 Command Output
intermediate
2:00remaining
What does this file write command do?
What is the result of running this command?
echo "Hello World" > greetings.txt

Assuming greetings.txt did not exist before.
Bash Scripting
echo "Hello World" > greetings.txt
ADeletes greetings.txt if it exists
BCreates greetings.txt with content 'Hello World'
CAppends 'Hello World' to greetings.txt if it exists
DPrints 'Hello World' to the terminal only
Attempts:
2 left
💡 Hint
The single > operator writes to a file, creating or overwriting it.
📝 Syntax
advanced
2: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?
Aecho "Goodbye" >> farewell.txt
Becho "Goodbye" < farewell.txt
Cecho "Goodbye" | farewell.txt
Decho "Goodbye" > farewell.txt
Attempts:
2 left
💡 Hint
Use the operator that adds to the file instead of replacing it.
🔧 Debug
advanced
2:00remaining
Why does this script fail to read the file?
This script tries to read 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.txt
AThe file redirection should be after done, not before
BThe variable $line is not quoted, causing issues with spaces
CThe file notes.txt does not exist or is empty
DThe read command needs -r option to work
Attempts:
2 left
💡 Hint
Check if the file exists and has content.
🚀 Application
expert
3: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 $count
Bash Scripting
count=0
while IFS= read -r line; do
  if [[ $line == *ERROR* ]]; then
    echo "$line"
    ((count++))
  fi
done < log.txt
echo $count
AThe number of lines containing 'ERROR' plus one
BThe number of lines containing 'ERROR'
C100 lines
DZero lines
Attempts:
2 left
💡 Hint
The script prints lines with 'ERROR' and then prints the count of those lines.