Skip to content
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
61 changes: 22 additions & 39 deletions Challenges/Day_2/backup_with_rotation.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,24 @@
#!/bin/bash

# Function to display usage information and available options
function display_usage {
echo "Usage: $0 /path/to/source_directory"
}

# 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

# Directory path of the source directory to be backed up
source_dir="$1"

# 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}"
###################################################################
# Author: Sasiram Beeke
# Date: 01/08/2023
# Description: Interactive File and Directory Explorer
# Tip: to execute shell give permision +x scriptname
###################################################################

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

# 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

# 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
#!/bin/bash
# accept argument and assing it to source_dir variable
source_dir=$1
# create timestamp and store it to timestamp variable
timestamp=$(date +%Y-%m-%d-%H-%M-%S)
# Create the backup folder name using the timestamp
dir_name="backup_${timestamp}"
backup_folder="${source_dir}/${dir_name}"
mkdir "${backup_folder}"
#The script uses rsync to copy the contents of the source directory to the backup folder.
#The rsync command ensures that the backup folder itself is excluded from the copy.
rsync -av --exclude="${dir_name}" "$source_dir/" "$backup_folder"
# Find and remove older backups, retaining only the last 3 backups
find "$source_dir" -maxdepth 1 -type d -name "backup_*" | sort | head -n -3 | xargs rm -rf
# Print a success message
echo "Backup created: $backup_folder"
32 changes: 16 additions & 16 deletions Challenges/Day_2/explorer.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#!/bin/bash

# Part 1: File and Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!"
###################################################################
# Author: Sasiram Beeke
# Date: 01/08/2023
# Description: Interactive File and Directory Explorer
# Tip: to execute shell give permision +x scriptname
###################################################################

#!/bin/bash
echo "Welcome to the Interactive File and Directory Explorer!"
echo "Files and Directories in the Current Path:"
ls -lh| awk '{print $9, "(" $5 ")"}'
while true; do
# List all files and directories in the current path
echo "Files and Directories in the Current Path:"
ls -lh

# Part 2: Character Counting
read -p "Enter a line of text (Press Enter without text to exit): " input
# Ask the user if they want to exit the explorer
read -p "Enter a line of text (press Enter without text to exit):" input

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

# Calculate and print the character count for the input line
char_count=$(echo -n "$input" | wc -m)
echo "Character Count: $char_count"
echo "Character count:$(echo -n "$input" | wc -m)"
done
echo "Exiting the Interactive Explorer. Goodbye!"
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file added Challenges/Day_2/test/f1.txt
Empty file.
Empty file added Challenges/Day_2/test/f2.txt
Empty file.
Empty file added Challenges/Day_2/test/f3.txt
Empty file.
Empty file added Challenges/Day_2/test1/f4.txt
Empty file.
Empty file added Challenges/Day_2/test1/f5.txt
Empty file.
Empty file added Challenges/Day_2/test1/f6.txt
Empty file.
Empty file added Challenges/Day_2/test3/f7.txt
Empty file.
Empty file added Challenges/Day_2/test3/f8.txt
Empty file.
Empty file added Challenges/Day_2/test3/f9.txt
Empty file.
125 changes: 125 additions & 0 deletions Challenges/Day_3/user_management.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/bin/bash
# check if a username exists
username_exists() {
if id "$1" &>/dev/null; then
return 0 # Username exists
else
return 1 # Username does not exist
fi
}

# create a new user account
create_user() {
read -p "Enter the new username: " new_username
if username_exists "$new_username"; then
echo "Error: Username already exists. Please choose a different username."
exit 1
fi

read -s -p "Enter the password for the new user: " new_password
echo
read -s -p "Confirm the password: " confirm_password
echo

if [ "$new_password" != "$confirm_password" ]; then
echo "Error: Passwords do not match. User account creation failed."
exit 1
fi

# Create the new user account
sudo useradd -m "$new_username" &>/dev/null
echo "$new_username:$new_password" | sudo chpasswd
echo "User account '$new_username' created successfully."
}

# delete an existing user account
delete_user() {
read -p "Enter the username to be deleted: " del_username
if ! username_exists "$del_username"; then
echo "Error: Username does not exist. Deletion failed."
exit 1
fi

# Delete the user account
sudo userdel -r "$del_username" &>/dev/null
echo "User account '$del_username' deleted successfully."
}

# reset the password of an existing user account
reset_password() {
read -p "Enter the username for password reset: " reset_username
if ! username_exists "$reset_username"; then
echo "Error: Username does not exist. Password reset failed."
exit 1
fi

read -s -p "Enter the new password for the user: " new_password
echo
read -s -p "Confirm the new password: " confirm_password
echo

if [ "$new_password" != "$confirm_password" ]; then
echo "Error: Passwords do not match. Password reset failed."
exit 1
fi

# Reset the user's password
echo "$reset_username:$new_password" | sudo chpasswd
echo "Password for user '$reset_username' reset successfully."
}

# list all user accounts on the system
list_users() {
echo "List of user accounts:"
echo "Username UID"
echo "-----------------"
# cut -d: -f1,3 /etc/passwd
while IFS=: read -r username _ uid _; do
echo "- $username (UID: $uid)"
done < /etc/passwd
}

# display usage information and available options
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 the password of an existing user account"
echo " -l, --list List all user accounts on the system"
echo " -h, --help Display this help message"
}

# Main script starts here
# Check if there are any command-line arguments
if [ $# -eq 0 ]; then
usage
exit 1
fi
while [[ $# -gt 0 ]]; do
key="$1"

case $key in
-c|--create)
create_user
;;
-d|--delete)
delete_user
;;
-r|--reset)
reset_password
;;
-l|--list)
list_users
;;
-h|--help)
usage
;;
*)
echo "Error: Invalid option. Use -h or --help to see available options."
exit 1
;;
esac
shift
done

62 changes: 62 additions & 0 deletions Challenges/day1_solution.script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#####################################################################################
# Author: Sasiram Beeke
# Date: 31/07/2023
# Description: Basics of shell scripting.
# Tip: to execute shell give permision +x scriptname
#####################################################################################

#!/bin/bash
echo '############### Task1 Comments start here #####################################'
echo '############### With single lineComment #######################################'
echo "welcome to my first example for single line comment"
#echo "this line us for comment purpose"
echo "single line comment example ends here" ## in this by usng # symbol we can give single line comment
echo '############### using << With multi line comment ##############################'
<<EOF
echo "this is example for multi line comment"
echo "multi line comment example"
EOF
echo 'after multiline comment code print here' # in this by usng << multiline comment in used.
echo '############ using : operator with multi line comment #########################'
: '
echo "this is first line"
echo "this is second line"
echo "this is third line"
'
echo "this is fourth line" # in this by using : operator multiline commnet is used.
echo '##################Task1 Comments ends here ####################################'

echo '################## Task2 echo starts here #####################################'
echo "echo is used to display text or message in terminal"
echo "welcome to the shell scripting" #in this both echo statement prints whatever message is gien to display in terminal
echo '################## Task2 echo Ends here #######################################'

echo '################# Task3 variables starts here #################################'
name="sasiram beeke"
echo "my name is $name"
address='pune'
echo "I am from $address"
echo "I am $name and i am from $address" #in this name and address are variables which are used to store values.
echo '################# Task3 variables ends here ###################################'

echo '################## Task4 using variables addition starts here #################'
echo 'Enter first number'
read a
echo 'Enter second number'
read b
echo "Addition for $a and $b is:- $((a+b))" #it will accept user input and print addition of it
echo '################## Task4 using variables addition ends here ###################'

echo '################## Task5 built-in variables starts here #######################'
echo "todays date is `date`" ##describe current date
echo "$0" ##describe name of your file or script
echo "$#" ##describe number of arguments passed to the script
echo "$USER" ##desribe current username of the user
echo "$SHELL" ##describe path to the shell program being used
echo '################## Task5 built-in variables ends here #########################'

echo '################### Task6 wildcards starts here ###############################'
echo "All text files which are available in current directory:"
ls *.txt #it will display all text files using * wildcard
echo '################### Task6 wildcards ends here #################################'