JTCyberTech / Cybersecurity-Home-Labs

5 stars 1 forks source link

3. 25 Basic Bash Scripting Hands-On Tasks #79

Open JTCyberTech opened 1 year ago

JTCyberTech commented 1 year ago

25 Basic Bash Scripting Hands-On Tasks

Please use Task 1 as a refers for all description. Since there are a lot of overlapping scripts, I will only write description that are new.

Task 1:

Create a script that will print "Hello World" on Screen.

Inside task_1.sh Vim Editor:

Outside task_1.sh Vim Editor:



Task 2:

Create a script that will print system date and time on Screen. - Command used: - ```vi task_2.sh``` Inside task_2.sh Vim Editor: - ```#!/bin/bash``` - ```date``` Outside task_2.sh Vim Editor: - ```chmod 755 task_2.sh``` - ```./task_2.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```date```: command that displays date and time.



Task 3:

Create a script that will take user name as input and then print the same on Screen. - Command used: - ```vi task_3.sh``` Inside task_3.sh Vim Editor: - ```#!/bin/bash``` - ```echo "Type Your Name"``` - ```read username``` - ```echo "$username"``` Outside task_3.sh Vim Editor: - ```chmod 755 task_3.sh``` - ```./task_3.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```echo "Type Your Name"```: Print out "Type Your Name on the first line of output. - ```read username```: capture user input and store it in a variable called "username". - ```echo "$username"```: print the value that the user entered. "$" is used to access the variable's value.



Task 4:

Create a script that will go to sleep for 60 seconds and then print "Good Bye" on screen. - Command used: - ```vi task_4.sh``` Inside task_4.sh Vim Editor: - ```#!/bin/bash``` - ```sleep 60``` - ```echo "Good Bye"``` Outside task_4.sh Vim Editor: - ```chmod 755 task_4.sh``` - ```./task_4.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```sleep 60```: sleep command will introduce a delay in the script for 60 seconds. - ```echo "Good Bye"```: After 60 seconds, script will print out Good Bye.



Task 5:

Create a script that will create a file with name "devops.txt" and insert the free memory details of your Linux machine in it. - Command used: - ```vi task_5.sh``` Inside task_5.sh Vim Editor: - ```#!/bin/bash``` - ```free -m > devops.txt``` - ```cat devops.txt``` Outside task_5.sh Vim Editor: - ```chmod 755 task_5.sh``` - ```./task_5.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```free -m```: used to display memory usage information in Linux or Unix Script. - ```free```: command used to display memory-related information. - ```-m```: option that specifies the unit of measurement for memory in MB. - ```>```: output redirection operator. Directs the standard output of the command on the left to the file specified on the right. - ```devops.txt```: name of the file where the output of "free -m" command will be saved.



Task 6:

Create a script that will create a file with name "disk.txt" and insert the disk utilization details of your Linux machine in that file. - Command used: - ```vi task_6.sh``` Inside task_6.sh Vim Editor: - ```#!/bin/bash``` - ```df -h > disk.txt``` - ```cat disk.txt``` Outside task_6.sh Vim Editor: - ```chmod 755 task_6.sh``` - ```./task_6.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```df```: used to display information about disk space usage. - ```-h```: option for "df" command, human readable.



Task 7:

Create a script that will print "Even Day" if current date is an even number. - Command used: - ```vi task_7.sh``` Inside task_7.sh Vim Editor: - ```#!/bin/bash``` - ```DATE=$ (date +'%d')``` - ```if [ $((DATE % 2)) -eq 0 ]; then``` - ```echo "Even Day"``` - ```else echo "Not Even Day"``` - ```fi``` Outside task_7.sh Vim Editor: - ```chmod 755 task_7.sh``` - ```./task_7.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```DATE=$ (date +'%d')```: used to capture the day of the month correctly and store in a variable called "DATE". - ```%d```: used to obtain the day of the month. - ```if [ $((DATE % 2)) -eq 0 ]; then```: if statement, checks whether the day of the month is even by performing the following steps: - ```((DATE % 2))```: calculates the remainder when "DATE" is divided by 2. - ```[ ... -eq 0 ]```: checks if the result of '((DATE % 2))' is equal to 0, which means the day is even. - ```echo "Even Day"```: if the day of the month is even, this command will be executed, and print out "Even Day". - ```else echo "Not Even Day"```: if the day of the month is not even, this command will be executed, and print out "Not Even Day". - ```fi```: closing keyword for if statement.



Task 8:

Create a script that will print "OK" if current hour is an even number. - Command used: - ```vi task_8.sh``` Inside task_8.sh Vim Editor: - ```#!/bin/bash``` - ```HOUR= $(date +'%H')``` - ```if [ $((HOUR % 2)) -eq 0 ]; then``` - ```echo "OK"``` - ```else echo "Not OK"``` - ```fi``` Outside task_8.sh Vim Editor: - ```chmod 755 task_8.sh``` - ```./task_8.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```HOUR= $(date +'%H')```: used to capture the hour of the day correctly and store in a variable called "HOUR". - ```%H```: used to obtain the hour of the day in 24 hours format. - ```if [ $((HOUR % 2)) -eq 0 ]; then```: if statement, checks whether the hour of the day is even by performing the following steps: - ```((HOUR % 2))```: calculates the remainder when "HOUR" is divided by 2. - ```[ ... -eq 0 ]```: checks if the result of '((HOUR % 2))' is equal to 0, which means the hour is even. - ```then```: if the "if" statement is true, then execute the following condition. - ```echo "OK"```: if the hour of the day is even, this command will be executed, and print out "OK". - ```else echo "Not OK"```: if the day of the month is not even, this command will be executed, and print out "Not OK". - ```fi```: closing keyword for if statement.



Task 9:

Create a script that will check if number is less 100 and then prints "less than 100" else prints "greater than 100". - Command used: - ```vi task_9.sh``` Inside task_9.sh Vim Editor: - ```#!/bin/bash``` - ```n=99``` - ```if [ $n -lt 100 ]; then``` - ```echo "less than 100"``` - ```else echo "greater than 100"``` - ```fi``` Outside task_9.sh Vim Editor: - ```chmod 755 task_9.sh``` - ```./task_9.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```n=99```: assigns the value 99 to variable name "n". - ```if [ $n -lt 100]```: "if" statement, checks whether the value stored in the variable "n" is less than 100. - ```-lt```: comparison operator, lt stands for "less than". - ```then```: if the "if" statement is true, then execute the following condition. - ```echo "less than 100"```: if the value for n is less than 100, this command will be executed, and print out "less than 100". - ```else echo "greater than 100"```: if the value for n is greater than 100, this command will be executed, and print out "greater than 100". - ```fi```: closing keyword for if statement.



Task 10:

Create a script that will check if number is divisible by 10 and then prints "divisible by 10" else prints "not divisible by 10". - Command used: - ```vi task_10.sh``` Inside task_10.sh Vim Editor: - ```#!/bin/bash``` - ```n=120``` - ```if [ $((n % 10)) -eq 0 ]; then``` - ```echo "divisible by 10"``` - ```else echo "not divisible by 10"``` - ```fi``` Outside task_10.sh Vim Editor: - ```chmod 755 task_10.sh``` - ```./task_10.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```n=99```: assigns the value 99 to variable name "n". - ```if [ $n -lt 100]```: "if" statement, checks whether the value stored in the variable "n" is less than 100. - ```-lt```: comparison operator, lt stands for "less than". - ```then```: if the "if" statement is true, then execute the following condition. - ```echo "less than 100"```: if the value for n is less than 100, this command will be executed, and print out "less than 100". - ```else echo "greater than 100"```: if the value for n is greater than 100, this command will be executed, and print out "greater than 100". - ```fi```: closing keyword for if statement.



Task 11:

Write a script that will ask username and password from user and then using if condition verify if username="admin" and password="admin" , if condition meets then print "Login Success" else print "Wrong Login". - Command used: - ```vi task_11.sh``` Inside task_11.sh Vim Editor: - ```#!/bin/bash``` - ```echo "Enter your username:"``` - ```read username``` - ```echo "Enter your password:"``` - ```read password``` - ```if [[ $username == "admin" && $password == "admin" ]]; then``` - ```echo "Login Success"``` - ```else echo "Wrong Login"``` - ```fi``` Outside task_11.sh Vim Editor: - ```chmod 755 task_11.sh``` - ```./task_11.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```echo "Enter your username:```: to display a prompt to the user, asking them to enter their username. - ```read username```: uses the read command to capture the user's input for the username, and it stores the input in a variable named "username". - ```echo "Enter your password:"```: displays a prompt asking the user to enter their password. - ```read password```: captures the user's input for the password and stores it in a variable named "password". - ```if [[ $username == "admin" && $password == "admin" ]]; then```: if statement that checks whether the value stored in the username variable is equal to "admin" and whether the value stored in the password variable is equal to "admin." - ```&&```: operator is used for logical AND, meaning both conditions must be true for the code block to be executed. - ```then```: if the "if" statement is true, then execute the following condition. - ```echo "Login Success"```: if the "if" statement is true, met (both username and password are "admin"), then print out "Login Success". - ```else echo "Wrong Login"```: if the "if" statement is not met, then print out "Wrong Login". - ```fi```: closing keyword for if statement.



Task 12:

Write a script that will ask a number from user and then using if condition verify if number is divisible by 3 and 5 both , if condition meets then print "Divisible" else print "Not Divisible". - Command used: - ```vi task_12.sh``` Inside task_12.sh Vim Editor: - ```#!/bin/bash``` - ```echo "Enter your number:"``` - ```read number``` - ```if [[ ($((number % 3)) -eq 0) && ($((number % 5)) -eq 0) ]]; then``` - ```echo "Divisible"``` - ```else echo "Not Divisible"``` - ```fi``` Outside task_12.sh Vim Editor: - ```chmod 755 task_12.sh``` - ```./task_12.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```echo "Enter your number:```: to display a prompt to the user, asking them to enter their number. - ```read number```: uses the read command to capture the user's input for the number, and it stores the input in a variable named "number". - ```if [[ ($((number % 3)) -eq 0) && ($((number % 5)) -eq 0) ]]; then```: if statement that checks whether the value stored in the number variable is divisible by "3" and "5". - ```&&```: operator is used for logical AND, meaning both conditions must be true for the code block to be executed. - ```then```: if the "if" statement is true, then execute the following condition. - ```echo "Divisible"```: if the "if" statement is true, met (number are divisible by 3 and 5), then print out "Divisible". - ```else echo "Not Divisible"```: if the "if" statement is not met, then print out "Not Divisible". - ```fi```: closing keyword for if statement.



Task 13:

Write a script that will ask a number from user and then using if condition verify if number is divisible by 3 or 5 (any one) , if condition meets then print "Divisible" else print "Not Divisible". - Command used: - ```vi task_13.sh``` Inside task_13.sh Vim Editor: - ```#!/bin/bash``` - ```echo "Enter your number:"``` - ```read number``` - ```if [[ ($((number % 3)) -eq 0) || ($((number % 5)) -eq 0) ]]; then``` - ```echo "Divisible"``` - ```else echo "Not Divisible"``` - ```fi``` Outside task_13.sh Vim Editor: - ```chmod 755 task_13.sh``` - ```./task_13.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```echo "Enter your number:```: to display a prompt to the user, asking them to enter their number. - ```read number```: uses the read command to capture the user's input for the number, and it stores the input in a variable named "number". - ```if [[ ($((number % 3)) -eq 0) || ($((number % 5)) -eq 0) ]]; then```: if statement that checks whether the value stored in the number variable is divisible by "3" or "5". - ```||```: operator is used for logical OR, meaning only one condition must be true for the code block to be executed. - ```then```: if the "if" statement is true, then execute the following condition. - ```echo "Divisible"```: if the "if" statement is true, met (number are divisible by 3 or 5), then print out "Divisible". - ```else echo "Not Divisible"```: if the "if" statement is not met, then print out "Not Divisible". - ```fi```: closing keyword for if statement.



Task 14:

Write a script that will ask a number from user and then using if condition verify if number is divisible by 3 or 5 or 7 (any one) , if condition meets then print "Divisible" else print "Not Divisible". - Command used: - ```vi task_14.sh``` Inside task_14.sh Vim Editor: - ```#!/bin/bash``` - ```echo "Enter your number:"``` - ```read number``` - ```if [[ ($((number % 3)) -eq 0) || ($((number % 5)) -eq 0) || ($((number % 7)) -eq 0) ]]; then``` - ```echo "Divisible"``` - ```else echo "Not Divisible"``` - ```fi``` Outside task_14.sh Vim Editor: - ```chmod 755 task_14.sh``` - ```./task_14.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```echo "Enter your number:```: to display a prompt to the user, asking them to enter their number. - ```read number```: uses the read command to capture the user's input for the number, and it stores the input in a variable named "number". - ```if [[ ($((number % 3)) -eq 0) || ($((number % 5)) -eq 0) || ($((number % 7)) -eq 0) ]]; then```: if statement that checks whether the value stored in the number variable is divisible by "3" or "5" or "7". - ```||```: operator is used for logical OR, meaning only one condition must be true for the code block to be executed. - ```then```: if the "if" statement is true, then execute the following condition. - ```echo "Divisible"```: if the "if" statement is true, met (number are divisible by 3 or 5 or 7), then print out "Divisible". - ```else echo "Not Divisible"```: if the "if" statement is not met, then print out "Not Divisible". - ```fi```: closing keyword for if statement.



Task 15:

Write a script that will print numbers 1 to 20 on screen using for loop. - Command used: - ```vi task_15.sh``` Inside task_15.sh Vim Editor: - ```#!/bin/bash``` - ```for ((i=1; i<=20; i++); do``` - ```echo $i``` - ```done``` Outside task_15.sh Vim Editor: - ```chmod 755 task_15.sh``` - ```./task_15.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```for ((i=1; i<=20; i++); do```: initiates a for loop. Here's what each part does: - ```((i=1; i<=20; i++))```: the loop control instructure: - ```i=1```: initializes a variable "i" to 1. - ```i<=20```: set a condition "i<=20", "i" is less than or equal to 20. - ```i++```: increment the value of the variable "i" by 1 in each iteration of the loop. - ```; do```: keyword marks the beginning of the loop's body. - ```echo $i```: print out the variable i on each iteration of the loop. - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 16:

Write a script that will create a directory with name "dummy" and then generate 10 empty files named like 1, 2 ... 10 in that dummy directory using for loop. - Command used: - ```vi task_16.sh``` Inside task_16.sh Vim Editor: - ```#!/bin/bash``` - ```mkdir dummy``` - ```for ((i=1; i<=10; i++)); do``` - ```touch dummy/$i``` - ```done``` - ```ls -lrt dummy/``` Outside task_16.sh Vim Editor: - ```chmod 755 task_16.sh``` - ```./task_16.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```mkdir dummy```: creates a directory named "dummy" in the current working directory. - ```for ((i=1; i<=10; i++); do```: initiates a for loop. Here's what each part does: - ```for ((i=1; i<=10; i++))```: the loop control instructure: - ```i=1```: initializes a variable "i" to 1. - ```i<=10```: set a condition "i<=10", "i" is less than or equal to 10. - ```i++```: increment the value of the variable "i" by 1 in each iteration of the loop. - ```; do```: keyword marks the beginning of the loop's body. - ```touch dummy/$i```: inside the loop, this line uses the touch command to create an empty file in the "dummy" directory. The filename is determined by the loop variable i. For example, in the first iteration, it creates a file named "dummy/1." - ```done```: closing keyword for the loop, indicating the end of the loop's body. - ```ls -lrt dummy/```: after the loop, this command lists the contents of the "dummy" directory using the ls command. - ```-lrt```: options "-lrt" are used to display the contents in reverse order ("r") of modification time ("t") and with detailed information ("l").



Task 17:

Write a script that will iterate between 1 to 100 and print all numbers divisible by 5. - Command used: - ```vi task_17.sh``` Inside task_17.sh Vim Editor: - ```#!/bin/bash``` - ```for ((i=1; i<=100; i++)); do``` - ```if [ $((i % 5)) -eq 0 ]; then``` - ```echo $i``` - ```fi``` - ```done``` Outside task_17.sh Vim Editor: - ```chmod 755 task_17.sh``` - ```./task_17.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```for ((i=1; i<=100; i++)); do```: initiates a for loop. Here's what each part does: - ```for ((i=1; i<=100; i++))```: the loop control instructure: - ```i=1```: initializes a variable "i" to 1. - ```i<=100```: set a condition "i<=100", "i" is less than or equal to 100. - ```i++```: increment the value of the variable "i" by 1 in each iteration of the loop. - ```; do```: keyword marks the beginning of the loop's body. - ```if [ $((i % 5)) -eq 0 ]; then```: inside the for loop, if condition: if "i" is divisible by 5. - ```then```: if the "if" statement is true, then execute the following condition. - ```echo $i```: echo command to print the current value of "i" if it met the condition. - ```fi```: closing keyword for if statement. - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 18:

Write a script that will print numbers 1 to 10 on screen using while loop. - Command used: - ```vi task_18.sh``` Inside task_18.sh Vim Editor: - ```#!/bin/bash``` - ```i=1``` - ```while [ $i -le 10 ]; do``` - ```echo $i``` - ```((i++))``` - ```done``` Outside task_18.sh Vim Editor: - ```chmod 755 task_18.sh``` - ```./task_18.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```i=1```: initializes a counter variable named "i" to 1. - ```while [ $i -le 10 ]; do```: loop with the while condition: - ```[ $i -le 10 ]```: checks if the value of "i" is less than or equal to 10. - ```echo $i```: inside the loop, uses the echo command to print the current value of "i". - ```((i++))```: After printing the number, it increments the counter variable by 1 using ((i++)). - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 19:

Write a script that will keep running (using while loop) with "sleep 1" command until user press ctrl+c to kill it. - Command used: - ```vi task_19.sh``` Inside task_19.sh Vim Editor: - ```#!/bin/bash``` - ```while : ; do``` - ```echo "Script is running... Press Ctrl + C to exit."``` - ```sleep 1``` - ```done``` Outside task_19.sh Vim Editor: - ```chmod 755 task_19.sh``` - ```./task_19.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```while : ; do```: this line starts an infinite while loop. Here's what each part does: - ```while```: keyword that marks the beginning of the loop. - ```:```: The colon (:) is a command that always returns a true exit status, effectively creating an infinite loop. It's used as a placeholder for the loop condition, meaning the loop will run indefinitely. - ```;```: The ; (semicolon) is used to separate commands or statements in Bash. - ```do```: keyword marks the beginning of the loop's body. - ```echo "Script is running... Press Ctrl + C to exit."```: inside the loop, this line uses the echo command to print a message "script is running and that the user can press Ctrl+C to exit." - ```sleep 1```: This command introduces a 1-second pause (sleep) in each iteration of the loop. It controls the speed at which the loop runs. - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 20:

Write a script that will keep printing numbers from 1 to 10, if the number in loop is 2 then loop must break and shall not continue to print. - Command used: - ```vi task_20.sh``` Inside task_20.sh Vim Editor: - ```#!/bin/bash``` - ```i=1``` - ```while [ $i -lt 10 ] ; do``` - ```echo $i``` - ```((i++))``` - ```if [[ $i -eq 2 ]]; then break``` - ```fi``` - ```done``` Outside task_20.sh Vim Editor: - ```chmod 755 task_20.sh``` - ```./task_20.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```i=1```: initializes a counter variable named "i" to 1. - ```while [ $i -lt 10 ] ; do```: loop with the while condition: - ```[ $i -lt 10 ]```: checks if the value of "i" is less than 10. - ```;```: The ; (semicolon) is used to separate commands or statements in Bash. - ```do```: keyword marks the beginning of the loop's body. - ```echo $i```: inside the loop, uses the echo command to print the current value of "i". - ```((i++))```: After printing the number, it increments the counter variable by 1 using ((i++)). - ```if [[ $i -eq 2 ]]; then break```: if statement, checks the condition: - ```[[ $i -eq 2 ]]```: if statement checks, the value of the variable "$i" = 2. - ```;```: The ; (semicolon) is used to separate commands or statements in Bash. - ```then```: if the "if" statement is true, then execute the following condition. - ```break```: break command is used to exit the nearest enclosing loop. In this case, it breaks out of the while loop in which it's contained. - ```fi```: closing keyword for if statement. - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 21:

Write a script that will keep printing numbers from 1 to 5, and skip if number is 3 (Use while loop and Continue statement). - Command used: - ```vi task_21.sh``` Inside task_21.sh Vim Editor: - ```#!/bin/bash``` - ```i=1``` - ```while [ $i -le 5 ] ; do``` - ```if [ $i -eq 3 ]; then``` - ```((i++))``` - ```continue``` - ```fi``` - ```echo $i``` - ```((i++))``` - ```done``` Outside task_21.sh Vim Editor: - ```chmod 755 task_21.sh``` - ```./task_21.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```i=1```: initializes a counter variable named "i" to 1. - ```while [ $i -le 5 ] ; do```: loop with the while condition: - ```[ $i -le 5 ]```: checks if the value of "i" is less than or equal to 5. - ```;```: The ; (semicolon) is used to separate commands or statements in Bash. - ```do```: keyword marks the beginning of the loop's body. - ```if [ $i -eq 3 ]; then```: if statement, checks the condition: - ```[ $i -eq 3 ]``` if statement checks, the value of the variable "$i" = 3. - ```;```: The ; (semicolon) is used to separate commands or statements in Bash. - ```then```: if the "if" statement is true, then execute the following condition. - ```((i++))```: if statement to check if i is equal to 3. If i is 3, it increments i by 1. - ```continue```: statement to skip the rest of the loop's body and start the next iteration. - ```fi```: closing keyword for if statement. - ```echo $i```: inside the loop, uses the echo command to print the current value of "i". - ```((i++))```: After printing the number, it increments the counter variable by 1 using ((i++)). - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 22:

Write a script that will keep printing odd numbers between 1 to 100. - Command used: - ```vi task_22.sh``` Inside task_22.sh Vim Editor: - ```#!/bin/bash``` - ```for (( i = 1; i <= 100; i=i+2)); do``` - ```echo $i``` - ```done``` Outside task_22.sh Vim Editor: - ```chmod 755 task_22.sh``` - ```./task_22.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```for (( i = 1; i <= 100; i=i+2)); do```: This line initiates a for loop. Here's what each part does: - ```(( i = 1; i <= 100; i = i + 2))```: This is the loop control structure. - ```i=1```: initializes a variable "i" to 1. - ```i <= 100```: set a condition, "i" is less than or equal to 100. - ```i = i + 2```: increments "i" by 2 in each iteration. - ```;```: The ; (semicolon) is used to separate commands or statements in Bash. - ```do```: keyword marks the beginning of the loop's body. - ```echo $i```: inside the loop, uses the echo command to print the current value of "i". - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 23:

Write a script that will keep printing even numbers between 1 to 100. - Command used: - ```vi task_23.sh``` Inside task_23.sh Vim Editor: - ```#!/bin/bash``` - ```for (( i = 2; i <= 100; i=i+2)); do``` - ```echo $i``` - ```done``` Outside task_23.sh Vim Editor: - ```chmod 755 task_23.sh``` - ```./task_23.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```for (( i = 2; i <= 100; i=i+2)); do```: This line initiates a for loop. Here's what each part does: - ```(( i = 2; i <= 100; i = i + 2))```: This is the loop control structure. - ```i=2```: initializes a variable "i" to 2. - ```i <= 100```: set a condition, "i" is less than or equal to 100. - ```i = i + 2```: increments "i" by 2 in each iteration. - ```;```: The ; (semicolon) is used to separate commands or statements in Bash. - ```do```: keyword marks the beginning of the loop's body. - ```echo $i```: inside the loop, uses the echo command to print the current value of "i". - ```done```: closing keyword for the loop, indicating the end of the loop's body.



Task 24:

Write a script that will ask 2 numbers from user and then prints the sum of given 2 numbers. - Command used: - ```vi task_24.sh``` Inside task_24.sh Vim Editor: - ```#!/bin/bash``` - ```echo "Enter the first number."``` - ```read num1``` - ```echo "Enter the second number."``` - ```read num2``` - ```echo "Sum of the numbers is: $(( num1 + num2 ))"``` Outside task_24.sh Vim Editor: - ```chmod 755 task_24.sh``` - ```./task_24.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```echo "Enter the first number."```: This line uses the "echo" command to print the specified message, "Enter the first number" to the terminal. - ```read num1```: This line uses the "read" command to take user input from the terminal and store it in a variable named num1. The user's input is expected to be the first number. - ```echo "Enter the second number."```: This line uses the "echo" command to print the specified message, "Enter the second number" to the terminal. - ```read num2```: This line uses the "read" command to take user input from the terminal and store it in a variable named num2. The user's input is expected to be the second number. - ```echo "Sum of the numbers is: $(( num1 + num2 ))"```: This line uses the echo command to print the message "Sum of the numbers is: " followed by the result of the addition of the values stored in the num1 and num2 variables.



Task 25:

Write a script that will ask 2 numbers from user and then prints the multiplication of given 2 numbers. - Command used: - ```vi task_25.sh``` Inside task_24.sh Vim Editor: - ```#!/bin/bash``` - ```echo "Enter the first number."``` - ```read num1``` - ```echo "Enter the second number."``` - ```read num2``` - ```echo "Multiplication of the numbers is: $(( num1 * num2 ))"``` Outside task_25.sh Vim Editor: - ```chmod 755 task_25.sh``` - ```./task_25.sh``` - Description: - ```#!/bin/bash```: This is the shebang line, indicating that the script should be executed using the Bash shell. - ```echo "Enter the first number."```: This line uses the "echo" command to print the specified message, "Enter the first number" to the terminal. - ```read num1```: This line uses the "read" command to take user input from the terminal and store it in a variable named num1. The user's input is expected to be the first number. - ```echo "Enter the second number."```: This line uses the "echo" command to print the specified message, "Enter the second number" to the terminal. - ```read num2```: This line uses the "read" command to take user input from the terminal and store it in a variable named num2. The user's input is expected to be the second number. - ```echo "Multiplication of the numbers is: $(( num1 * num2 ))"```: This line uses the echo command to print the message "Multiplication of the numbers is: " followed by the result of the multiplication of the values stored in the num1 and num2 variables.