DEV Community

Cover image for File It Away: Groceries, Persistence, and Bash Mastery
Jimmy McBride
Jimmy McBride Subscriber

Posted on

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"
Enter fullscreen mode Exit fullscreen mode

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."
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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."
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)