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
129 changes: 129 additions & 0 deletions Challenges/Day_1/my_bash_solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
## **Introduction:**

Bash scripting is a powerful and versatile skill that allows you to automate tasks and perform complex operations using the command-line interface of a Unix-based operating system, such as Linux or macOS. Bash which stands for "Bourne Again Shell" is the default shell for many Unix-like systems and serves as a command interpreter for executing commands and scripts.

Today we are going to cover the basics of Bash scripting. By the end of this challenge, we will be able to improve our understanding of bash scripting and will be able to create a bash script for the respective requirements. So, let's begin!

## **Task 1: Comments**

In bash scripts, comments are the non-executed part of the script. They provide useful information or explanations about the code. They play an important role in making the script more understandable not only for the author but also for the developers who might need to work with the script.

Single-line comments: Single-line comments begin with (#) and extend till the end of the line. A line beginning with the # symbol will be completely ignored by the compiler. For example:

```plaintext
#!/bin/bash

# Task 1: Comments
# This is a single-line comment
echo "Hello World!"

```

Multi-line comments: There are multiple ways through which you can mention multiple comments in a bash script. One such method is to use (:'). For example:

```plaintext
#!/bin/bash
: '
This is a multi-line comment block.
You can add as many lines as you want.
The code below will not be executed:

echo "This line will not be executed."
'
echo "Hello World!"
```

## **Task 2: Echo**

In bash, an `echo` command is a command used to output or display messages on the terminal. It is one of the most commonly used commands in bash script. Now let's create a bash script to print a message using `echo` command.

```plaintext
#!/bin/bash

# Task 2: Echo
# In this task, we will use the 'echo' to display a message.
echo "Let's explore the world of bash scripting!"
```

## **Task 3: Variables**

In bash, variables are used to store data and values that can be referenced and manipulated throughout the script. They are a fundamental part of shell scripting and allow you to store temporary or permanent data. In this task, we will create a bash script that declares variables and assigns value to them. Here's how you declare and use variables in bash:

```plaintext
#!/bin/bash

# Task 3: Variables
# Remember to avaoid space between the '=' and variable
name="Tony Stark"
book="Think and Grow Rich"

echo "My name is $name"
echo "And my favourite book is $book"
```

## **Task 4: Using Variables**

In the previous task, we declared the variables. In this task, we will now make use of them to perform the given task. So we'll simply create two variables of type number and then print their sum.

```plaintext
#!/bin/bash

# Task 4: Using Variables
# In this task we will perform the addition of two numbers

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

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

# Display the result
echo "The sum of two given numbers is: $result"
```

## **Task 5: Using Built-in Variables**

Built-in variables are the special variables whose values are defined and maintained by the bash or the operating system. These variables are predefined and have specific names that are recognized by the shell. In this task, we will create a bash script that utilizes at least three different built-in variables to display relevant information.

```plaintext
#!/bin/bash

# Task 5: Built-in Variables
# In this task we will be utilizing built-in variables

# $HOME represents the home directory of the current user
echo "Your home directory is: $HOME"

# $USER holds the username of the current user
echo "Your username is: $USER"

# $PWD represents the present working directory
echo "You are currently in: $PWD"

# $HOSTNAME represents the current name of the host/machine
echo "Host name: $HOSTNAME"

# $OSTYPE defines the operating system type
echo "Operating system type: $OSTYPE"
```

## **Task 6: Wildcards**

Wildcards are special characters used to represent and match one or more filenames or paths. They are powerful tools that allow you to perform operations on multiple files or directories at once. In this task, we will create a bash script that utilizes wildcards to list all the files with a specific extension in a directory.

```plaintext
#!/bin/bash

# Task 6: Wildcards
# In this task, we will use wildcards to list files

# Define the target directory
target_directory="/home/devops/"

# List all files with a specific extension in the target directory
echo "Files with the '.txt' extension:"
ls "$target_directory"/*.txt
```


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

This file was deleted.

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

This file was deleted.

71 changes: 71 additions & 0 deletions Challenges/Day_2/explorer_solution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# **🌟 Introduction**

Welcome to Day 2 of the #TWSBashBlaze Challenge. In this blog, we will create a bash script that serves as an interactive file and directory explorer. We'll create a script that will allow you to explore the files and directories in the current path interactively and also provide a character-counting feature for the user's input. So without wasting any time let's dive in!

### **📂 Part 1: File and Directory Exploration 🔍**

In this task, we are going to create a bash script explorer.sh which will display a welcome message and list all the files and directories in the current path upon execution. Our script will not only allow us to navigate through files and directories in the current path but also display their names and sizes in a human-readable format (e.g., KB, MB, GB). We'll achieve this functionality using the powerful ls command with appropriate options. The list of files and directories will be displayed in a loop until the user decides to exit the explorer.

### **🔤Part 2: Character Counting 📏**

🔍On displaying the file and directory list, the script will prompt the user to enter a text-based input. The script will keep running in a loop until the user enters an empty string (👉the user presses Enter without any text). If the script is non-empty then it will count the number of characters present in the input. The character count will be displayed immediately after the input is passed. 🔤

Now let's dive in and write a script that fulfills all the above requirements!💻🚀


```plaintext
#!/bin/bash

# Part 1: File and Directory Exploration
echo "Welcome to the Interactive File and Directory Explorer!👋"

# Run the infinite loop of while until user wants to exit
while true; do

# Display the list of files and directories with their names and sizes
echo "Files and Directories in the Current Path:"
for item in *; do
if [ -e "$item" ]; then
size=$(ls -lh "$item" | awk '{print $5}')
echo "Name: $item Size: $size"
fi
done

# Take the input from user to decide whether to continue looping
read -p "Enter a text input and press enter if you want to exit" input

# Exit if the input is empty
if [ -z "$input" ]; then
echo "Exit the Interactive Explorer! Bbyee...See you!👋"
break
fi

# Count the number of characters in an input text and then display the count
char_count=${#input}
echo "Character count: $char_count"

done

```


### **🔍 Usage:**

Save the script in a file named explorer.sh.

Make the script executable using the command: chmod +x explorer.sh.

Execute the script using: ./explorer.sh.


### **🔍💻Output Screen:**

![image](https://github.com/gauri17-pro/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/60473255/2b31ca89-5109-4323-93e9-b546b3f29964)

# **📍 Conclusion:**

Congratulations! You have successfully completed Day 2 of the #TWSBashBlazeChallenge. You now have a versatile script that lets you explore files and directories in your current path, and it also includes a useful character-counting feature. 🚀 I hope you enjoyed building this script and learning more about Bash scripting. Stay tuned for more exciting challenges in the upcoming days!

Till then happy learning and keep exploring!🎈🌌


Loading