LA Journal-2
LA Journal-2
LA Journal-2
- SY MSc CS 05
Practical No. - 01
Aim - Write a Simple Shell Script that Prints "Hello, Linux!" to the Terminal.
__________________________________________________________________
Theory:
A shell script is a program written for the Unix/Linux shell, which interprets and executes
commands. A simple shell script to print "Hello, Linux!" to the terminal involves writing a script
that contains a series of commands. The echo command is commonly used to display text in the
terminal, and it is key to this task. To create the script, you begin by specifying the interpreter
using a shebang (#!/bin/bash), which tells the system that the script should be run using the Bash
shell. The echo command is then used to output "Hello, Linux!" to the terminal. The script is
saved with a .sh extension, and it must be given executable permission using the chmod
command. Once this permission is granted, the script can be run from the terminal, and it will
print the desired message.
Source Code:
#!/bin/bash
Output:
Practical No. - 02
Aim - Write a Script that Lists All Files in the Current Directory and
Indicates whether Each One is a File or a Directory.
__________________________________________________________________
Theory:
This script starts by using a for loop to iterate through all items in the current directory (* is a
wildcard that matches all files and directories). For each item, an if condition checks whether the
item is a directory or a file using the -d and -fflags, respectively. Depending on the result, the
script prints whether the item is a directory, file, or something else (like a symbolic link). The
script helps in identifying file types efficiently and can be expanded for more complex file
management tasks in Linux.
Source Code:
#!/bin/bash
Output:
Practical No. - 03
Aim - Develop a Script that Prompts the User to Enter their Name and Prints
a Personalized Greeting.
__________________________________________________________________
Theory:
This script begins by displaying a message asking the user to input their name using the echo
command. The read command captures the user's input and stores it in the variable name. After
the input is captured, the echo command prints a personalized greeting by referencing the
variable, integrating the user's name into the output message. This kind of script is useful for
creating interactive applications, user prompts, and basic input handling in shell environments. It
showcases the simplicity and flexibility of shell scripting for handling user interaction.
Source Code:
#!/bin/bash
Output:
Practical No. - 04
Aim - Write a Script that Compresses a Given File using Both gzip and bzip2,
Creating Two Compressed Versions.
__________________________________________________________________
Theory:
The script starts by prompting the user to enter the name of the file they wish to compress. The
read command captures the input and stores it in the variable filename. Next, the script checks if
the file exists using the -f option in the if statement. If the file is found, it is compressed using
both gzip and bzip2. The -c flag in both commands ensures that the compressed output is written
to a new file rather than replacing the original one. The compressed files will have .gz and .bz2
extensions. If the file is not found, the script informs the user to provide a valid filename.
Source Code:
#!/bin/bash
Practical No. - 04
Aim - Write a Script that Compresses a Given File using Both gzip and bzip2,
Creating Two Compressed Versions.
__________________________________________________________________
Output:
Practical No. - 05
Aim - Create a Script that Displays the Disk Space Usage of Each Directory in
the Current Location.
__________________________________________________________________
Theory:
This script uses the du command, which calculates the disk space used by files and directories.
The -h option displays the disk usage in a human-readable format (e.g., 1K, 10M, 2G). The
--max-depth=1 option ensures that du only calculates the disk space for top-level directories in
the current location, without drilling down into subdirectories. This prevents an overwhelming
amount of detail and focuses on summarizing usage at the directory level.
Source Code:
#!/bin/bash
Practical No. - 05
Aim - Create a Script that Displays the Disk Space Usage of Each Directory in
the Current Location.
__________________________________________________________________
Output:
Practical No. - 06
Aim - Write a Script to Automate the Process of Creating a New Partition on
a Specified Disk.
__________________________________________________________________
Theory:
This script automates the creation of a new partition on a specified disk by using fdisk in batch
mode. The script starts by checking if it is being run with root privileges using the $EUID
variable. If not, it prompts the user to run it as root. Next, it prompts the user to enter the name of
the disk they want to partition (e.g., /dev/sda) and checks if the disk exists using the -b option in
the if statement to verify the block device. The script then passes commands to fdisk using a
heredoc syntax ((...) | fdisk $disk). The commands include:
● n: Create a new partition.
● p: Specify it as a primary partition.
● 1: Use partition number 1.
● Default values for the first and last sectors (by pressing Enter).
● w: Write the changes to the disk.
Source Code:
#!/bin/bash
Practical No. - 06
Aim - Write a Script to Automate the Process of Creating a New Partition on
a Specified Disk.
__________________________________________________________________
# Inform the user about the need to format the new partition
echo "The partition has been created. You need to format it before use. For example, you can
use:"
echo "mkfs.ext4 ${disk}${partition_number}"
Output:
Practical No. - 07
Aim - Develop a Script that Checks the Status of Essential System Services
(e.g., SSH, Apache) and Prints whether they are Running.
__________________________________________________________________
Theory:
This script starts by defining an array (services) that contains the names of essential services
such as SSH (ssh) and Apache (apache2). The for loop iterates through each service in the array,
and for each service, the systemctl is-active command is used to check if the service is active
(i.e., running). The output of this command is stored in the variable status.
Source Code:
#!/bin/bash
Practical No. - 07
Aim - Develop a Script that Checks the Status of Essential System Services
(e.g., SSH, Apache) and Prints whether they are Running.
__________________________________________________________________
Output:
Practical No. - 08
Aim - Create a Script that Asks the User if they want to Shut Down
the System and Performs Accordingly.
__________________________________________________________________
Theory:
This script starts by asking the user if they want to shut down the system using the echo and read
commands. The user’s response is stored in the response variable. The if statement checks the
value of the response:
● If the user types "yes," the system will shut down immediately using the sudo shutdown
-h now command (-hstands for "halt," which powers off the system).
● If the user types "no," the script prints a message saying the shutdown is canceled and
exits.
● If the input is anything other than "yes" or "no," the script will notify the user of invalid
input and end without taking any further action.
Source Code:
#!/bin/bash
Practical No. - 08
Aim - Create a Script that Asks the User if they want to Shut Down
the System and Performs Accordingly.
__________________________________________________________________
Output:
Practical No. - 09
Aim - Write a Script that Generates a SSH Key Pair and Provides
Instructions for Adding the Public Key to the authorized_keys File.
__________________________________________________________________
Theory:
This script automates the process of generating an SSH key pair using the ssh-keygen command.
The key pair consists of a private key (stored in ~/.ssh/id_rsa) and a public key (stored in
~/.ssh/id_rsa.pub). The -t rsa option specifies that an RSA key should be generated, and the -b
4096 option defines the key size as 4096 bits for stronger encryption. The -N "" option specifies
an empty passphrase, meaning the private key is not encrypted with a passphrase. If a passphrase
is preferred, the user can remove the -N "" part, and the script will prompt for one. Once the key
pair is generated, the script provides instructions for adding the public key to the
authorized_keys file on the remote server. It suggests using the ssh-copy-id command, which
automates the process of copying the public key to the server. Alternatively, it guides the user to
manually copy the contents of the public key (id_rsa.pub) to the ~/.ssh/authorized_keys file on
the server, explaining how to view the public key using the cat command.
Source Code:
#!/bin/bash
Practical No. - 09
Aim - Write a Script that Generates a SSH Key Pair and Provides
Instructions for Adding the Public Key to the authorized_keys File.
__________________________________________________________________
Output: