diff --git a/Challenges/Day_2/backup_with_rotation.sh b/Challenges/Day_2/backup_with_rotation.sh index 2c5e56f..234f5f4 100644 --- a/Challenges/Day_2/backup_with_rotation.sh +++ b/Challenges/Day_2/backup_with_rotation.sh @@ -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" diff --git a/Challenges/Day_2/explorer.sh b/Challenges/Day_2/explorer.sh index be62ca1..42a99aa 100644 --- a/Challenges/Day_2/explorer.sh +++ b/Challenges/Day_2/explorer.sh @@ -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!" diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-13/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-13/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-13/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-13/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-13/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-13/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-21-16/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-21-16/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-19-01/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-19-01/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-19-01/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-19-01/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-19-01/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-19-01/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-13/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-19-01/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/backup_2023-08-01-16-19-01/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/backup_2023-08-01-16-21-13/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/backup_2023-08-01-16-21-16/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/f1.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/f2.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/backup_2023-08-01-16-22-21/f3.txt b/Challenges/Day_2/test/backup_2023-08-01-16-22-21/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/f1.txt b/Challenges/Day_2/test/f1.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/f2.txt b/Challenges/Day_2/test/f2.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test/f3.txt b/Challenges/Day_2/test/f3.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test1/f4.txt b/Challenges/Day_2/test1/f4.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test1/f5.txt b/Challenges/Day_2/test1/f5.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test1/f6.txt b/Challenges/Day_2/test1/f6.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test3/f7.txt b/Challenges/Day_2/test3/f7.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test3/f8.txt b/Challenges/Day_2/test3/f8.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_2/test3/f9.txt b/Challenges/Day_2/test3/f9.txt new file mode 100644 index 0000000..e69de29 diff --git a/Challenges/Day_3/user_management.sh b/Challenges/Day_3/user_management.sh new file mode 100755 index 0000000..17281d5 --- /dev/null +++ b/Challenges/Day_3/user_management.sh @@ -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 + diff --git a/Challenges/day1_solution.script.sh b/Challenges/day1_solution.script.sh new file mode 100755 index 0000000..e6ad78f --- /dev/null +++ b/Challenges/day1_solution.script.sh @@ -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 ##############################' +<