Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Why is file input/output (I/O) important in bash scripting?
easy
A. It makes scripts run faster by using less memory.
B. It allows scripts to read from and write to files, automating tasks.
C. It helps scripts connect to the internet automatically.
D. It prevents scripts from crashing on errors.

Solution

  1. Step 1: Understand file I/O purpose

    File I/O lets scripts handle data stored in files, such as logs or reports.
  2. Step 2: Connect file I/O to automation

    By reading and writing files, scripts can automate many tasks involving data processing.
  3. Final Answer:

    It allows scripts to read from and write to files, automating tasks. -> Option B
  4. Quick Check:

    File I/O = Automate file data handling [OK]
Hint: File I/O means reading/writing files in scripts [OK]
Common Mistakes:
  • Confusing file I/O with internet access
  • Thinking file I/O speeds up script execution
  • Assuming file I/O prevents all script errors
2. Which of the following is the correct way to redirect the output of a command to a file in bash?
easy
A. command |> output.txt
B. command >> output.txt <
C. command < output.txt
D. command > output.txt

Solution

  1. Step 1: Identify output redirection syntax

    In bash, using > after a command sends its output to a file, overwriting it.
  2. Step 2: Check other options for correctness

    > is correct; >> appends, < reads input, | pipes output, so only > fits the question.
  3. Final Answer:

    command > output.txt -> Option D
  4. Quick Check:

    Output redirection uses > [OK]
Hint: Use > to write output to a file, overwriting it [OK]
Common Mistakes:
  • Using < instead of > for output
  • Confusing >> (append) with > (overwrite)
  • Using invalid symbols like |>
3. What will be the content of the file output.txt after running this script?
echo "Hello" > output.txt
echo "World" >> output.txt
medium
A. Hello World
B. World
C. Hello
D. HelloWorld

Solution

  1. Step 1: Analyze first echo command

    The first echo writes "Hello" to output.txt, overwriting any existing content.
  2. Step 2: Analyze second echo command

    The second echo appends "World" to output.txt, adding it on a new line.
  3. Final Answer:

    Hello World -> Option A
  4. Quick Check:

    First overwrite, then append with newline [OK]
Hint: > overwrites file, >> appends with newline [OK]
Common Mistakes:
  • Thinking >> overwrites instead of appends
  • Ignoring newline added by echo
  • Assuming output is just 'Hello' or 'World'
4. Identify the error in this script that tries to read from a file and print its content:
cat < input.txt
medium
A. The command should be 'cat >> input.txt' to append content.
B. The file name should be after the command without <, like 'cat > input.txt'.
C. The input redirection < is unnecessary; use 'cat input.txt' instead.
D. The script needs quotes around the file name.

Solution

  1. Step 1: Understand cat command usage

    cat reads and prints file content when given the filename as an argument.
  2. Step 2: Check input redirection usage

    Using < input.txt is valid but redundant here; 'cat input.txt' is simpler and standard.
  3. Final Answer:

    The input redirection < is unnecessary; use 'cat input.txt' instead. -> Option C
  4. Quick Check:

    cat filename is simpler than cat < filename [OK]
Hint: Use 'cat filename' to read files, not input redirection [OK]
Common Mistakes:
  • Confusing input redirection with output redirection
  • Using > or >> instead of < for input
  • Adding unnecessary quotes around filename
5. You want to create a script that reads a list of usernames from users.txt and writes a greeting for each user into greetings.txt. Which script snippet correctly does this?
hard
A. while read user; do echo "Hello, $user!" >> greetings.txt; done < users.txt
B. for user in users.txt; do echo "Hello, $user!" > greetings.txt; done
C. cat users.txt | echo "Hello, $user!" > greetings.txt
D. echo "Hello, $(cat users.txt)!" > greetings.txt

Solution

  1. Step 1: Understand reading lines from a file

    The 'while read user; do ... done < users.txt' reads each line (username) from users.txt correctly.
  2. Step 2: Check writing greetings

    Inside the loop, echo appends a greeting line to greetings.txt for each user.
  3. Final Answer:

    while read user; do echo "Hello, $user!" >> greetings.txt; done < users.txt -> Option A
  4. Quick Check:

    Loop reads lines and appends greetings [OK]
Hint: Use while read loop with input redirection and append >> [OK]
Common Mistakes:
  • Using for loop incorrectly on filename instead of lines
  • Overwriting greetings.txt inside loop instead of appending
  • Misusing cat with echo without looping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard