0
0
Bash Scriptingscripting~30 mins

Why file I/O is core to scripting in Bash Scripting - See It in Action

Choose your learning style9 modes available
Why file I/O is core to scripting
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use cat expensive.txt to show the filtered products.