dev-resources.site
for different kinds of informations.
File It Away: Groceries, Persistence, and Bash Mastery
Welcome to the bonus 4th installment of our Textual Healing series! By now, youâve already got a handle on some essential text manipulation tools, but today weâre taking things up a notch and talking about persistenceâno, not in the âkeep going when things get toughâ sense (though, kudos if youâre doing that too). We're talking about file storage and how to keep your data around even after your script finishes running.
So buckle upâtodayâs goal is simple: learn how to take user input, store it in a file, and manipulate that data over multiple script executions. As an example, weâll be building a script to manage a grocery list. Weâll show you how to add items, delete items, and persist the list in a file.
Letâs get into it.
Step 1: Taking Input from the User
First up, we need to capture what the user types. In Bash, this is super easy thanks to the read
command. Letâs start by asking our user for an item they want to add to their grocery list:
echo "What item do you want to add to the grocery list?"
read item
echo "You added: $item"
Easy enough, right? Now weâre going to save that to a file, because letâs be honestâno one has the time to retype their grocery list every time the script runs.
Step 2: Storing Input in a File
To keep our grocery list persistent, weâre going to write each item into a file called grocery_list.txt
. Every time we add something, weâll append it to the end of the file.
echo "$item" >> grocery_list.txt
echo "$item has been added to your grocery list."
Whatâs happening here? The >>
operator appends whatever $item
holds into the grocery_list.txt
file. If the file doesnât exist yet, Bash will create it for you. Persistence: achieved! đ
Step 3: Displaying the List
Letâs get fancy. Now that weâre adding items to the file, it would be helpful to display the full list back to the user.
echo "Here's your current grocery list:"
cat grocery_list.txt
This reads and outputs the content of grocery_list.txt
so you can see everything youâve added so far. And since weâve already covered the power of cat
in previous installments, you know how useful this is for text manipulation.
Step 4: Deleting an Item from the List
Now, letâs take it up a notch. What if you accidentally added "ice cream" to your list and, out of guilt, you want to remove it? Letâs build a way to delete items from the grocery list. First, letâs display all items and let the user choose which one they want to remove.
echo "Here are the items in your list:"
cat -n grocery_list.txt
echo "Enter the number of the item you want to delete:"
read number
The cat -n
command prints the list with line numbers, making it easy to pick an item to delete by its number. Next, weâll use sed
to remove the item based on that number:
sed -i "${number}d" grocery_list.txt
echo "Item $number has been removed."
The ${number}d
in sed
tells it to delete the specified line. The -i
flag makes the changes directly in the file. Warning: Use sed -i
with cautionâthereâs no trash bin in Bash, so once itâs deleted, itâs gone forever!
Step 5: Making It All WorkâPutting It Together
Hereâs a simplified version of what weâve covered so far. Weâll make a script where the user can add grocery items, view their list, or delete items. Hereâs how it looks:
#!/bin/bash
grocery_list="grocery_list.txt"
while true; do
echo "What do you want to do?"
echo "1) Add an item"
echo "2) View the list"
echo "3) Remove an item"
echo "4) Quit"
read choice
case $choice in
1)
echo "What item do you want to add?"
read item
echo "$item" >> "$grocery_list"
echo "$item has been added."
;;
2)
echo "Hereâs your grocery list:"
cat "$grocery_list"
;;
3)
echo "Hereâs your grocery list:"
cat -n "$grocery_list"
echo "Enter the number of the item you want to delete:"
read number
sed -i "${number}d" "$grocery_list"
echo "Item $number has been removed."
;;
4)
echo "Goodbye!"
break
;;
*)
echo "Invalid option. Try again."
;;
esac
done
This script gives the user full control over their grocery listâadding, viewing, and removing items as they please. Now itâs your turn to take these concepts and build something cool, like a to-do list or any other interactive CLI app. Youâve got the tools; go build!
Step 6: Making Your Script Executable
Before you run your script, youâll want to make sure itâs executable:
chmod +x your_script_name.sh
./your_script_name.sh
And there you go! Now your script is ready to use on-demand without needing to call bash
every time.
Conclusion
With this bonus installment of Textual Healing, you now have the power to capture user input, store it in a file, and manipulate that data across multiple script runs.
If you enjoyed this post and want to dive deeper into Bash, CLI tools, and Linux wizardry, join us over in The Developers Lounge! Weâve got a fantastic community of developers who love to code, tinker, and help each other grow. Whether youâre a beginner or an experienced dev, weâd love to have you! Join here.
Featured ones: