cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 305 Views

Solve this Shell Scripting Challenge.

Write a Bash shell script that checks if a user exists in your system. If the user doesn't exist, the script should prompt the user to create the user with a strong password. The script should also ensure that the user's home directory is created.

More marks for clarity, reusability and readability!

Labels (2)
11 Replies
  • 38 Views

#!/bin/bash

# Function to check if a user exists
user_exists() {
local username=$1
if id "$username" &>/dev/null; then
return 0 # User exists
else
return 1 # User does not exist
fi
}

# Function to create a strong password using openssl
generate_strong_password() {
# Generate a 16-character password using openssl
local password
password=$(openssl rand -base64 16) # 16-byte base64 password
echo "$password"
}

# Function to create a user
create_user() {
local username=$1
# Check if user already exists
if user_exists "$username"; then
echo "User '$username' already exists on the system."
return
fi

# Ask for confirmation to create the user
echo "User '$username' does not exist."
read -p "Do you want to create this user? (y/n): " create_choice

if [[ "$create_choice" =~ ^[Yy]$ ]]; then
# Generate a strong password
password=$(generate_strong_password)
echo "Generated strong password: $password"

# Create the user with a home directory and set the password
sudo useradd -m -s /bin/bash "$username" # Create user with home dir
echo "$username:$password" | sudo chpasswd # Set the user's password

# Optionally, you can force the user to change password on first login
sudo chage -d 0 "$username"

echo "User '$username' created successfully with a strong password."
echo "Please store this password securely."
else
echo "User creation cancelled."
fi
}

# Main script logic

# Prompt for username
read -p "Enter the username to check: " username

# Create user if necessary
create_user "$username"

  • 23 Views

#!/bin/bash

# Function to check if a user exists
function check_user_exists {
# Check if the username is given as input
if [ -z "$1" ]; then
echo "No username provided."
exit 1
fi

# Checking if the user exists in the system
if id "$1" &>/dev/null; then
echo "User '$1' already exists."
exit 0
else
echo "User '$1' does not exist."
create_user "$1"
fi
}

# Function to create a user
function create_user {
# Prompt for the username
local username="$1"

# Prompt for a strong password
while true; do
read -s -p "Enter password for user '$username': " password
echo
read -s -p "Confirm password: " password2
echo

if [[ "$password" == "$password2" ]]; then
# Check for password strength (at least 8 characters, 1 uppercase, 1 number, and 1 special character)
if [[ ${#password} -ge 8 && "$password" == *[[:upper:]]* && "$password" == *[[:digit:]]* && "$password" == *[![:alnum:]]* ]]; then
# Create the user with the provided password
sudo useradd -m -p "$(openssl passwd -1 $password)" "$username"

if [ $? -eq 0 ]; then
echo "User '$username' created successfully with home directory."
else
echo "Failed to create user '$username'."
fi
break
else
echo "Password is not strong enough. Please try again."
fi
else
echo "Passwords do not match. Please try again."
fi
done
}

# Main Script Execution
# Prompt for a username
read -p "Enter the username to check: " user_input
check_user_exists "$user_input"

0 Kudos
Join the discussion
You must log in to join this conversation.