0
0
Bash Scriptingscripting~15 mins

Why scripts often process text in Bash Scripting - See It in Action

Choose your learning style9 modes available
Why Scripts Often Process Text
📖 Scenario: You work as a system helper who needs to check and organize information stored in text files on a computer. Many system details and logs are saved as text, so scripts help to read and manage this text quickly.
🎯 Goal: Build a simple Bash script that reads a list of usernames and their login counts, then filters and shows only those users who logged in more than a certain number of times.
📋 What You'll Learn
Create a text data variable with usernames and login counts
Add a threshold variable to filter users by login count
Use a loop to process each line of the text data
Print only users with login counts above the threshold
💡 Why This Matters
🌍 Real World
System administrators often write scripts to read and analyze text logs or user data to monitor system usage or detect issues.
💼 Career
Knowing how to process text in scripts is essential for automating tasks, managing servers, and handling data in many IT and DevOps roles.
Progress0 / 4 steps
1
Create the user login data
Create a Bash variable called user_data that contains these exact lines of text, each with a username and login count separated by a space:
alice 5
bob 3
carol 8
dave 2
Bash Scripting
Need a hint?

Use double quotes and \n to separate lines inside the variable.

2
Set the login count threshold
Create a variable called threshold and set it to 4 to filter users who logged in more than 4 times.
Bash Scripting
Need a hint?

Just assign the number 4 to the variable threshold.

3
Filter users by login count
Use a while loop with read to process each line of user_data. Inside the loop, split each line into user and count. Then use an if statement to check if count is greater than threshold. If yes, print the user and count separated by a space.
Bash Scripting
Need a hint?

Use done <<< "$user_data" to feed the variable lines into the loop.

4
Print the filtered users
Run the script so it prints only the users with login counts greater than the threshold. The output should show each qualifying user and their count on a separate line.
Bash Scripting
Need a hint?

The script should print only users with counts above 4.