Skip to content

My submission for 7 days of bash blaze challenge : Name - Yashraj Jaiswal #41

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
55 changes: 34 additions & 21 deletions Challenges/Day_1/solution_day1_script.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,44 @@
#!/bin/bash
# Author: Yashraj Jaiswal
# Date: 01-08-2023
# Description: #TWSBashBlazeChallenge Day - 1

# First line of the script is the shebang which tells the system how to execute
# Task 1: Comments
# - Comments are lines in a script that are ignored by the interpreter and help us describe the script.

# Task 2: Echo
echo "Scripting is fun with @TWS"
# Task 2 : echo
echo "Today is Day 1 of #TWSBashBlazeChallenge an amazing bash scripting challenge"

# Task 3: Variables
variable1="Hello"
variable2="Bash"
# Task 3: Variables

# Task 4: Using Variables
greeting="$variable1, $variable2!"
echo "$greeting Welcome to the world of Bash scripting!"
# declaring and initializing variables
name = "Yashraj Jaiswal"
interests = "web dev | devops"

# Task 5: Using Built-in Variables
echo "My current bash path - $BASH"
echo "Bash version I am using - $BASH_VERSION"
echo "PID of bash I am running - $$"
echo "My home directory - $HOME"
echo "Where am I currently? - $PWD"
echo "My hostname - $HOSTNAME"
# Task 4: Using Variables

# Task 6: Wildcards
echo "Files with .txt extension in the current directory:"
ls *.txt
echo "Hello, my name is $name and I am interesed in $interests"

# Task 5: Using Built-in Variables
#use built-in variables from bash to print useful information

# print the host name
echo "Hostname: $HOSTNAME"

# print the current user
echo "Current user: $USER"

# print the current working directory
echo "Current directory: $PWD"

# Task 6: Wildcards

# using * wild card to search for files ending in .txt
ls -al *.txt

# using ? wild card to search for files names ending with a single unknown character
ls -al file?.txt

# using [] wild card to search for files that start with 'file' and have numbers 0-5
ls -al 'file[0-5]'.txt

#Make sure to provide execution permission with the following command:
#chmod +x day1_script.sh
80 changes: 44 additions & 36 deletions Challenges/Day_2/backup_with_rotation.sh
Original file line number Diff line number Diff line change
@@ -1,41 +1,49 @@
#!/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
# Author: Yashraj Jaiswal
# Date: 01/08/2023
# Description: TWSBashBlazeChallenge Day-2
# Task 2: Directory Backup with Rotation

# Validate input parameter for the directory to backup
if [ "$#" -ne 1 ];
then
echo "Usage: $0 <path_to_directory>"
echo "Hint: enter the full path to the directory to backup"
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}"

# 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
}
path_to_dir="$1"
backup_dir="/var/backups"
max_backup_files=3

# Get the number of existing backup files in the backup directory
total_backup_files=$(find "$backup_dir" -type f -name "*.tgz" | wc -l)
echo
echo "Total backup files: $total_backup_files"

# Check if more than the allowed number of backup files exist
if (( total_backup_files >= max_backup_files ));
then
echo
echo "More than $max_backup_files backup files detected"
# Sort the backup files by modification time and delete the oldest one
oldest_file=$(find "$backup_dir" -type f -name "*.tgz" | sort -r | tail -n 1)
echo "Deleting the oldest backup file - $oldest_file"
sudo rm -f "$oldest_file"
fi

# Main script logic
create_backup
perform_rotation
# Create a new tar file with timestamp in the filename
backup_file="backup_$(date +%d_%m_%Y_%H_%M_%S).tgz"
sudo tar -cvzf "$backup_dir/$backup_file" "$path_to_dir" > /dev/null 2>&1

# Check if the tar command executed successfully
if [[ $? -eq 0 ]];
then
echo
echo "Backup created successfully"
echo "List of backups created in the past 3 days:"
sudo ls -lh "$backup_dir"/*.tgz | awk '{print "-",$NF, "\t(", $5, ")"}'
else
echo "Unable to create backup"
fi
echo
44 changes: 26 additions & 18 deletions Challenges/Day_2/explorer.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#!/bin/bash
#Author: Yashraj Jaiswal
# Date: 02/08/2023
# Description: #TWSBashBlazeChallenge Day-2
# Task 1: Interactive File and Directory Explorer

# Part 1: File and Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!"
# print a welcome message for the user
echo Welcome to the Interactive File and Directory Explorer!
# Format and display files and directories with their size in human readable format
echo "Files and Directories in current path - $PWD"
ls -lh | sed 1d | awk '{print "- ",$NF,"\t","(",$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

# Exit if the user enters an empty string
if [ -z "$input" ]; then
echo "Exiting the Interactive Explorer. Goodbye!"
break
# Infinite loop to continiously take user input
while true;
do
# prompt user to enter a text
echo -n "Enter a line of text (Press Enter without text to exit)"
read input
# use -z option to test for empty string
if [[ -z $input ]];
then
echo Exiting the interactive explorer. Goodbye!
exit
else
# bash built-in string length
input_length=${#input}
# print the character count on screen
echo "Character count $input_length"
fi

# Calculate and print the character count for the input line
char_count=$(echo -n "$input" | wc -m)
echo "Character Count: $char_count"
done

Loading