0
0
Bash Scriptingscripting~10 mins

User account management script in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - User account management script
Start Script
Check Input Arguments
Match Command: add|delete|list
Add User
Show Result
End Script
The script starts, checks the command given, runs the matching user action (add, delete, or list), then shows the result and ends.
Execution Sample
Bash Scripting
#!/bin/bash
if [ "$1" = "add" ]; then
  sudo useradd "$2"
  echo "User $2 added"
elif [ "$1" = "delete" ]; then
  sudo userdel "$2"
  echo "User $2 deleted"
else
  cut -d: -f1 /etc/passwd
fi
This script adds or deletes a user based on the first argument, or lists users if no valid command is given.
Execution Table
StepInput ArgumentsCondition CheckedAction TakenOutput
1add alice$1 == 'add'?Yes, run useradd aliceUser alice added
2delete bob$1 == 'add'?No, check $1 == 'delete'?No
3delete bob$1 == 'delete'?Yes, run userdel bobUser bob deleted
4list$1 == 'add'?NoNo
5list$1 == 'delete'?NoNo
6listElse branchRun cut to list users(list of users)
7unknown$1 == 'add'?NoNo
8unknown$1 == 'delete'?NoNo
9unknownElse branchRun cut to list users(list of users)
💡 Script ends after performing the matched action or listing users.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 6After Step 9
$1 (command)noneadddeletelistunknown
$2 (username)nonealicebobnonenone
Key Moments - 3 Insights
Why does the script list users when the command is unknown?
Because the else branch runs when $1 is not 'add' or 'delete', as shown in execution_table rows 6 and 9.
What happens if no username is given with 'add' or 'delete'?
The script tries to run useradd or userdel with an empty username, which will cause an error. This is not handled in the current script.
Why do we use sudo before useradd and userdel?
Because adding or deleting users requires admin rights. Without sudo, these commands will fail unless run as root.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output when the input is 'add alice'?
A(list of users)
BUser alice deleted
CUser alice added
DNo output
💡 Hint
Check row 1 in the execution_table for input 'add alice'.
At which step does the script run the command to delete a user?
AStep 3
BStep 6
CStep 1
DStep 9
💡 Hint
Look for the action 'run userdel bob' in the execution_table.
If the input is 'unknown', what branch does the script take?
AAdd user branch
BElse branch to list users
CDelete user branch
DScript exits without output
💡 Hint
See rows 7-9 in the execution_table for input 'unknown'.
Concept Snapshot
User account management script syntax:
if [ "$1" = "add" ]; then
  sudo useradd "$2"
elif [ "$1" = "delete" ]; then
  sudo userdel "$2"
else
  cut -d: -f1 /etc/passwd
fi

Use first argument as command, second as username.
Add/delete users or list all users if unknown command.
Full Transcript
This script starts by checking the first input argument. If it is 'add', it runs the command to add a user with the second argument as username and prints confirmation. If it is 'delete', it deletes the user named by the second argument and prints confirmation. If the command is anything else, it lists all users on the system by reading /etc/passwd. Variables $1 and $2 hold the command and username respectively. The script ends after performing the requested action. Using sudo is necessary for useradd and userdel commands. If no username is given for add or delete, the script will try to run the command with an empty username, which causes an error. This script is a simple example of user account management in bash.