Faculty of Engineering and Technology Bachelor of Technology
Faculty of Engineering and Technology Bachelor of Technology
III SEMESTER
CSE-PIET(CS) Page-1
2303031260068 KLSS LABORATORY
CERTIFICATE
Of Department:...........................................
CSE-PIET(CS) Page-2
2303031260068 KLSS LABORATORY
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.
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.
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
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.
CSE-PIET(CS) Page-6
2303031260068 KLSS LABORATORY
Practical 1
CSE-PIET(CS) Page-7
2303031260068 KLSS LABORATORY
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’.
#!/bin/bash
var="HELLO WORLD"
echo $Var
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
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.
Output:
CSE-PIET(CS) Page-12
2303031260068 KLSS LABORATORY
#!/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
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
Description of Tools:
CSE-PIET(CS) Page-15
2303031260068 KLSS LABORATORY
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.
#!/bin/bash
read -r directories
mkdir -p "$dir"
CSE-PIET(CS) Page-16
2303031260068 KLSS LABORATORY
done
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
CSE-PIET(CS) Page-19
2303031260068 KLSS LABORATORY
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."
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:
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
sh script.sh
The sh command invokes the shell interpreter explicitly to execute the script.
bash script.sh
Similar to the sh command, using bash explicitly specifies the Bash shell as the interpreter
for executing the script.
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
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
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
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