0% found this document useful (0 votes)
23 views25 pages

Faculty of Engineering and Technology Bachelor of Technology

Uploaded by

smitmalaviya49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views25 pages

Faculty of Engineering and Technology Bachelor of Technology

Uploaded by

smitmalaviya49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

2303031260068 KLSS LABORATORY

FACULTY OF ENGINEERING AND TECHNOLOGY


BACHELOR OF TECHNOLOGY

KALI LINUX AND SHELL SCRIPTING


(303105213)

III SEMESTER

Computer Science & Engineering Department

CSE-PIET(CS) Page-1
2303031260068 KLSS LABORATORY

CERTIFICATE

This is to certify that


Mr. Goti Rudra Mukeshbhai with enrolment no. 2303031260068

has successfully completed his/her laboratory experiments in the


KALI LINUX AND SHELL SCRIPTING (303105213) from the
department of COMPUTER SCIENCE & ENGINEERING during
the academic year 2024-2025.

Date of Submission:......................... Staff In charge:........................... Head

Of Department:...........................................

CSE-PIET(CS) Page-2
2303031260068 KLSS LABORATORY

KALI LINUX AND SHELL SCRIPTING PRACTICAL BOOK


COMPUTER SCIENCE & ENGINEERING DEPARTMENT
PREFACE

It gives us immense pleasure to present the first edition of the KALI LINUX AND
SHELL SCRIPTING Practical Book for the B.Tech . 3rd semester students for
PARUL UNIVERSITY.

The KLSS theory and laboratory courses at PARUL UNIVERSITY, WAGHODIA,


VADODARA are designed in such a way that students develop the basic
understanding of the subject in the theory classes and then try their hands on
the experiments to realize the various implementations of problems learnt
during the theoretical sessions. The main objective of the KLSS laboratory
course is: Learning KLSS through Experimentations. All the experiments are
designed to illustrate various problems in different areas of KLSS and also to
expose the students to various uses.

The objective of this KLSS Practical Book is to provide a comprehensive source


for all the experiments included in the KLSS laboratory course. It explains all the
aspects related to every experiment such as: basic underlying concept and how
to analyze a problem. It also gives sufficient information on how to interpret and
discuss the obtained results.

We acknowledge the authors and publishers of all the books which we have
consulted while developing this Practical book. Hopefully this KLSS Practical
Book will serve the purpose for which it has been developed.

CSE-PIET(CS) Page-3
2303031260068 KLSS LABORATORY

INSTRUCTIONS TO STUDENTS

1. The main objective of the KLSS laboratory is: Learning through the
Experimentation. All the experiments are designed to illustrate various
problems in different areas of KLSS and also to expose the students to
various problems and their uses.

2. Be prompt in arriving to the laboratory and always come well


prepared for the practical. 3. Every student should have
his/her individual copy of the KLSS Practical Book.
3. Every student have to prepare the notebooks specifically reserved for
the KLSS practical work: ” KLSS Practical Book”

4. Every student has to necessarily bring his/her KLSS Practical Book, KLSS
Practical Class Notebook and KLSS Practical Final Notebook.

5. Finally find the output of the experiments along with the problem and note
results in the KLSS Practical Notebook.
6. The grades for the KLSS practical course work will be awarded based
on our performance in the laboratory, regularity, recording of
experiments in the KLSS Practical Final Notebook, lab quiz, regular
viva-voce and end-term examination.

CSE-PIET(CS) Page-4
2303031260068 KLSS LABORATORY

Faculty of Engineering & Technology (FET)


Parul Institute of Engineering &Technology (PIET)
Department of Computer Science &Engineering

Practical Assessment Table


Sr. Practical Page No. Ma Sign
No Title rks
Fro To
(10
m
)
1 1. Write a shell script to
display a greeting
message using variables.
2. Create a script that
uses loops to print
numbers from 1 to 10.
2 Write a script to automate the
process of creating multiple
directories.
3 Experiment with
different ways of
executing shell scripts
(e.g., ./script.sh, sh
script.sh).
4 Practice text
CSE-PIET(CS) Page-5
2303031260068 KLSS LABORATORY

processing by writing a
script to search for a
specific pattern in a
file using grep.
5 Write a script that
demonstrates the use of
functions to perform repetitive
tasks.
6 1. Write a script to monitor
CPU usage and send an alert if
it exceeds a certain threshold.
2. Create backup and restore
scripts for critical system files
and directories
7 Design custom scripts
tailored to specific
system administration
tasks in your
environment.
8 Develop a script to
remotely administer
multiple Linux machines
over SSH.

9 Build an interactive script that


prompts users for input and
performs actions accordingly.

CSE-PIET(CS) Page-6
2303031260068 KLSS LABORATORY

Practical 1

1. Write a shell script to display a greeting message using variables.

2. Create a script that uses loops to print numbers from 1 to 10.

CSE-PIET(CS) Page-7
2303031260068 KLSS LABORATORY

AIM1 : Shell script to display a greeting message using variables.

About shell scripting :-

 Interpreter: The shell (such as Bash, Zsh, or others) interprets and executes the script
commands.

 Commands and Syntax: Shell scripts use familiar Unix commands and syntax. They
include control structures (like loops and conditionals), variables, functions, and file
operations.

 Variables: Shell scripts use variables to store data and configuration values. These
variables can be manipulated and used throughout the script.

 Control Structures: Shell scripting supports control structures like if statements, for and
while loops, case statements, etc., allowing for conditional execution and looping.

 Functions: Like in other programming languages, functions in shell scripting allow for
modularization and code reuse.

 Input and Output: Shell scripts handle input from users or other programs and produce
output that can be displayed in the terminal or redirected to files.

 Error Handling: Scripts can handle errors through exit codes, error messages, and error
handling mechanisms.

Description:

In this practical, we will create a simple shell script in Kali Linux to display a greeting message
using variables. Shell scripting helps automate tasks and can be used to execute commands
efficiently. By defining and using variables, we can make our scripts more flexible and reusable.

Tools:

Virtual Machine: A shell script in a virtual machine environment with Kali Linux installed,
providing a controlled and safe learning environment.

CSE-PIET(CS) Page-8
2303031260068 KLSS LABORATORY

Steps:-

Code:

#!/bin/bash : This is the shebang line. It tells the system that the script should be executed using the
Bash shell.

var=”HELLO WORLD” : The line ‘Var="HELLO WORLD"’ assigns the string ‘"HELLO WORLD"’ to
the variable ‘Var’.

echo $var : The line ‘echo $Var’ prints the value of the variable ‘Var’ to the terminal, which is
‘HELLO WORLD’.

Step 1 : Open Terminal

Step 2: Create Script File: ‘nano 11.sh’

Step 3: Write Script:

#!/bin/bash
var="HELLO WORLD"
echo $Var

Step 4: Save and Exit: Ctrl + S and Ctrl + X

Step 5: Make Script Executable: ‘chmod +x 11.sh’

Step 6: Run Script: ‘./11.sh’

CSE-PIET(CS) Page-9
2303031260068 KLSS LABORATORY

OUTPUT:-

Conclusion:

In this practical, we successfully created and executed a simple shell script in Kali Linux that
displays a greeting message using variables.

CSE-PIET(CS) Page-10
2303031260068 KLSS LABORATORY

AIM2: Script that user loops to print numbers from 1 to 10.

Description:

In this practical, we will create a shell script in Kali Linux that uses a loop to print numbers from 1
to 10. Loops are a fundamental programming construct that allow repetitive execution of a block of
code. By using a ‘for’ loop, we will automate the task of printing each number in the specified range,
demonstrating how loops can simplify repetitive tasks and enhance scripting efficiency in Unix-like
operating systems.

Tools:

Virtual Machine: A shell script in a virtual machine environment with Kali Linux installed,
providing a controlled and safe learning environment.

Steps:

Code:

CSE-PIET(CS) Page-11
2303031260068 KLSS LABORATORY

#!/bin/bash : This is the shebang line. It tells the system that the script should be
executed using the Bash shell.

for i in 1 2 3 4 5 6 7 8 9 10 : This line starts a ‘for’ loop. The loop variable ‘i’ will take
on each value in the list ‘1 2 3 4 5 6 7 8 9 10’ one by one.

do : This line indicates the start of the commands to be executed within the loop. All
commands after this line and before the ‘done’ statement will be executed repeatedly
for each value of ‘i’.

echo “Current $i” : This command prints the current value of the loop variable ‘i’.
The ‘echo’ command outputs the string ‘"Current "’ followed by the value of ‘i’.

done : This line indicates the end of the ‘for’ loop. The script will return to the ‘for’
statement and assign the next value in the list to ‘i’ until all values have been
processed.

Step 1 : Open Terminal

Step 2: Create Script File: ‘nano 12.sh’

Step 3: Write Script:


#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9 10
do
echo “Current $i”
done

Step 4: Save and Exit: Ctrl + S and Ctrl + X

Step 5: Make Script Executable: ‘chmod +x 12.sh’

Step 6: Run Script: ‘./12.sh’

Output:

CSE-PIET(CS) Page-12
2303031260068 KLSS LABORATORY

Using While Loop:

#!/bin/bash : This is the shebang line. It tells the system that the script should be
executed using the Bash shell.
CSE-PIET(CS) Page-13
2303031260068 KLSS LABORATORY

i=1 : Here we create a variable and give the value 1.


While(($i<= 10)) : Here we are using the while loop to print the numbers
from 1 to 10. Here (($i<=- 10)) means the value of ‘i’ will go till number 10.
do:This refers to the beginning of the loop.

echo “Number $i” : By using echo we can print the values of ‘Number $i’.

((i++)) : I n this after printing one number it increase one and print the other number

Conclusion:

In this practical, we created a shell script in Kali Linux that uses a `for` loop to print numbers from 1
to 10. By writing and executing this script, we demonstrated how loops can be utilized to automate
repetitive tasks, making our scripts more efficient and concise. This exercise provided a hands-on
approach to understanding basic loop constructs in shell scripting, reinforcing the importance of
automation in managing and executing multiple commands in Unix-like operating systems.

PRACTICAL – 2

CSE-PIET(CS) Page-14
2303031260068 KLSS LABORATORY

Write a script to automate the process of creating multiple directories.

AIM : Write a script to automate the process of creating multiple


directories.

Description of Tools:
CSE-PIET(CS) Page-15
2303031260068 KLSS LABORATORY

 Bash Scripting: Bash (Bourne Again SHell) is a Unix shell and


command language used for scripting and automation tasks.
 Text Editor: Any text editor can be used to write and edit the bash script.
Common ones include nano, vim, gedit, etc.
 Terminal: To run the bash script, a terminal emulator like gnome-
terminal, xterm, or the integrated terminal in IDEs can be used.

Steps :

1. Create the Bash Script: Open your preferred text editor and create a new file. Save it
with a .sh extension, for example, create_directories.sh.

2. Write the Script:

#!/bin/bash

# Bash script to create multiple directories

echo "Enter the directory names separated by spaces:"

read -r directories

# Create each directory

for dir in $directories; do

mkdir -p "$dir"

echo "Created directory: $dir"

CSE-PIET(CS) Page-16
2303031260068 KLSS LABORATORY

done

echo "Directory creation completed."

Script Explanation:
#!/bin/bash: This line specifies the interpreter (in this case, bash).

echo "Enter the directory names separated by spaces:": Displays a prompt message for the
user to enter directory names.

read -r directories: Reads user input and stores it in the variable directories.

for dir in $directories; do: Iterates over each directory name entered by the user.

mkdir -p "$dir": Creates each directory specified by $dir. The -p option ensures that parent
directories are created as needed.

echo "Created directory: $dir": Outputs a message confirming each directory creation.

echo "Directory creation completed.": Indicates the end of the script execution.

Output:

CSE-PIET(CS) Page-17
2303031260068 KLSS LABORATORY

Conclusion:

This practical demonstrates the effectiveness of bash scripting for automating repetitive tasks
like directory creation. By using simple commands and user input, complex tasks can be
streamlined, saving time and reducing errors in manual execution. Bash scripting is a
powerful tool for automation in Unix-like systems, offering flexibility and efficiency in
managing routine tasks.

Practical = 3
CSE-PIET(CS) Page-18
2303031260068 KLSS LABORATORY

The aim of this practical is to explore and understand different


methods of executing shell scripts in Unix-like systems.

CSE-PIET(CS) Page-19
2303031260068 KLSS LABORATORY

Aim: The aim of this practical is to explore and understand different


methods of executing shell scripts in Unix-like systems.

Description of Tools:
 Bash Script: A script named script.sh will be used for demonstration purposes.
 Terminal: A terminal emulator such as gnome-terminal, xterm, or an integrated
terminal in an IDE will be used to execute the scripts.

Steps:
1. Create a Sample Bash Script: Create a new file named script.sh using your preferred
text editor.
2. Write the Script:

#!/bin/bash
echo "Hello World! This script was executed."

Make the Script Executable:


Before running the script, ensure it is executable using the chmod command:
chmod +x script.sh

Execute the Script in Different Ways: Open a terminal and navigate to the directory
containing script.sh. Try executing the script using the following methods:

Method 1: Using ./ (Relative Path):

bash
./script.sh

This method executes the script using its relative path. Ensure you have the execute
permission (chmod +x script.sh).

Output:-

CSE-PIET(CS) Page-20
2303031260068 KLSS LABORATORY

Method 2: Using sh Command:

sh script.sh
 The sh command invokes the shell interpreter explicitly to execute the script.

 Method 3: Using bash Command:

bash script.sh

Similar to the sh command, using bash explicitly specifies the Bash shell as the interpreter
for executing the script.

Observe and Compare Outputs:


 Each method should output the message Hello World! This script was executed. to
the terminal.
 Note any differences in behavior or output between the methods.

Conclusion:
 Method Choice: Using ./script.sh is straightforward and commonly used for
executable scripts within the current directory.
 Compatibility: Using sh or bash explicitly ensures compatibility across different Unix-
like systems where the default shell might vary.
 Best Practices: It's generally recommended to use ./script.sh for executable scripts
within the current directory, ensuring proper permissions are set (chmod +x). Using
sh or bash explicitly is useful when the script requires specific shell features or
compatibility considerations.
CSE-PIET(CS) Page-21
2303031260068 KLSS LABORATORY

Practical = 4

The aim of this practical is to write a bash script that utilizes


grep to search for a specific pattern in a file.

CSE-PIET(CS) Page-22
2303031260068 KLSS LABORATORY

Aim: The aim of this practical is to write a bash script that utilizes grep
to search for a specific pattern in a file.

Description of Tools:
 Bash Scripting: Utilizing bash (Bourne Again SHell) for scripting purposes.
 Text Editor: Any text editor can be used to create and edit the bash script.
 Terminal: A terminal emulator such as gnome-terminal, xterm, or an integrated
terminal in an IDE will be used to execute the scripts.
 grep: A command-line utility for searching text patterns within files.

Steps:
1. Create a Bash Script: Open your preferred text editor and create a new file. Save it
with a .sh extension, for example, search_pattern.sh.
2. Write the Script:

#!/bin/bash
Read -p "Enter word to search:" word

Echo “line number”


Grep -n “word” hello.txt

Echo “Ignore Case”


Grep -i “word” hello.txt

Echo “Inverse Case”


Grep -v “word” hello.txt

Echo “Recursive Search”


Grep -r “word” hello.txt

Echo “Search in list (Extended Regex)”


Grep -E “word” hello.txt

CSE-PIET(CS) Page-23
2303031260068 KLSS LABORATORY

1. Script Explanation:
a. #!/bin/bash: Specifies the bash interpreter.
b. echo "Enter the pattern to search:": Prompts the user to enter the pattern
they want to search.
c. read pattern: Reads the pattern input from the user and stores it in the
variable pattern.
d. echo "Enter the filename to search in:": Prompts the user to enter the
filename where the search will be conducted.
e. read filename: Reads the filename input from the user and stores it in the
variable filename.
f. grep "$pattern" "$filename": Executes the grep command to search for the
specified pattern in the filename. grep by default outputs lines that match
the pattern.

2. Make the Script Executable: Before running the script, make it executable using the
chmod command:
a. bash
b. chmod +x search_pattern.sh

3. Prepare a Test File: Create a text file (e.g., test.txt) with some sample text content
where you will search for patterns.

4. Run the Script: Execute the script by typing ./search_pattern.sh in the terminal. Follow
the prompts to enter the pattern and filename:

CSE-PIET(CS) Page-24
2303031260068 KLSS LABORATORY

a. Enter the pattern to search:


b. Hello
c. Enter the filename to search in:
d. test.txt

5. Observe Results:
a. The script will output lines from test.txt that contain the pattern Hello (or any
other pattern you input).
b. If no lines match the pattern, there will be no output.

Conclusion:
o This practical demonstrates the use of grep within a bash script for searching
specific patterns in text files.
o grep is powerful for text processing tasks such as searching, filtering, and
manipulating text based on patterns.
o Bash scripting combined with command-line utilities like grep provides
flexibility and efficiency in handling text-based operations.

CSE-PIET(CS) Page-25

You might also like