📖 Scenario: You work as a helper for a small shop. The shop keeps a list of products and their prices in a text file. You want to write a script that reads this file, finds products that cost more than a certain amount, and saves those products to a new file. This helps the shop owner quickly see expensive items.
🎯 Goal: Build a bash script that reads a file with product prices, filters products above a price limit, and writes them to a new file.
📋 What You'll Learn
Create a file named products.txt with exact product and price lines
Create a variable price_limit with a specific value
Use a while loop to read products.txt line by line
Write products with price greater than price_limit to expensive.txt
Print the contents of expensive.txt at the end
💡 Why This Matters
🌍 Real World
Scripts often read and write files to automate tasks like filtering logs, processing data, or generating reports.
💼 Career
Knowing file input/output is essential for automation roles, system administration, and any scripting job to handle real data.
Progress0 / 4 steps
1
Create the product list file
Create a file named products.txt with these exact lines: Apple 50 Banana 20 Cherry 75 Date 30 Elderberry 90
Bash Scripting
Hint
Use cat with a here-document to create the file with exact lines.
2
Set the price limit variable
Create a variable called price_limit and set it to 40.
Bash Scripting
Hint
Use price_limit=40 to set the variable.
3
Filter products above the price limit
Use a while loop with read to read products.txt line by line. For each line, split into product and price. If price is greater than price_limit, append the line to a file named expensive.txt. Clear expensive.txt before the loop starts.
Bash Scripting
Hint
Use while read product price; do ... done < products.txt and test price with [ "$price" -gt "$price_limit" ].
4
Display the filtered products
Print the contents of expensive.txt using cat.
Bash Scripting
Hint
Use cat expensive.txt to show the filtered products.
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
Step 1: Understand file I/O purpose
File I/O lets scripts handle data stored in files, such as logs or reports.
Step 2: Connect file I/O to automation
By reading and writing files, scripts can automate many tasks involving data processing.
Final Answer:
It allows scripts to read from and write to files, automating tasks. -> Option B
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
Step 1: Identify output redirection syntax
In bash, using > after a command sends its output to a file, overwriting it.
Step 2: Check other options for correctness
> is correct; >> appends, < reads input, | pipes output, so only > fits the question.
Final Answer:
command > output.txt -> Option D
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?
The first echo writes "Hello" to output.txt, overwriting any existing content.
Step 2: Analyze second echo command
The second echo appends "World" to output.txt, adding it on a new line.
Final Answer:
Hello
World -> Option A
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
Step 1: Understand cat command usage
cat reads and prints file content when given the filename as an argument.
Step 2: Check input redirection usage
Using < input.txt is valid but redundant here; 'cat input.txt' is simpler and standard.
Final Answer:
The input redirection < is unnecessary; use 'cat input.txt' instead. -> Option C
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
Step 1: Understand reading lines from a file
The 'while read user; do ... done < users.txt' reads each line (username) from users.txt correctly.
Step 2: Check writing greetings
Inside the loop, echo appends a greeting line to greetings.txt for each user.
Final Answer:
while read user; do echo "Hello, $user!" >> greetings.txt; done < users.txt -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.