Skip to content

Day 6 submission #34

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 13 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
44 changes: 44 additions & 0 deletions Challenges/Day_1/my_solution_day1.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!bin/bash/

#task1- comments
#this is BashBlazeChallenge- Day1

#task2- echo
#echo prints on the screen
echo "This is BashBlazeChallenge- Day1"

#task3 - variables
name="Shivani"
yeo=2

#task4- using variables
echo "My name is $name and YEO is $yeo"

a=13 b=89
sum=$(($a+$b))
echo "The sum of $a and $b is $sum"

#task5 - using built-in variables
echo "My current working directory is $PWD"
echo "My home directory is $HOME"
echo "The PID of this bash process is $$"

#task6 - using wildcards
echo "making some files:"
touch day{8..11}.txt
touch hello.sh ls

echo "Using wildcard- ls .sh"
ls *.sh

echo "Using wildcard- ls .txt"
ls *.txt

echo "Using ? wildcard- ls day?.txt"
ls day?.txt

echo "Using [] wildcard- ls day[1-10].txt"
ls day[1-10].txt

echo "Using ? wildcard- ls day*"
ls day*
47 changes: 13 additions & 34 deletions Challenges/Day_2/backup_with_rotation.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,20 @@
#!/bin/bash
#!bin/bash/

# Function to display usage information and available options
function display_usage {
echo "Usage: $0 /path/to/source_directory"
}
#we will take source path from user and gets stored in $1
src_path=$1

# Check if a valid directory path is provided as a command-line argument
if [ $# -eq 0 ] || [ ! -d "$1" ]; then
echo "Error: Please provide a valid directory path as a command-line argument."
display_usage
exit 1
fi
#we will store the current date and time in 2 variables
fname="$(date '+%Y-%m-%d')"
time="$(date '+%I-%M-%S')"

# Directory path of the source directory to be backed up
source_dir="$1"
#storing the name of the backup file in f_name
f_name=backup_$fname"_"$time.tar.gz

# Function to create a timestamped backup folder
function create_backup {
local timestamp=$(date '+%Y-%m-%d_%H-%M-%S') # Get the current timestamp
local backup_dir="${source_dir}/backup_${timestamp}"
#this line will zip the files present in $src_path
tar -czf $f_name $src_path

# Create the backup folder with the timestamped name
mkdir "$backup_dir"
echo "Backup created successfully: $backup_dir"
}
echo "Backup Created: $PWD/$f_name"

# Function to perform the rotation and keep only the last 3 backups
function perform_rotation {
local backups=($(ls -t "${source_dir}/backup_"* 2>/dev/null)) # List existing backups sorted by timestamp
#deleting the files except the 3 latest backup files
rm -f $(ls -1t *.tar.gz | tail -n +4)

# Check if there are more than 3 backups
if [ "${#backups[@]}" -gt 3 ]; then
local backups_to_remove="${backups[@]:3}" # Get backups beyond the last 3
rm -rf "${backups_to_remove[@]}" # Remove the oldest backups
fi
}

# Main script logic
create_backup
perform_rotation
33 changes: 16 additions & 17 deletions Challenges/Day_2/explorer.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#!/bin/bash
#!bin/bash/

# Part 1: File and Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!"

while true; do
# List all files and directories in the current path
echo "Files and Directories in the Current Path:"
ls -lh
#ls -lh will give us the list of files and directories with their size in human readable format
# awk will help us to print the 9th and 5th column of that output
echo "Files and Directories in the Current Path:"
ls -lh | awk '{print "-",$9,"(",$5,")"}'

# Part 2: Character Counting
read -p "Enter a line of text (Press Enter without text to exit): " input
#checking if the length of msg variable is 0, it will break out of the loop
while [ True ];
do
read -p "Enter a line of text(Press enter without text to exit): " msg
if [ ${#msg} -eq 0 ]
then
break
fi
echo "Character Count: ${#msg} \n"
done

# Exit if the user enters an empty string
if [ -z "$input" ]; then
echo "Exiting the Interactive Explorer. Goodbye!"
break
fi
echo "Exiting the Interactive Explorer. Goodbye!"

# Calculate and print the character count for the input line
char_count=$(echo -n "$input" | wc -m)
echo "Character Count: $char_count"
done
183 changes: 105 additions & 78 deletions Challenges/Day_3/user_management.sh
Original file line number Diff line number Diff line change
@@ -1,94 +1,121 @@
#!/bin/bash

# Function to display usage information and available options
function display_usage {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " -c, --create Create a new user account."
echo " -d, --delete Delete an existing user account."
echo " -r, --reset Reset password for an existing user account."
echo " -l, --list List all user accounts on the system."
echo " -h, --help Display this help and exit."
#this will be called when the user inputs nothing, -h or --help
help(){
echo "Usage: ./user_management.sh [OPTIONS]"
echo "Options:"
echo " -c, --create Create a new user account."
echo "-d, --delete Delete and existing user account."
echo "-r, --reset Reset password for an existing user account."
echo "-l, --list List all user accounts on the system."
echo "-h, --help Display this help and exit."
}

# Function to create a new user account
function create_user {
read -p "Enter the new username: " username
#this will be called when the user inputs -c or --create
create(){
read -p "Enter the new username: " username

# Check if the username already exists
if id "$username" &>/dev/null; then
echo "Error: The username '$username' already exists. Please choose a different username."
else
# Prompt for password (Note: You might want to use 'read -s' to hide the password input)
read -p "Enter the password for $username: " password
#this line will check if the user exists or not
a=`grep -w $username /etc/passwd`

# Create the user account
useradd -m -p "$password" "$username"
echo "User account '$username' created successfully."
fi
}

# Function to delete an existing user account
function delete_user {
read -p "Enter the username to delete: " username
#the length of "a" will not be zero if the user already exists
if [ ${#a} -ne 0 ]
then echo "Error: The username $username already exists. Please choose a different username."
else
#this will create the user as the user doesnt exist
read -p "Enter the password for $username: " password
sudo useradd $username

# Check if the username exists
if id "$username" &>/dev/null; then
userdel -r "$username" # -r flag removes the user's home directory
echo "User account '$username' deleted successfully."
else
echo "Error: The username '$username' does not exist. Please enter a valid username."
fi
#this will give password to the command and any output will be not displayed on terminal
echo -e "$password\n$password" | sudo passwd $username 2>>/dev/null
echo "User account '$username' created successfully."
fi
}

# Function to reset the password for an existing user account
function reset_password {
read -p "Enter the username to reset password: " username
#this will be called when the user inputs -d or --delete
delete(){
read -p "Enter the username to delete: " uname_del

# Check if the username exists
if id "$username" &>/dev/null; then
# Prompt for password (Note: You might want to use 'read -s' to hide the password input)
read -p "Enter the new password for $username: " password
#this line will check if the user exists or not
a=`grep -w $uname_del /etc/passwd`

#the length of "a" will not be zero if the user already exists
if [ ${#a} -eq 0 ]
then echo "Error: The username $uname_del doesn't exist. Please enter a valid username."
else
#this will delete the user as the user exists
sudo userdel $uname_del
echo "User account $uname_del deleted successfully"
fi
}

# Set the new password
echo "$username:$password" | chpasswd
echo "Password for user '$username' reset successfully."
else
echo "Error: The username '$username' does not exist. Please enter a valid username."
fi
#this will be called when the user inputs -r or --reset
reset(){
read -p "Enter the username to reset password: " uname_res

#this line will check if the user exists or not
a=`grep -w $uname_res /etc/passwd`

#the length of "a" will not be zero if the user already exists
if [ ${#a} -eq 0 ]
then echo "Error: The username $uname_res doesn't exist. Please enter a valid username."
else
#this will reset the password of the user as the user exists
read -p "Enter the new password for $uname_res: " newp
echo -e "$newp\n$newp" | sudo passwd $uname_res 2>>/dev/null
echo "Password for user '$uname_res' reset successfully."
fi
}

# Function to list all user accounts on the system
function list_users {
echo "User accounts on the system:"
cat /etc/passwd | awk -F: '{ print "- " $1 " (UID: " $3 ")" }'
#this will be called when the user inputs -l or --list
list(){
#this will print the 1st and 3rd column from the /etc/passwd file with required formatting.
awk -F: '{print "- "$1,"(UID: "$3")"}' /etc/passwd
}

# Check if no arguments are provided or if the -h or --help option is given
if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
display_usage
exit 0
fi
#we will use better than if-elif to call the functions based on the arguemnt passed
#in case, we can use "and" to combine conditions, i have written multiple times.
case "$1" in
"")
help
;;
"-h")
help
;;
"--help")
help
;;
"--create")
create
;;
"-c")
create
;;
"--delete")
delete

;;
"-d")
delete
;;
"--reset")
reset
;;
"-r")
reset
;;
"--list")
list
;;
"-l")
list
;;
*)
echo "This is not a valid option. Please check usage of this script below -"
help
;;
esac



# Command-line argument parsing
while [ $# -gt 0 ]; do
case "$1" in
-c|--create)
create_user
;;
-d|--delete)
delete_user
;;
-r|--reset)
reset_password
;;
-l|--list)
list_users
;;
*)
echo "Error: Invalid option '$1'. Use '--help' to see available options."
exit 1
;;
esac
shift
done

Loading