0
0
Bash Scriptingscripting~30 mins

User account management script in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
User account management script
📖 Scenario: You are a system administrator who needs to manage user accounts on a Linux server. You want to automate the process of checking user accounts and their statuses.
🎯 Goal: Create a Bash script that stores a list of user accounts with their statuses, sets a status filter, filters the users based on that status, and then prints the filtered list.
📋 What You'll Learn
Create an associative array called users with usernames as keys and their statuses as values.
Create a variable called filter_status to hold the status to filter users by.
Use a for loop to iterate over the users array and select users whose status matches filter_status.
Print the filtered usernames and their statuses.
💡 Why This Matters
🌍 Real World
System administrators often need to manage user accounts and their statuses on servers. Automating this with scripts saves time and reduces errors.
💼 Career
Knowing how to write scripts for user account management is valuable for roles in IT support, system administration, and DevOps.
Progress0 / 4 steps
1
Create the user accounts data
Create an associative array called users with these exact entries: alice with status active, bob with status inactive, carol with status active, and dave with status inactive.
Bash Scripting
Need a hint?

Use declare -A users to create an associative array in Bash.

2
Set the status filter
Create a variable called filter_status and set it to the string active.
Bash Scripting
Need a hint?

Assign the string active to the variable filter_status.

3
Filter users by status
Use a for loop with the variable user to iterate over the keys of the users array. Inside the loop, use an if statement to check if the user's status matches filter_status. If it matches, add the user and their status to a new associative array called filtered_users.
Bash Scripting
Need a hint?

Use for user in "${!users[@]}" to loop over keys of an associative array.

4
Print the filtered users
Use a for loop with the variable user to iterate over the keys of the filtered_users array. Inside the loop, print the username and their status in the format: user: status.
Bash Scripting
Need a hint?

Use echo "$user: ${filtered_users[$user]}" inside the loop to print each filtered user.