cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 168 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)
7 Replies
erich
Mission Specialist
Mission Specialist
  • 142 Views

#!/bin/bash

if id "$1" >/dev/null 2>&1; then
    echo "User $1 exists! no further operation needed."
else
    echo "User $1 not found on system! Creating..."
    sudo useradd -m $1
    sudo passwd $1
fi

  • 111 Views

hi

garvchaudhary
Flight Engineer
Flight Engineer
  • 92 Views

This script is great. I think we should add some sequence in script itself for strong password for every user.

  • 92 Views

Please find my script below:

#!/bin/bash

check_user() {
    local username=$1
    id $username >> /dev/null 2>&1
    output=$(echo $?)
    return $output
}

create_user() {
    local username=$1
    echo "User NOT exist"
    echo "Creating the User with a home directory:"
    sudo /sbin/useradd -m $username
    echo "Enter a strong password for the new user: "
    sudo passwd $username
}

echo "This script will check the user is exist in the system and create it if not exists"

echo "Enter the username: "

read username

if check_user "$username"; then
    echo "The user "$username" is exists"
    echo "User check completed. Thank You!"
else
    create_user "$username"
fi

  • 51 Views

This is my script for question :2.

#!/bin/bash

# Function to check password strength
check_password_strength() {
local password="$1"

if [[ ${#password} -ge 8 && "$password" =~ [A-Z] && "$password" =~ [a-z] && "$password" =~ [0-9] && "$password" =~ [^a-zA-Z0-9] ]]; then
return 0
else
return 1
fi
}

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

# Check if the user exists
if id "$username" &>/dev/null; then
echo "User '$username' already exists."
else
echo "User '$username' does not exist."
read -p "Do you want to create this user? (yes/no): " create_user

if [[ "$create_user" == "yes" ]]; then
# Prompt for a strong password
while true; do
read -s -p "Enter a strong password for the user: " password
echo
read -s -p "Confirm the password: " confirm_password
echo

if [[ "$password" != "$confirm_password" ]]; then
echo "Passwords do not match. Please try again."
elif check_password_strength "$password"; then
break
else
echo "Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one digit, and one special character."
fi
done

# Create the user and set the password
sudo useradd -m "$username" && echo "$username:$password" | sudo chpasswd

if [[ $? -eq 0 ]]; then
echo "User '$username' has been created successfully with a home directory."
else
echo "Failed to create user '$username'."
fi
else
echo "User creation canceled."
fi
fi

njain51
Mission Specialist
Mission Specialist
  • 30 Views

#!/bin/bash

# --- Constants ---
USERNAME_PROMPT="Enter the username to check or create: "
PASSWORD_PROMPT="Enter a strong password for the new user: "
USER_EXISTS_MSG="User '%s' already exists."
USER_CREATED_MSG="User '%s' created successfully."
USER_CREATION_FAILED_MSG="Failed to create user '%s'."
HOME_DIR_CREATED_MSG="Home directory created for user '%s'."
HOME_DIR_CREATION_FAILED_MSG="Failed to create home directory for '%s'."
INVALID_USERNAME_MSG="Invalid username. Please use only alphanumeric characters, underscores, and hyphens."
PASSWORD_TOO_SHORT_MSG="Password too short. Please enter a password with at least 8 characters."
PASSWORD_VALIDATION_FAILED_MSG="Password does not meet requirements (8 chars, at least 1 number and 1 letter)."
MIN_PASSWORD_LENGTH=8

# --- Functions ---

# Function to check if a username is valid
is_valid_username() {
local username="$1"
if [[ ! "$username" =~ ^[a-zA-Z0-9_-]+$ ]]; then
return 1 # Invalid username
fi
return 0 # Valid username
}


# Function to check password strength
is_strong_password() {
local password="$1"

if [[ ${#password} -lt "$MIN_PASSWORD_LENGTH" ]]; then
echo "$PASSWORD_TOO_SHORT_MSG" >&2
return 1
fi

if [[ ! "$password" =~ [0-9] ]]; then
return 1
fi

if [[ ! "$password" =~ [a-zA-Z] ]]; then
return 1
fi

return 0
}

# Function to prompt for a valid password
get_valid_password() {

local password=""

while true; do
read -r -s -p "$PASSWORD_PROMPT" password

echo #Move to the next line

if is_strong_password "$password"; then
break
else
echo "$PASSWORD_VALIDATION_FAILED_MSG" >&2
fi
done

echo "$password"
}

 

# Function to create a new user
create_user() {
local username="$1"
local password="$2"

# Create the user and their home directory
if sudo useradd -m "$username" &>/dev/null; then

if ! sudo usermod -p "$(echo "$password" | mkpasswd -s sha512)" "$username" &>/dev/null; then
echo "$USER_CREATION_FAILED_MSG" >&2
return 1
fi

printf "$USER_CREATED_MSG\n" "$username"

printf "$HOME_DIR_CREATED_MSG\n" "$username"
return 0 # User created successfully
else
printf "$USER_CREATION_FAILED_MSG\n" "$username" >&2
return 1 # User creation failed
fi
}


# --- Main Script ---

# Prompt for the username
read -r -p "$USERNAME_PROMPT" username

#Validate username
if ! is_valid_username "$username"; then
echo "$INVALID_USERNAME_MSG" >&2
exit 1
fi


# Check if the user exists
if id -u "$username" &>/dev/null; then
printf "$USER_EXISTS_MSG\n" "$username"
exit 0 # User exists, exit gracefully
fi

# If the user doesn't exist, prompt for password and create
password=$(get_valid_password)


if create_user "$username" "$password"; then
echo "" # Add a newline for better readability
echo "User '$username' is ready to use."
else
echo "User creation process failed. Exiting..." >&2
exit 1
fi


exit 0

  • 22 Views

#!/bin/bash

# Function to check if a user exists
check_user_exists() {
local username=$1
id "$username" &>/dev/null
}

# Function to create a user with a strong password
create_user() {
local username=$1
local password

while true; do
read -s -p "Enter a strong password for $username: " password
echo
read -s -p "Confirm password: " password_confirm
echo

if [[ "$password" == "$password_confirm" ]]; then
break
else
echo "Passwords do not match. Please try again."
fi
done

sudo useradd -m "$username"
echo "$username:$password" | sudo chpasswd
echo "User $username created successfully."
}

# Main script logic
read -p "Enter the username to check or create: " username

if check_user_exists "$username"; then
echo "User $username already exists."
else
echo "User $username does not exist. Creating user..."
create_user "$username"
fi

 

 

  1. Function check_user_exists: This function checks if a user exists by using the id command. If the user exists, the command returns 0; otherwise, it returns a non-zero status.

  2. Function create_user: This function prompts the user to enter a strong password and confirms the password. It then uses sudo useradd -m to create the user and chpasswd to set the password.

  3. Main Script Logic: Prompts for the username and checks if the user exists. If the user doesn't exist, it calls the create_user function.

 

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