cancel
Showing results for 
Search instead for 
Did you mean: 
Chetan_Tiwary_
Community Manager
Community Manager
  • 621 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)
14 Replies
  • 158 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"

  • 143 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"

ipalagin
Flight Engineer Flight Engineer
Flight Engineer
  • 113 Views

Here is my version:

~~~

#!/bin/bash
PATH=/usr/bin:/usr/sbin

# Ask for the user name
read -p "Enter the user name: " _USERNAME  

# Exit if the user exists
getent passwd ${_USERNAME} >/dev/null && echo -e "\nUser \"${_USERNAME}\" exists\n"

# Prompt to create new user if it doesn't exist
read -p "User \"${_USERNAME}\" doesn't exist, do you want it created? (y/n) " _CREATEYN
if [[ ${_CREATEYN} != 'y' ]]
then
 echo -e "\nNOTHING TO DO: New user won't be created\n"
 exit 0
fi

# Create new user
sudo useradd -m ${_USERNAME}

# Set the password
while true
do

 read -p "Enter a strong password for the new account: " -s _NEWPASSWD
  
 # Analyze the password with cracklib
 _passwdcheck=$(echo ${_NEWPASSWD} | cracklib-check | awk '{print $2}' )

 if [[ ${_passwdcheck} == "OK" ]]
 then
   echo -e "\nOK: The password is strong, proceeding...\n"
   echo "${_USERNAME}:${_NEWPASSWD}" | sudo chpasswd
   break
 else
   echo -e "\n ERROR: You've entered a weak password, please try again!\n"
 fi  
done

echo -e "DONE: ${_USERNAME} was created, and the password was set\n"
exit 0

~~~

 

Numan
Cadet
Cadet
  • 84 Views

# vim user-creation-script.sh


#!/bin/bash

check_user_exists() {
    id "$1" &>/dev/null
}

create_password() {
     local password=$(tr -dc 'A-Za-z0-9!@#$%^&*()_+~' </dev/urandom | head -c 12)
     echo "$password"
}

read -p "Enter the username to check: " username

if check_user_exists "$username"; then
         echo "User '$username' exists in the system."
else
         echo "User '$username' does not exist."

         read -p "Do you want to create this user? (y/n): " response
         if [[ "$response" =~ ^[Yy]$ ]]; then
                password=$(create_password)
                useradd -m "$username"
                echo "$username:$password" | chpasswd
                echo "User '$username' has been created successfully."
                echo "The generated password is: $password"
                echo "Please make sure to note down the password or change it later."
         else
                echo "User creation aborted."
         fi
fi

wq!

# chmod +x user-creation-script.sh
# ./user-createion-script.sh

FelipeHenriquez
Mission Specialist
Mission Specialist
  • 46 Views

 

#!/bin/bash

# checking  if a user exists
user_exists() {
id "$1" &>/dev/null
}

# checking strong passwords
is_strong_password() {
local password="$1"
[[ ${#password} -ge 8 && "$password" =~ [A-Z] && "$password" =~ [a-z] && "$password" =~ [0-9] && "$password" =~ [^a-zA-Z0-9] ]]
}

# enter  username
read -p "give username : " username

if user_exists "$username"; then
echo "User '$username' already exists."
else
echo "User '$username' does not exist."

# create useer
read -p "create this user? (yes/no): " response
if [[ "$response" =~ ^[Yy][Ee][Ss]$ || "$response" =~ ^[Yy]$ ]]; then
# Loop for strong password 
while true; do
read -s -p "give  a strong pass for the  new user: " password
echo
if is_strong_password "$password"; then
break
else
echo "Password must be at least 8 characters long and include uppercase, lowercase, a number, and a special character."
fi
done

# Create the user with home directory
sudo useradd -m "$username"
echo "$username:$password" | sudo chpasswd
echo "a new User '$username' was created  "
else
echo "cancelled."
fi
fi

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