forked from techarkit/shell-scripting-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseradd_improved.sh
More file actions
37 lines (29 loc) · 871 Bytes
/
useradd_improved.sh
File metadata and controls
37 lines (29 loc) · 871 Bytes
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
#!/bin/bash
## Author: Ankam Ravi Kumar
## Date: 21st Sep 2024
## Purpose: To Create a users in Linux
check_command_success() {
if [ $? -ne 0 ]; then
echo "Error: $1"
exit 1
fi
}
if [ $(id -u) -ne 0 ]; then
echo "Error: Only root may add a user to the system."
exit 2
fi
# Prompt for the username and password
read -p "Enter username: " username
read -s -p "Enter password: " password
echo
if id "$username" &>/dev/null; then
echo "Error: User '$username' already exists!"
exit 1
fi
encrypted_password=$(perl -e 'print crypt($ARGV[0], "password")' "$password")
check_command_success "Failed to encrypt the password."
useradd -m -p "$encrypted_password" "$username"
check_command_success "Failed to add the user."
passwd -e "$username"
check_command_success "Failed to set password expiry."
echo "Success: User '$username' has been added to the system!"