blob: 9793155285fca79b09d42e202e8993f43c0aa63d (
plain)
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
|
#!/bin/sh
# My Arch Linux installation script
# by David T. Sadler <david@davidtsadler.com>
# License: GNU GPLv3
### OPTIONS AND VARIABLES ###
### FUNCTIONS ###
read_password() {
PASSWORD="$(
# always read from the tty even when redirected:
exec < /dev/tty || exit # || exit only needed for bash
# save current tty settings:
tty_settings=$(stty -g) || exit
# schedule restore of the settings on exit of that subshell
# or on receiving SIGINT or SIGTERM:
trap 'stty "$tty_settings"' EXIT INT TERM
# disable terminal local echo
stty -echo || exit
# prompt on tty
printf "$1" > /dev/tty
# read password as one line, record exit status
IFS= read -r password; ret=$?
# display a newline to visually acknowledge the entered password
echo > /dev/tty
# return the password for $REPLY
printf '%s\n' "$password"
exit "$ret"
)"
}
get_username_and_password() {
printf "Enter a name for the user account: "
read name
while ! echo "$name" | grep "^[a-z_][a-z0-9_-]*$" >/dev/null 2>&1; do
unset name
printf "Username not valid. Give a username beginning with a letter, with only lowercase letters, - or _.\n\n"
printf "Enter a name for the user account: "
read name
done
read_password "Enter a password for that user: "
pass1=$PASSWORD
read_password "Retype password: "
pass2=$PASSWORD
while ! [ "$pass1" = "$pass2" ]; do
unset pass2
echo "Passwords do not match."
read_password "Enter a password again: "
pass1=$PASSWORD
read_password "Retype password: "
pass2=$PASSWORD
done
}
add_user() {
get_username_and_password
[ -d "/home/$name" ] && echo "Skipping adding of user as $name already exists" && return
useradd -m -s /bin/zsh "$name" >/dev/null 2>&1 || mkdir -p /home/"$name" && chown "$name":"$name" /home/"$name"
usermod -aG wheel "$name"
echo "$name:$pass1" | chpasswd
unset pass1 pass2
}
# Remove un-needed files and directories from the home directory.
clean_user_home_directory() {
[ -f "/home/$name/.bash_history" ] && rm /home/$name/.bash_history
[ -f "/home/$name/.bash_logout" ] && rm /home/$name/.bash_logout
[ -f "/home/$name/.bash_profile" ] && rm /home/$name/.bash_profile
[ -f "/home/$name/.bashrc" ] && rm /home/$name/.bashrc
}
# Sets up the home directory for the user account.
setup_user_home_directory() {
clean_user_home_directory
}
### THE ACTUAL SCRIPT ###
add_user
setup_user_home_directory
|