forked from devopsbytanishka/linux-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_management.sh
More file actions
75 lines (73 loc) · 2.3 KB
/
user_management.sh
File metadata and controls
75 lines (73 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# Function to display usage information and available options
function display_usage {
echo "Usage: $0 [OPTIONS]"
echo "options":
echo " -c, --create Create a new user account."
echo " -d, --delete Delete an existing user account."
echo " -l, --list List all user accounts on the system."
echo " -h, --help Display this help and exit."
}
# Function to create a new user account
create_user() {
read -p "Enter the new username: " username
# Check if the user name already exists
if id "$username" &>/dev/null; then
echo "Error: The username '$username' already exists. Please choose a different username."
return 1
fi
# Prompt for password (Note: you might want to use 'read -s' to hide the password input)
read -p "Enter the password for $username: " password
# Create the user account using sudo
if sudo useradd -m -p "$password" "$username"; then
echo "User account '$username' created successfully."
else
echo "Error: Failed to create user account '$username'."
return 1
fi
}
# Function to delete an existing user account
function delete_user {
read -p "Enter the username to delete: " username
# Check if the username exists
if id "$username" &>/dev/null; then
if sudo userdel -r "$username" 2>/dev/null; then # Redirect stderr to /dev/null
echo "User account '$username' deleted Successfully."
else
echo "Error: Failed to delete user account '$username'."
return 1
fi
else
echo "Error: The username '$username' does not exist. Please enter a valid username."
return 1
fi
}
#function to list all user accounts on the system.
function list_users {
echo "User account on the system:"
cat /etc/passwd | awk -F: '{print "- " $1 " (UID: " $3 ")"}'
}
#check if no arguments are provided or if the -h or --help option is given
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
display_usage
exit 0
fi
#command-line argument parsing
while [ $# -gt 0 ]; do
case "$1" in
-c| --create)
create_user
;;
-d| --delete)
delete_user
;;
-l| --list)
list_users
;;
*)
echo "Error: Invalid option "$1". Use "-h" to see available options."
exit 1
;;
esac
shift
done