Skip to content

Apurv day3 #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions Challenges/Day_3/user_mamagement.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#Author : Apurv Samadder
#Date : Aug 2 2023
#Version: v1
#Day 3 Bash Scripting Challenge - User Account Management.



#!/bin/bash

#Create a user
add_user()
{
echo "enter username "
read username

#echo "Enter password"
#read pass

if grep "${username}" /etc/passwd >/dev/null 2>&1; then

echo "User alread present"

else
echo "enter password"
read pass
useradd -m -p $pass $username && echo "Successfully added user"

fi
}

#delete user

delete_user()
{

if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root or with sudo privileges."
exit 1
fi

# Prompt the user for the username to delete
read -p "Enter the username you want to delete: " username

# Check if the username exists
if id "$username" &>/dev/null; then
# Delete the user and their home directory
userdel -r "$username"
echo "User '$username' and home directory deleted."
else
echo "User '$username' does not exist."
fi
}


reset_passwd()
{

if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root or with sudo privileges."
exit 1
fi

# Prompt the user for the username to changes password
read -p "Enter the username you want to delete: " username

# Check if the username exists
if id "$username" &>/dev/null; then
# Changes the password if user exist
passwd "$username"
echo "User '$username' Password changed."
else
echo "User '$username' does not exist."
fi
}

list_user()
{
# Read uid column /etc/passwd
for userid in `awk -F: '{print $3}' /etc/passwd`
do
if ("$userid" >= 1000); then
echo "Valid User" :`cat /etc/passwd | grep $userid | awk -F: '{print $1,$3}'`
fi
done
}


user_mgmt()
{
echo "options"
echo "-c,--create create a new user accoutn"
echo "-d, --delete Delete and existing user account"
echo "-r, --reset Reset a password for existing user"
echo "-l, --list List all the user in system"
echo "-h, --help Display this help and exit"

read option

case $option in
-c|--create)
add_user
;;
-d|--delete)
echo "inside delete"
delete_user
;;
-r|--reset)
echo "inside reset"
reset_passwd
;;
-l|--list)
list_user
echo "inside list"
;;
-h|--help)
echo "inside help"
;;
*)
echo "please enter valid option"
user_mgmt
;;
esac
}

user_mgmt


94 changes: 0 additions & 94 deletions Challenges/Day_3/user_management.sh

This file was deleted.