diff --git a/Day1_Solution/Day1_Completed_BashBlazeChallenge.png b/Day1_Solution/Day1_Completed_BashBlazeChallenge.png new file mode 100644 index 00000000..db6a14c4 Binary files /dev/null and b/Day1_Solution/Day1_Completed_BashBlazeChallenge.png differ diff --git a/Day1_Solution/Day1_My_Solution.sh b/Day1_Solution/Day1_My_Solution.sh new file mode 100755 index 00000000..0846269c --- /dev/null +++ b/Day1_Solution/Day1_My_Solution.sh @@ -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 diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_dir1/dummy_file4 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_dir1/dummy_file4 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_dir1/dummy_file5 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_dir1/dummy_file5 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_file1 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_file1 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_file2 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_file2 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_file3 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-36-36/dummy_file3 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_dir1/dummy_file4 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_dir1/dummy_file4 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_dir1/dummy_file5 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_dir1/dummy_file5 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_file1 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_file1 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_file2 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_file2 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_file3 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-37-55/dummy_file3 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_dir1/dummy_file4 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_dir1/dummy_file4 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_dir1/dummy_file5 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_dir1/dummy_file5 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_file1 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_file1 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_file2 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_file2 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_file3 b/Day2_Solution/Backup_Dir/backup_2023-08-02_05-38-02/dummy_file3 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/dummy_file1 b/Day2_Solution/Backup_Dir/dummy_file1 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/dummy_file2 b/Day2_Solution/Backup_Dir/dummy_file2 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Backup_Dir/dummy_file3 b/Day2_Solution/Backup_Dir/dummy_file3 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Day2.0_Output.png b/Day2_Solution/Day2.0_Output.png new file mode 100644 index 00000000..09f48511 Binary files /dev/null and b/Day2_Solution/Day2.0_Output.png differ diff --git a/Day2_Solution/Day2.1_Output.png b/Day2_Solution/Day2.1_Output.png new file mode 100644 index 00000000..9e441186 Binary files /dev/null and b/Day2_Solution/Day2.1_Output.png differ diff --git a/Day2_Solution/Project_Alpha/dummy_dir1/dummy_file4 b/Day2_Solution/Project_Alpha/dummy_dir1/dummy_file4 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Project_Alpha/dummy_dir1/dummy_file5 b/Day2_Solution/Project_Alpha/dummy_dir1/dummy_file5 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Project_Alpha/dummy_file1 b/Day2_Solution/Project_Alpha/dummy_file1 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Project_Alpha/dummy_file2 b/Day2_Solution/Project_Alpha/dummy_file2 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/Project_Alpha/dummy_file3 b/Day2_Solution/Project_Alpha/dummy_file3 new file mode 100644 index 00000000..e69de29b diff --git a/Day2_Solution/ReadMe.md b/Day2_Solution/ReadMe.md new file mode 100644 index 00000000..8d9ee334 --- /dev/null +++ b/Day2_Solution/ReadMe.md @@ -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. diff --git a/Day2_Solution/backup_with_rotation.sh b/Day2_Solution/backup_with_rotation.sh new file mode 100755 index 00000000..c6528dae --- /dev/null +++ b/Day2_Solution/backup_with_rotation.sh @@ -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 + + + diff --git a/Day2_Solution/explorer.sh b/Day2_Solution/explorer.sh new file mode 100755 index 00000000..a3ecc718 --- /dev/null +++ b/Day2_Solution/explorer.sh @@ -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 diff --git a/Day2_Solution/sample_directory/sample_file3 b/Day2_Solution/sample_directory/sample_file3 new file mode 100644 index 00000000..ac590c66 --- /dev/null +++ b/Day2_Solution/sample_directory/sample_file3 @@ -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! + diff --git a/Day2_Solution/sample_directory/sample_file4 b/Day2_Solution/sample_directory/sample_file4 new file mode 100644 index 00000000..7650a69e --- /dev/null +++ b/Day2_Solution/sample_directory/sample_file4 @@ -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! + diff --git a/Day2_Solution/sample_file1.txt b/Day2_Solution/sample_file1.txt new file mode 100644 index 00000000..26376fbf --- /dev/null +++ b/Day2_Solution/sample_file1.txt @@ -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! diff --git a/Day2_Solution/sample_file2.txt b/Day2_Solution/sample_file2.txt new file mode 100644 index 00000000..26376fbf --- /dev/null +++ b/Day2_Solution/sample_file2.txt @@ -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! diff --git a/Day3_Solution/Day3_Output.md b/Day3_Solution/Day3_Output.md new file mode 100644 index 00000000..0e27f020 --- /dev/null +++ b/Day3_Solution/Day3_Output.md @@ -0,0 +1,60 @@ +Here I am presenting the output of the script user_management.sh + +Scenerio 0 : When no argument/option is passed or selected. + +![No Argument](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/dd949e41-a2ce-473f-93e9-d990dffdaae9) + +------------------------------------------------------------Part 1-------------------------------------------------------------------------------- + +Scenerio 1a : Creating New User, when same username does not exist. + +![-c](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/a987feaa-dcdd-4f32-994b-5cd696bd0d06) + +Scenerio 1b : Creating New User, when same username exist. + +![-c duplicate](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/6bf137e3-ab78-4126-8fc6-6f7d1fb89e55) + +------------------------------------------------------------Part 2-------------------------------------------------------------------------------- +Scenerio 2a : Deleting Existing User. + +![-d](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/b6d1b256-894a-4609-acaa-4d2a3f84c91f) + +Scenerio 2b : Deleting non-Existing User + +![-d no user](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/a5cfc8c5-c466-4ff8-a058-ba81e99f85d5) + +------------------------------------------------------------Part 3-------------------------------------------------------------------------------- +Scenerio 3 : Reseting password for existing user. + +![-r](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/c62388da-40e9-475a-984f-4a2da01c5d0f) + +------------------------------------------------------------Part 4-------------------------------------------------------------------------------- + +Scenerio 4: List User Account Details such as User name and UID. + +![-l 1](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/c9f70e51-4c88-465e-b07a-787903a669cd) + +![-l 2](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/2bdb1925-15aa-4c96-ab24-cfa78cad95fd) + +------------------------------------------------------------Part 5-------------------------------------------------------------------------------- + +Scenerio 5: Display Help Usage Information. + +![-h](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/24f058d3-5b48-4c98-ad9b-df9747ba2978) + +------------------------------------------------------------Bonus 1-------------------------------------------------------------------------------- +Scenerio 6: Displaying more Details like Hpme Directory and Shell of the USER. + +![-m](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/a06dab6a-3ddd-4253-bc38-7d9ac32d1ab3) + + +------------------------------------------------------------Bonus 2-------------------------------------------------------------------------------- + +Scenerio 7: Manipulation of user account properties: Changing Username of existing user. + +![-n](https://github.com/Kiravadi/BashBlaze-7-Days-of-Bash-Scripting-Challenge/assets/55143259/b83c520c-02e3-4e00-a1d7-210fedb16e6d) + + + + + diff --git a/Day3_Solution/user_management.sh b/Day3_Solution/user_management.sh new file mode 100755 index 00000000..46370e63 --- /dev/null +++ b/Day3_Solution/user_management.sh @@ -0,0 +1,190 @@ +#!/bin/bash + +# Above is a shebang command. + +# Create user function if called will ask for new user name and create a new user name. If user already exist then error message will be displayed. +create_user() +{ + + echo "Enter the New Username: " + #read command will assign username entered by user in terminal to variable "user" + read -p " " user + # If statement will check for valid user. "getent" will check for username in /etc/passwd file. + if [ $(getent passwd $user) ] ; then + echo "Sorry, User "$user" already exist in the system. Please enter different User Name" + + else + # If the "IF" condition is false then user is added using sudo useradd command. + sudo useradd $user + echo "User account "$user" created successfully" + echo "Please Enter the Password for USER: $user " + #read command will assign password entered by user in terminal to variable "password" + read -p " " password + # This password variable is assigned as password to the user using sudo passwd command. "\n" is used to give to separate lines of same password. + echo -e "$password\n$password" | sudo passwd $user + echo "Password for USER $user has been ADDED Successfully" + # Ending If loop using "fi". + fi + +} + +delete_user() +{ + echo "Enter username that needs to be deleted : " + #read command will assign username entered by user in terminal to variable "user" + read -p " " user + # If statement will check for valid user. "getent" will check for username in /etc/passwd file. + if [ $(getent passwd $user) ] ; then + echo "User $user is present, Deleting User" + # cd /root is changing directory to root. + cd /root + # "userdel" command is used to delete user. + sudo userdel -rf $user + echo "USER: $user has been DELETED Successfully" + else + + # If "IF" statement becomes false then below messege will be displayed. + echo "Sorry, USER: "$user" does not exist in the system. Please enter valid User Name" + # Ending If loop using "fi". + fi + +} + +reset_password() +{ + echo "Enter the username whose password needs to be reset: " + #read command will assign username entered by user in terminal to variable "user" + read -p " " user + + # If statement will check for valid user. "getent" will check for username in /etc/passwd file. + if [ $(getent passwd $user) ] ; then + + echo "Please enter the New Password for user $user : " + #read command will assign new password entered by user in terminal to variable "password" + read -p " " password + # This new password will be assigned to username using passwd command. + echo -e "$password\n$password" | sudo passwd $user + echo "Password for user '$user' has been RESET Successfully" + else + # If "IF" statement becomes false then below messege will be displayed. + echo "Sorry, User "$user" does not exist in the system. Please enter valid Username" + # Ending If loop using "fi". + fi +} + +list_useraccounts() +{ + echo "User account on the system:" + # User account details such as user name and UID will be present in /etc/passwd file. + # Here cat command is used to print contents in passwd file and these lines will be passed as parameters to awk command. Using awk command we can display required column. First colunm is printed using $1, third column is printed using $3. ":" is the field separator. + sudo cat /etc/passwd | awk -F ":" '{print "-" $1,"(UID: "$3 ")"}' + +} + +help_option() +{ + echo " + Usage : ./user_management.sh [OPTIONS] + Options: + -c, --create Create a new user account. + -d, --delete Delete an existing user account. + -r, --reset Reset password for an existing user account. + -l, --list List all user accounts on the system. + -h, --help Display this help and exit. + -n, --changename Change the name of a user. + -m, --homedirectory Displey Home Directory and User Shell Details." +} + +homedirectory() +{ + echo "Home Dirctory and shell of USER" + echo "Enter the Username that needs to be changed" + read -p " " user + # If statement will check for valid user. "getent" will check for username in /etc/passwd file. + if [ $(getent passwd $user) ] ; then + echo "User $user is present." + #Here cat command is used to print contents in passwd file and these lines will be passed as parameters to awk command. Using awk comm and we can display required column. First colunm is printed using $1, 6th column is printed using $6 and 7th column using "$7". ":" is the field separator. + sudo cat /etc/passwd | awk -F ":" '{print "USERNAME--"$1,"-- HOME DIRECTORY:" $6,"( -- USER SHELL: "$7 ")"}' | grep -w $user + + else + # If "IF" statement becomes false then below messege will be displayed. + echo "Entered Username is invalid" + fi + +} + +changename() + +{ + echo "Enter the Username that needs to be changed" + read -p " " user + # If statement will check for valid user. "getent" will check for username in /etc/passwd file. + if [ $(getent passwd $user) ] ; then + echo "User $user is present." + echo "Enter new name for USER: $user" + # Read command will take new username. + read -p " " newname + # usermod command is used to make changes to the username. + sudo usermod -l $newname $user + else + # If "IF" statement becomes false then below messege will be displayed. + + echo "Entered Username is invalid" + fi + + + +} + + +# Main program starts from here +# Below if statement will check for the argument passed in command line. If argument is not passed then "$#" system variable will give "0" and the if statement hold true. + +if [[ $# -eq 0 ]]; then + # Below message will be displayed no argument is passed. + echo " Wrong Option arument or No argument is given " + +fi + +# If any value is entered then it will the first argument, whose value will be store in "$1" system variable. +# This value is assigned to user defined variable "option" using below command. + +option=$1 + +# Below is a CASE statement that checks the input passed as argument and matches variables and executes particular function thats been mentioned in the path. + +case $option in +-c | --create) + echo "Option -c or --create is pressed" + # if -c or --create is pressed then create_user() function is called. + create_user ;; +-d | --delete) + echo "Option -d or --delete is pressed" + # if Option -d or --delete is pressed then delete_user() function is called. + delete_user ;; +-r | --reset) + echo "Option -r or --reset is pressed" + # if Option -r or --reset is pressed then reset_password() function is called. + reset_password;; +-l | --list) + echo "Option -l or --list is pressed" + # if Option -l or --list is pressed then list_useraccounts() function is called. + list_useraccounts;; +-h | --help) + echo "Option -h or --help is pressed" + #if Option -h or --help is pressed then help_option() functon is called. + help_option;; +-n | --changename) + echo "Option -n or --changename is pressed" + # if Option -n or --changename is pressed then changename() function is called. + changename;; +-m | --homedirectory) + echo "Option -h or --homedirectory is pressed" + # if Option -h or --homedirectory is pressed then homedirectory() function is called. + homedirectory;; +*) + echo "Invalid Option Pressed" + # if Invalid Option is Pressed help_option() functoion is called. + help_option;; +esac +