Repository navigation
Expand file tree
/
Copy pathuser-creation.sh
More file actions
111 lines (91 loc) · 2.72 KB
/
Copy pathuser-creation.sh
File metadata and controls
111 lines (91 loc) · 2.72 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Functions in this source:
# creation_prompt
# create_user
# select_password
# Sudo required to run script
if [ $(id -u) -ne 0 ]; then
echo "Script failed. Super user required."
exit -1
fi
# prompt username creation
function creation_prompt {
read -p "Enter new username: " username
read -p "Enter full name: (e.g. John Smith) " name_of_user
}
function create_user {
# This function checks if a user exists and creates it if it doesn't
echo "Checking if $username is valid."
# ensure username only contains valid alphanumerical characters
if [[ ! "$username" =~ ^[a-zA-Z0-9_]+$ ]]; then
echo "$username is not valid."
echo
echo "You may only use alpha-numeric characters. (A-z, 0-9)."
echo
echo "Exiting."
exit -1
fi
if [[ ! $name_of_user =~ ^[A-Za-z\ ]+$ ]]; then
echo "$name_of_user is not valid."
echo
echo "You may only use the Latin alphabet and spaces. (A–Z or a–z)."
echo
echo "Sorry hyphenated and apostrophed names, I couldn't get the regex working."
echo
echo "Exiting."
exit 1
fi
# check for availability
echo "Checking if $username exists."
if grep -q "^${username}:" /etc/passwd; then
echo "$username is not available."
read -p "Do you want to try again? (Y/N): " answer
case "$answer" in
[Yy]*)
create_user
;;
[Nn]*)
echo "Script cancelled. Exiting."
exit 0
;;
*)
echo "Invalid response. Exiting."
exit -1
;;
esac
else
echo
echo "$username is available. Creating user $username."
echo
useradd -m "$username" -c "$name_of_user"
if grep -q "^${username}:" /etc/passwd; then
echo "$username successfully created!"
else
echo "Failed to create account for username $username."
echo "Script failed."
echo
echo "Exiting."
exit -1
fi
fi
}
function select_password {
passwd $username
}
# function end_user_creation {
# read -p "Would you like to configure settings for "$username"? (Y/N): " answer
# case "$answer" in
# [Yy]*)
# ./user-admin.sh
# ;;
# [Nn]*)
# echo "Script finished. Exiting."
# exit 0
# ;;
# *)
# echo "Invalid response. Exiting."
# exit -1
# ;;
# esac
# exit
# }