Skip to content

Day2 Challenge completed #12

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 15 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions Day1_Solution/Day1_My_Solution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash

# The first line above is called shebang "#!", this tells the kernal which interpreter should be used to run the commands present in the file. In this case the kernel will execute /bin/bash day1.sh


# This is a bash script and it contains tasks given in "#TWSBashBlazeChallenge" by DevOps Mentor Mr.Shubham Londhe
echo "Task 1 has been completed inside the shell script day1.sh"

#-----------------------------------------------------------------------------------------------------------------

# Task 1 of challenge for Day 1 is to create a bash script with comments explaining what the script does.


#-----------------------------------------------------------------------------------------------------------------

echo "Task 2 : "
# Task 2 : Printing a messege of my choice using echo command as shown below.

echo "Bhooooom! Lets Bash it Off!"

#-----------------------------------------------------------------------------------------------------------------

echo "Task 3 : "
# Task 3 : Variables in bash are used to store data and can be referenced by their name. Your task is to create a bash script that declares variables and assigns values to them.

# Lets begin by declaring two variables namely variable_1 and variable_2 and assigning them values 4 and 7 respectively.

variable_1=4

variable_2=3

echo "Value of variable_1 is assigned as $variable_1"
echo "Value of variable_2 is assigned as $variable_2"
#-----------------------------------------------------------------------------------------------------------------

echo "Task 4 :"
# Task 4 : Create a bash script that takes two variables (numbers) as input and prints their sum using those variables.

sum=$(( $variable_1 + $variable_2 ))

# In the above line I am adding two variables using "+" and assigning the sum to a variable called "sum".

# In the below line I am prining the value of sum using echo command.

echo " The sum of variale_1 and variable_2 is $sum"

#-----------------------------------------------------------------------------------------------------------------

echo "Task 5 : "
# Task 5 : Create a bash script that utilizes at least three different built-in variables to display relevant information.

# In this task I am giving few important Shell Built-in variables that can be very helpful in knowing current shell, current PID, current user and current file name.

# 1. Shell built in variable "$SHELL" stores the path to the shell program that is being used.
echo "The path of the shell program that is currently being used is $SHELL"

# 2. Shell built in variable "$$" gives the value of the PID of the current shell.

echo "PID of the current shell is $$ "

# 3. Shell built in variable "USER" gives the name of the current user.

echo "I am $USER, the current user of this session"

# 4. Shell built in variable "$0" gives the current file name or file name of this shell script.

echo "Name of the file that is currently being executed is $0 "


#-----------------------------------------------------------------------------------------------------------------

echo "Task 6 :"
# Task 6 : Create a bash script that utilizes wildcards to list all the files with a specific extension in a directory.

# There are three basic sets of wild cards in linux. They are "*","?" and "[]". In below example I am using "*" wildcard to list files with .md extention in the directory.

echo "Files with .md extention present in the current directory are :"
ls *.md

#-----------------------------------------------------------------------------------------------------------------

#Give read, write and execution permission to user for the file using below command.
# sudo chmod 700 day1.sh
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Binary file added Day2_Solution/Day2.0_Output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Day2_Solution/Day2.1_Output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions Day2_Solution/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Sample files and directories have been added just get good outpoot
#explorer.sh is the bash file for day2.0 challenge.
80 changes: 80 additions & 0 deletions Day2_Solution/backup_with_rotation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash

# Above is the shebang command, this will let the kernel know which interpreter to be used to run the below comands. In this case it is /bin/bash

#Challenge of Day_2.1 is to create backup of a directory (path of this directory is passed as argument), this backup needs to be saved in other particular directory and retain only the latest three backups along with any other files present in this directory.

# Path of directory that needs backup is passed as argument to $1 and assigned to variable "sourcedirectory" as shown below.

sourcedirectory=$1

echo "Source Directory path whose backup is required is : $sourcedirectory"
#---------------------------------------------------------------------------------------------------------------

# Creating a time stamp function. The output of this function is a timestamp. This will be used to name the Backup Directory.
# The format of the time stamp is YYYY-MM-DD_HH-MM-SS. Using the "date" bash command to get runtime date and time.



timestamp() {
date +%Y-%m-%d_%H-%M-%S
}



#----------------------------------------------------------------------------------------------------------------

# Providing a format of names given to newly created backup directories, with timestamp.
# Here function "timestamp" is called and appeded its value to "backup_" text. This overall text + timestamp format is assigned to a variable "filename".

filename=backup_$(timestamp)


#-----------------------------------------------------------------------------------------------------------------

# Here I am storing all the new backups in to "/home/ubuntu/challenge/BashBlaze-7-Days-of-Bash-Scripting-Challenge/Backup_Dir/"

# Using echo command to specifiy the path of the Backup Directory where all new and old backups are stored.

echo "Path of the backup directory is : /home/ubuntu/challenge/BashBlaze-7-Days-of-Bash-Scripting-Challenge/Day2_Solution/Backup_Dir/"

# Here I am creating a new variable "Backup_Dir", this will give the name of the new backup (using variable "filename") that will be created along with its path.


Backup_Dir="/home/ubuntu/challenge/BashBlaze-7-Days-of-Bash-Scripting-Challenge/Day2_Solution/Backup_Dir/$filename"

#----------------------------------------------------------------------------------------------------------------

# I am using a simple copy command "cp" to create backup.

# Below command will copy "sourcedirectory" and its contents including subdirectories to "Backup_Dir" directory.

# Using -R optoin to copy all subdirectories.

cp -R $sourcedirectory $Backup_Dir

echo "---------------------------------New Backup file Name is $filename ------------------------------------------"

echo "Backup created: $Backup_Dir"
#-----------------------------------------------------------------------------------------------------------------


# In this part of the code we will implement a rotation mechanism to keep only three latest backups.


# First we need to change the current workning directory to the directory that contains all the backups. This is done using "cd" command.

cd /home/ubuntu/challenge/BashBlaze-7-Days-of-Bash-Scripting-Challenge/Day2_Solution/Backup_Dir/

# After getting into Backup_Dir directory, below command is executed to remove all directories excpt for last three backups.

# Here "ls -1tp" comand is used to list contents in the present directory and sort them by modification time.

# Standard output of "ls -1tp" is given to grep command using pipe [|]. Here "grep" will filter all files and directories except for files and directories starting with "backup_*". Next this list is given to tail command using [|] pipe. Tail command skips the first three entries in the listing.

# "rm" is used to remove all the entries. Since "rm command accepts arguments and not parameters like grep, "xarg" command is used. "xarg" command will invoke "rm" separately for each line withthe help of " -d '\n' ", this makes "xarg" consider each input line a separate argument. "-rf" option is used to remove all directories and files present in the list"

ls -1tp |grep 'backup_*' | tail -n +4 | xargs -d '\n' -r rm -rf



59 changes: 59 additions & 0 deletions Day2_Solution/explorer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

# This is solution bash script of Day_2.0 Challenge.

# First line is called shebang it tells kernel which interpreter to be used to run the below commands.

# Day_2.0 challenge has two parts.

# Part 1 : File and Directory Exploration.

# Below echo command will give a welcome message.
echo "------------------------File and Directory Exploration---------------------"

echo "Welcome to Day2 of BashBlazeChallenge! "

echo "Thses are the list of directories and files along with their sizes, in the current path"

# Below command has two parts. First is "du -sh *", here du is a bash command that is used to get information about disk usage statistics. Option -s gives display for each specified entry. -h option gives output in human readable format. "*" option gives sizes of all the files present in the current path.

# The output of "du -sh *" is piped using [|] to be fed in to awk command. awk command first prints 2nd line which has files and directory names and the 1st column that has files and directory size. Two paranthesis are "()" added to separate file name and size info.

du -sh * | awk -F " " '{print $2,"("$1")"}'

echo "--------------------------Character Counting-------------------------------"
# Part 2 : Character Counting

# Here I am using while loop to make the program run continuously, and continuously ask the user to enter a line of text or press Enter to stop and come out of the program.

while :
do
echo -n "Enter a line of text or just press enter without text to exit"

# Bash read is a built-in utility command used to record the line entered by user.

# The text line entered by used is stored in variable "line"
read -r line


# if loop is used to check if the user has pressed enter.If Enter is pressed the zero will be passed to read command.
# Expresion ${#line} is used to count characters. line is the parameter passed to the expression. The expression gives character count.

# Expression inside if [ ${#line} -eq 0 ] will check if value entered by user is equal to 0. If true then break command is given to come out of the program after "Enter was Hit" message was displayed.
if [ ${#line} -eq 0 ];
then
echo "Enter was hit by User"
# break command is given here to exit the loop.
break
else
# if user enters a text line then its calculated by ${#parameter} expression. And echo is used to display the value.
echo "Character count: ${#line}"
fi
# if loop is closed using fi
# while loop is closed using done
done

echo "-----------------------Exiting the Interactive Explorer. Thank You!--------------"

# Give execution permission to user using command
# chmod 700 filename
57 changes: 57 additions & 0 deletions Day2_Solution/sample_directory/sample_file3
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Hi this is another sample file. It has random content just to give some size to the file.



Day_2.0

# Day 2 Bash Scripting Challenge - Interactive File and Directory Explorer

Welcome to Day 2 of the Bash Scripting Challenge! In this challenge, you will create a bash script that serves as an interactive file and directory explorer. The script will allow you to explore the files and directories in the current path and provide a character counting feature for the user's input.

## Challenge Description

The script will have two main parts:

### Part 1: File and Directory Exploration

1. Upon execution without any command-line arguments, the script will display a welcome message and list all the files and directories in the current path.
2. For each file and directory, the script will print its name and size in human-readable format (e.g., KB, MB, GB). This information will be obtained using the `ls` command with appropriate options.
3. The list of files and directories will be displayed in a loop until the user decides to exit the explorer.

### Part 2: Character Counting

1. After displaying the file and directory list, the script will prompt the user to enter a line of text.
2. The script will read the user's input until an empty string is entered (i.e., the user presses Enter without any text).
3. For each line of text entered by the user, the script will count the number of characters in that line.
4. The character count for each line entered by the user will be displayed.

## Example Interaction

```
$ ./explorer.sh
Welcome to the Interactive File and Directory Explorer!

Files and Directories in the Current Path:
- file1.txt (100 KB)
- dir1 (2 MB)
- script.sh (3 KB)
...

Enter a line of text (Press Enter without text to exit): Hello, this is a sample line.
Character Count: 27

Enter a line of text (Press Enter without text to exit): Another line to count.
Character Count: 25

Enter a line of text (Press Enter without text to exit):
Exiting the Interactive Explorer. Goodbye!
```

## Submission Instructions

1. Create a bash script named `explorer.sh` that implements the Interactive File and Directory Explorer as described in the challenge.
2. Add comments in the script to explain the purpose and logic of each part.
3. Submit your entry by pushing the script to your GitHub repository.

Congratulations! You've completed Day 2 of the Bash Scripting Challenge. The challenge focuses on Command-Line Argument Parsing and Loops to build an interactive script that explores files, directories, and performs character counting. Happy scripting and exploring!

55 changes: 55 additions & 0 deletions Day2_Solution/sample_directory/sample_file4
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Hi this is another sample file. It has random content just to give some size to the file.

Day_2.0

# Day 2 Bash Scripting Challenge - Interactive File and Directory Explorer

Welcome to Day 2 of the Bash Scripting Challenge! In this challenge, you will create a bash script that serves as an interactive file and directory explorer. The script will allow you to explore the files and directories in the current path and provide a character counting feature for the user's input.

## Challenge Description

The script will have two main parts:

### Part 1: File and Directory Exploration

1. Upon execution without any command-line arguments, the script will display a welcome message and list all the files and directories in the current path.
2. For each file and directory, the script will print its name and size in human-readable format (e.g., KB, MB, GB). This information will be obtained using the `ls` command with appropriate options.
3. The list of files and directories will be displayed in a loop until the user decides to exit the explorer.

### Part 2: Character Counting

1. After displaying the file and directory list, the script will prompt the user to enter a line of text.
2. The script will read the user's input until an empty string is entered (i.e., the user presses Enter without any text).
3. For each line of text entered by the user, the script will count the number of characters in that line.
4. The character count for each line entered by the user will be displayed.

## Example Interaction

```
$ ./explorer.sh
Welcome to the Interactive File and Directory Explorer!

Files and Directories in the Current Path:
- file1.txt (100 KB)
- dir1 (2 MB)
- script.sh (3 KB)
...

Enter a line of text (Press Enter without text to exit): Hello, this is a sample line.
Character Count: 27

Enter a line of text (Press Enter without text to exit): Another line to count.
Character Count: 25

Enter a line of text (Press Enter without text to exit):
Exiting the Interactive Explorer. Goodbye!
```

## Submission Instructions

1. Create a bash script named `explorer.sh` that implements the Interactive File and Directory Explorer as described in the challenge.
2. Add comments in the script to explain the purpose and logic of each part.
3. Submit your entry by pushing the script to your GitHub repository.

Congratulations! You've completed Day 2 of the Bash Scripting Challenge. The challenge focuses on Command-Line Argument Parsing and Loops to build an interactive script that explores files, directories, and performs character counting. Happy scripting and exploring!

52 changes: 52 additions & 0 deletions Day2_Solution/sample_file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Hi this is another sample file. It has random content just to give some size to the file.

# Day 2 Bash Scripting Challenge - Interactive File and Directory Explorer

Welcome to Day 2 of the Bash Scripting Challenge! In this challenge, you will create a bash script that serves as an interactive file and directory explorer. The script will allow you to explore the files and directories in the current path and provide a character counting feature for the user's input.

## Challenge Description

The script will have two main parts:

### Part 1: File and Directory Exploration

1. Upon execution without any command-line arguments, the script will display a welcome message and list all the files and directories in the current path.
2. For each file and directory, the script will print its name and size in human-readable format (e.g., KB, MB, GB). This information will be obtained using the `ls` command with appropriate options.
3. The list of files and directories will be displayed in a loop until the user decides to exit the explorer.

### Part 2: Character Counting

1. After displaying the file and directory list, the script will prompt the user to enter a line of text.
2. The script will read the user's input until an empty string is entered (i.e., the user presses Enter without any text).
3. For each line of text entered by the user, the script will count the number of characters in that line.
4. The character count for each line entered by the user will be displayed.

## Example Interaction

```
$ ./explorer.sh
Welcome to the Interactive File and Directory Explorer!

Files and Directories in the Current Path:
- file1.txt (100 KB)
- dir1 (2 MB)
- script.sh (3 KB)
...

Enter a line of text (Press Enter without text to exit): Hello, this is a sample line.
Character Count: 27

Enter a line of text (Press Enter without text to exit): Another line to count.
Character Count: 25

Enter a line of text (Press Enter without text to exit):
Exiting the Interactive Explorer. Goodbye!
```

## Submission Instructions

1. Create a bash script named `explorer.sh` that implements the Interactive File and Directory Explorer as described in the challenge.
2. Add comments in the script to explain the purpose and logic of each part.
3. Submit your entry by pushing the script to your GitHub repository.

Congratulations! You've completed Day 2 of the Bash Scripting Challenge. The challenge focuses on Command-Line Argument Parsing and Loops to build an interactive script that explores files, directories, and performs character counting. Happy scripting and exploring!
Loading