Skip to content
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

Pull request for Day4 solution #23

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6802d88
Added 1st challange solution
salwad-basha-shaik Jul 31, 2023
bba8b54
Merge branch 'prajwalpd7:main' into main
salwad-basha-shaik Aug 1, 2023
818767e
added Day-2.0 solution
salwad-basha-shaik Aug 1, 2023
62e92df
Merge remote-tracking branch 'origin/main'
salwad-basha-shaik Aug 1, 2023
83efbcf
Added Day2.1 solution
salwad-basha-shaik Aug 1, 2023
4cd2317
Merge branch 'prajwalpd7:main' into main
salwad-basha-shaik Aug 2, 2023
5fe74e4
Added day3 solution
salwad-basha-shaik Aug 2, 2023
74e90c5
Improvised and added more bonus features for day-3
salwad-basha-shaik Aug 2, 2023
6b480e6
Merge branch 'prajwalpd7:main' into main
salwad-basha-shaik Aug 3, 2023
f4ea8f9
Added Day4 part solution with bonus feature
salwad-basha-shaik Aug 3, 2023
27255fc
Added Day4 part solution with bonus feature
salwad-basha-shaik Aug 3, 2023
737c4ea
Added Day4 part solution with bonus feature
salwad-basha-shaik Aug 3, 2023
bd60830
Update and rename readme.txt to README.md
salwad-basha-shaik Aug 3, 2023
7822e07
Update README.md with slack notification sc
salwad-basha-shaik Aug 3, 2023
64c05a1
Update README.md
salwad-basha-shaik Aug 3, 2023
6311495
Added day-4 part2 solution
salwad-basha-shaik Aug 3, 2023
0921bbf
Update and rename PART2README.md to Part2README.md
salwad-basha-shaik Aug 3, 2023
027cfbe
Update and rename README.md to Part1README.md
salwad-basha-shaik Aug 3, 2023
e0e837b
Create readme.txt
salwad-basha-shaik Aug 3, 2023
be1316c
Added day-4 parts(1&2) practiced scripts
salwad-basha-shaik Aug 3, 2023
623b0ce
Merge remote-tracking branch 'origin/main'
salwad-basha-shaik Aug 3, 2023
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
95 changes: 95 additions & 0 deletions Self-solutions/Day_1/day1_challange_solution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash

#####################################################################
# Script Name: day1_challange_solution.sh
# Author: Salwad Basha Shaik
# Date: July 31, 2023
# Description: This script contains solution for all the 6 tasks for Day-1 part of BashBlaze: 7 Days of Bash Scripting Challenge.
# Usage: ./day1_challange_solution.sh
# Email: salvathshaik786143@gmail.com
# LinkedIn: https://www.linkedin.com/in/salwad-basha-shaik/
# Github: https://github.com/salvathshaik/
#####################################################################

# set -x Used it while debugging the script.

#Task 1: Comments
#In bash scripts, comments are used to add explanatory notes or disable certain lines of code. Your task is to create a bash script with comments explaining what the script does.

#Task_1 Solution:
## This is a comment in shell script


#Task 2: Echo
#The echo command is used to display messages on the terminal. Your task is to create a bash script that uses echo to print a message of your choice.

#Task-2 solution:
echo "This is Day-1 task-2 echo example"


#Task 3: Variables
#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.

#Task 4: Using Variables
#Now that you have declared variables, let's use them to perform a simple task. Create a bash script that takes two variables (numbers) as input and prints their sum using those variables.


#Task-3 and Task-4 solution:
#note: Don't give space after '=' operand below , otherwise you will get errors
a=3
b=7
c=$(( a + b )) #The double parentheses are used for arithmetic evaluation in Bash. It allows you to perform arithmetic operations on numerical values within the shell script.
echo "sum of two elements $a + $b = $c"

#Improvising the task3&4
echo "enter the first number: "
read a

echo "enter the second number: "
read b

c=$(( a + b ))
echo "sum of $a and $b is $c"



#Task 5: Using Built-in Variables
#Bash provides several built-in variables that hold useful information. Your task is to create a bash script that utilizes at least three different built-in variables to display relevant information.

#task-5 solution:
echo "Disk usage: "
df -h

echo "Checking exit status of the last commmand $?" #If we get 0 means successful

echo "To install any module, let's say net-tools for checking network connections"
sudo apt-get update
sudo apt-get install net-tools


echo "Checking network connections: "
echo "Display listening TCP and UDP ports"
netstat -tuln

#netstat -rn # Display routing table


echo "Checking DNS resolution of google: "
nslookup www.google.com

echo "Checking if service is running: "
systemctl status ssh


#Task-6 Wildcards
#Wildcards are special characters used to perform pattern matching when working with files. Your task is to create a bash script that utilizes wildcards to list all the files with a specific extension in a directory.

#Task-6 solution:
#Wildcards in shell scripting are special characters used to represent one or more characters in a file or directory name. example ls, mv, cp rm etc. commonly used wildcards in shell scripting:

#* (Asterisk): Represents zero or more characters.
ls *.txt # *.txt matches all files with the .txt extension within the directory.

#Wildcards are powerful tools for batch processing and manipulating files in shell scripts. However, it's essential to be cautious when using wildcards, especially with commands like rm.


140 changes: 140 additions & 0 deletions Self-solutions/Day_1/day_1_solution_practice.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash

set -x

#Task 1: Comments
#In bash scripts, comments are used to add explanatory notes or disable certain lines of code. Your task is to create a bash script with comments explaining what the script does.

#Task_1 Solution:
## This is a comment in shell script


#Task 2: Echo
#The echo command is used to display messages on the terminal. Your task is to create a bash script that uses echo to print a message of your choice.

#Task-2 solution:
echo "This is Day-1 task-2 echo example"


#Task 3: Variables
#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.

#Task 4: Using Variables
#Now that you have declared variables, let's use them to perform a simple task. Create a bash script that takes two variables (numbers) as input and prints their sum using those variables.


#Task-3 and Task-4 solution:
#note: Don't give space after '=' operand below , otherwise you will get errors
a=3
b=7
c=$(( a + b )) #The double parentheses are used for arithmetic evaluation in Bash. It allows you to perform arithmetic operations on numerical values within the shell script.
echo "sum of two elements $a + $b = $c"

#Improvising the task3&4
echo "enter the first number: "
read a

echo "enter the second number: "
read b

c=$(( a + b ))
echo "sum of $a and $b is $c"



#Task 5: Using Built-in Variables
#Bash provides several built-in variables that hold useful information. Your task is to create a bash script that utilizes at least three different built-in variables to display relevant information.

#task-5 solution:
echo "Disk usage: "
df -h

echo "Checking exit status of the last commmand $?" #If we get 0 means successful

echo "To install any module, let's say net-tools for checking network connections"
sudo apt-get update
sudo apt-get install net-tools


echo "Checking network connections: "
echo "Display listening TCP and UDP ports"
netstat -tuln

#netstat -rn # Display routing table


echo "Checking DNS resolution of google: "
nslookup www.google.com

echo "Checking if service is running: "
systemctl status ssh

## There are more as follows

#Download a file using wget: wget http://example.com/file.zip
#Generate an SSH key pair: ssh-keygen -t rsa -b 4096
#Install Python dependencies from requirements.txt: pip install -r requirements.txt
#Monitor log files in real-time: tail -f /var/log/<log_file>
#Search for a specific pattern in files: grep "pattern" /path/to/files/*
#Check the content of a log file: cat /var/log/<log_file>
#Use curl to test APIs or URLs: curl http://example.com/api
#Restart a service: sudo systemctl restart <service_name>
#Create a backup of a directory using tar: tar -czvf backup.tar.gz /path/to/directory
#View running processes: ps aux
#Monitor system resources with top: top
#to check and list all runnig services: systemctl list-units --type=service --state=running
#service --status all
## Display the name of the script file: echo "Script name: $0"
# Display the number of arguments passed to the script: echo "Number of arguments: $#"
## Display the process ID of the current script: echo "Process ID of the current script: $$"
## Display the parent process ID: echo "Parent process ID: $PPID"
## Display the username of the current user: echo "Current username: $USER"
# Display the home directory of the current user: echo "Home directory: $HOME"
# Display the exit status of the last executed command: echo "Exit status of the last command: $?"
## Display the current working directory: echo "Current working directory: $PWD"


##


#Task-6 Wildcards
#Wildcards are special characters used to perform pattern matching when working with files. Your task is to create a bash script that utilizes wildcards to list all the files with a specific extension in a directory.

#Task-6 solution:
#Wildcards in shell scripting are special characters used to represent one or more characters in a file or directory name. example ls, mv, cp rm etc. commonly used wildcards in shell scripting:

#* (Asterisk): Represents zero or more characters.
#Example: *.txt matches all files with the .txt extension.
ls *.txt

#? (Question Mark): Represents a single character.
#Example: file?.txt matches files like file1.txt, file2.txt, etc.

#[] (Square Brackets): Represents a range of characters or a character set.
#Example: [aeiou] matches any vowel character.
#Example: [0-9] matches any digit.

#! (Exclamation Mark): Negates the pattern.
#Example: !*.txt matches all files except those with the .txt extension.

#{} (Curly Braces): Used for brace expansion, which generates multiple strings based on a pattern.
#Example: file{1,2,3}.txt expands to file1.txt, file2.txt, file3.txt.

#Wildcards are powerful tools for batch processing and manipulating files in shell scripts. However, it's essential to be cautious when using wildcards, especially with commands like rm, to avoid accidentally deleting important files. Always double-check your patterns before executing commands with wildcards.

















1 change: 1 addition & 0 deletions Self-solutions/Day_1/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
please run 'day1_challange_solution.sh'
81 changes: 81 additions & 0 deletions Self-solutions/Day_2/day2.0_challange_solution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

#####################################################################
# Script Name: day_2.0_challange_solution.sh
# Author: Salwad Basha Shaik
# Date: August 1, 2023
# Description: This script contains solution for Day 2 Bash Scripting Challenge - Interactive File and Directory Explorer part of BashBlaze: 7 Days of Bash Scripting Challenge.
# Usage: ./day_2.0__challange_solution.sh
# Email: salvathshaik786143@gmail.com
# LinkedIn: https://www.linkedin.com/in/salwad-basha-shaik/
# Github: https://github.com/salvathshaik/
#####################################################################

####################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
#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.
#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.
#The list of files and directories will be displayed in a loop until the user decides to exit the explorer.
##################Part 2: Character Counting
#After displaying the file and directory list, the script will prompt the user to enter a line of text.
#The script will read the user's input until an empty string is entered (i.e., the user presses Enter without any text).
#For each line of text entered by the user, the script will count the number of characters in that line.
#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!

echo
echo "Welcome to the Interactive File and Directory Explorer!"
while true; do #this will be worked as infinite loop in shell script

echo
echo "Files and Directories in the Current Path:"

#This extract each and every directory and file in current path using $PWD and for loop to iterate over every item
for directoryItemName in $PWD/*; do

if [ -f "$directoryItemName" ]; then #checking if the item is file then we use ls -lh to get the file size
directoryItemSize=$(ls -lh "$directoryItemName" | awk '{print $5}') #we are using awk command to extract the 5th item from ls output

elif [ -d "$directoryItemName" ]; then #here we are checking if item is directory or not using -d
directoryItemSize=$(du -sh "$directoryItemName" | awk '{print $1}') #using du -sh for directory size as ls will not gonna give and extractin g the first argument for size.
fi

directoryItemBaseName=$(basename "$directoryItemName") #using basename keyword to extract only name of the file otherwise we wil get whole path
echo "- $directoryItemBaseName ($directoryItemSize)"

done
echo
read -p "Enter a line of text (Press Enter without text to exit): " userInputString #taking input from the user using read with -p argument , if we mention -p then it will ask the prompt and store the input to variable.
if [ -z "$userInputString" ]; then #checking if the statement length is 0 using -z means it is empty then we will exit the infinite loop.
echo "Exiting the Interactive Explorer. Goodbye!"
echo
break #using break to exit from the infinite loop
else
characterCountOfStatement=$( echo -n "$userInputString" | wc -c ) #counting the characters of a statement using wc -c means word count with -c argument for character counting. and we are eliminating newline with the help of echo -n here.
echo "Character Count: $characterCountOfStatement"
fi
done
Loading