Skip to content
Open
66 changes: 66 additions & 0 deletions Challenges/Day_1/Day1_solution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash

## Task 1: Comments

# This is a bash script to automate the deployment process.

## Task 2: Echo

# Display a welcome message
echo "Welcome! This is Day 1 of the Bash Script Challenge initiated by Shubham Londhe Sir..."

## Task 3: Variables

# Declare and assign a variable
Var1="Gopal"

# Print the result
echo "Hi, My Name Is $Var1"

## Task 4: Using Variables

# Read input from the user
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2

# Calculate the sum of the two numbers
sum=$((num1 + num2))

# Print the result
echo "The sum of $num1 and $num2 is: $sum"

## Task 5: Using Built-in Variables

# Get the current date and time
current_date_time=$(date)

# Get the username of the current user
current_user=$(whoami)

# Get the hostname of the system
hostname=$(hostname)

# Display the information
echo "Current date and time: $current_date_time"
echo "Current user: $current_user"
echo "Hostname: $hostname"

## Task 6: Wildcards

# Specify the directory path
directory="/home/gopal/Pictures/BashBlaze-7-Days-of-Bash-Scripting-Challenge"

# Specify the file extension you want to search for
extension=".txt"

# Use the wildcard * to match files with the specified extension in the directory
files_with_extension=$(ls "$directory"/*"$extension")

# Check if any files with the specified extension were found
if [ -z "$files_with_extension" ]; then
echo "No files with the extension $extension found in the directory $directory."
else
echo "Files with the extension $extension in the directory $directory:"
echo "$files_with_extension"
fi

31 changes: 0 additions & 31 deletions Challenges/Day_1/solution_day1_script.sh

This file was deleted.

41 changes: 0 additions & 41 deletions Challenges/Day_2/backup_with_rotation.sh

This file was deleted.

21 changes: 21 additions & 0 deletions Challenges/Day_2/day_2.0_explorer_solution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# Display welcome message and list files and directories in the current path
echo "Welcome to the File and Directory Explorer!"
# List files and directories in the current path with their sizes in human-readable format
ls -lh | awk '{print $9 "\t" $5}'
while true; do

#Prompt the user to enter a line of text
read -p "Enter a line of text (Press Enter to exit): " input

#Read user's input until an empty string is entered
if [ -z "$input" ]; then
echo "Exiting the File and Directory Explorer. Goodbye!"
exit 0
fi

#Count the number of characters in the entered line of text
char_count=$(echo -n "$input" | wc -m)
echo "Character count: $char_count"
done
54 changes: 54 additions & 0 deletions Challenges/Day_2/day_2.1_Dir_Back_Rot_solution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash

# Function to create backup with timestamped folder
create_backup() {
local source_dir=$1
local source_dir="${source_dir}"
local backup_dir="backup_$(date +'%Y-%m-%d_%H-%M-%S')"
mkdir "$source_dir/$backup_dir"
echo "path: $source_dir/$backup_dir/$backup_dir"
cp -r "${source_dir}"/* "$backup_dir"
rm -r $source_dir/$backup_dir/$backup_dir
echo "Backup created: $backup_dir"
}

# Function to rotate backups and keep only the last 3 backups
rotate_backups() {
local source_dir=$1
local backup_dirs=($(ls -d "${source_dir}"/backup_* | sort))

local backup_count=${#backup_dirs[@]}
if [ "$backup_count" -gt 3 ]; then
local backups_to_remove=$((backup_count - 3))

for ((i=0; i<"$backups_to_remove"; i++)); do
rm -rf "${backup_dirs[$i]}"
echo "Backup removed: ${backup_dirs[$i]}"
done
fi
}

# Check if a directory path is provided as a command-line argument
if [ $# -eq 0 ]; then
echo "Error: Directory path not provided."
echo "Usage: $0 <directory_path>"
exit 1
fi

# Check if the provided directory exists
source_dir=$1
if [ ! -d "$source_dir" ]; then
echo "Error: Directory '$source_dir' does not exist."
exit 1
fi

# Create three backups with timestamped folders
for ((i=0; i<3; i++)); do
create_backup "$source_dir"
done

# Rotate backups and keep only the last 3 backups
rotate_backups "$source_dir"



23 changes: 0 additions & 23 deletions Challenges/Day_2/explorer.sh

This file was deleted.

Loading