Linux Unit 5
Linux Unit 5
1. What is a shell shell script? What are the elements of a good shell are script? How is a script
executed?
Ans
1) A shell script is a text file that contains a sequence of commands. Basically, anything that can run a
bunch of commands is considered a shell script.
2) Nevertheless, there are some rules to ensure that you create quality shell scripts scripts that not only
work well for the task for which they are written but that also will be readable by others.
Elements of a Shell Script
✓Has a unique name
✓Includes the shebang (#!) to tell the shell which subshell should execute the script
✓Includes comments—lots of comments
✓Uses the exit command to tell the shell executing the script that it has executed successfully.
✓Is Executable
Executing the Script
There are three different ways to execute Shell Scripts
1.Making the Script Executable The most common way to run a shell script is by making it executable. To
do this with the hello script from Example, use the following command:
chmod +x hello
2.Running the Script as an Argument of the Bash Command The second option for running a script is to
specify its name as the argument of the bash command. For example, the script hello would run using
the command bash hello. The advantage of running the script this way is that there is no need to make it
executable first
3.Sourcing the Script The third way of running a script is completely different. You can source the script.
By sourcing a script, you don’t run it as a subshell. Rather, you include it in the current shell. This can be
useful if the script contains variables that you want to be active in the current shell.
2. What is high availability? State and explain the requirements of high availability.
Ans
1) High availability (HA) refers to a system's ability to operate continuously without interruptions or
downtime for an extended period. It ensures that critical services remain accessible even in the event of
hardware failures, software issues, or other disruptions.
2) High availability is essential for systems that support vital functions in industries such as healthcare,
finance, and e-commerce.
3) The goal of high availability is often quantified as a percentage, such as 99.999% uptime ("five nines"),
equating to only about 5 minutes of downtime per year.
Requirements of High Availability
To achieve high availability, specific components and strategies are essential:
1. Redundant Systems
Description: Critical servers and resources should have backups to handle failures without disrupting
access.
Purpose: Ensures continuous operation by automatically switching to backup components when a failure
occurs.
2. Failover Mechanisms
Description: The ability to automatically switch to a standby system or resource in case of a failure in the
primary system.
Purpose: Reduces downtime and maintains service continuity.
3. Load Balancing
Description: Distributing incoming traffic or workloads across multiple servers or resources to prevent
overload and ensure optimal resource utilization.
Purpose: Prevents system overload and improves response times and reliability.
5. Scalability
Description: The ability of the system to scale up (add resources) or scale out (add more nodes) to
handle increased load.
Purpose: Ensures consistent performance even during high demand.
root= This parameter specifies the name of the device that contains the root file system.
default This specifies which of the proposed operating systems to load, and it refers to the sections that
are defined below the generic section in grub.conf.
timeout During boot, the user can press Tab to open the GRUB menu and add or change kernel
parameters. The timeout boot option is used to specify how much time a user has to get into the GRUB
menu.
splash image This parameter refers to a fi le containing a graphical background, which isloaded while
GRUB is displayed.
hiddenmenu This option makes sure that no GRUB menu is shown while booting. To enter the GRUB
menu, the administrator has to press the Enter key at the right moment.
6. Write a script to accept the number from user and print its multiplication table.
Ans
# Prompt the user for a number
read -p "Enter a number: " number
Save the script in a file, for example, multiplication_table.sh. Then, make the file executable by running
the following command in the terminal:
chmod +x multiplication_table.sh
7. Write steps for Configuring the DHCP Server for PXE Boot.
Ans
option space pxelinux;
option pxelinux.magic code 208 = string;
option pxelinux.configfile code 209 = text;
option pxelinux.pathprefix code 210 = text;
option pxelinux.reboottime code 211 = unsigned integer 32 ;
subnet 192.168.1.0 netmask 255.255.255.0 {
option routers 192.168.1.1 ;
range 192.168.1.200 192.168.1.250 ;
class "pxeclients"
{
match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
next-server 192.168.1.70;
filename "pxelinux/pxelinux.0";
}
}
10. What is pattern matching? Write a shell script that isolates the directory name from a
complete file name.
Ans
• A pattern-matching operator can be used to search for a pattern in a variable and if that pattern is
found, modify the variable.
• It allows to define a variable in exactly the way you want.
• Pattern-matching operators always try to locate a given string.
# Prompt the user for a file name
read -p "Enter a complete file name (including the path): " file_name
# Extract the directory name from the file name
directory_name=$(dirname "$file_name")
# Print the directory name
echo "Directory name: $directory_name”
Save the script in a file, directory_name.sh then make the file executable by running following command
in terminal:
chmod +x directory_name.sh
11. Write a shell script to add ten users, remove their passwords and add them to the group
students. Use for loop.
Ans
# Add ten users
for ((i=1; i<=10; i++))
do
username="user$i"
sudo useradd -m $username
# Remove password for the user
sudo passwd -d $username
# Add the user to the "students" group
sudo usermod -a -G students $username
echo "User $username added to the 'students' group"
done
Save the script in a file, “add_users.sh” then make the file executable by running following command in
terminal:
chmod +x add_users.sh
12. How is the network server configured as an installation server? Enumerate the steps for the
same.
Ans
1) Setting Up the Network Installation Server
1. Insert the Red Hat Enterprise Linux installation DVD in the optical drive of your server.
2. Use mkdir /www/docs/server1.example.com/install to create a subdirectory in the Apache document
root for server1.example.com.
3. Use cp -R * /www/docs/server1.example.com/install from the directory where the Red Hat Enterprise
Linux installation DVD is mounted to copy all of the files on the DVD to the install directory in your web
server document root.
4. Modify the configuration file for the server1 virtual host in /etc/httpd/conf.d/server1.example.com, and
make sure that it includes the line Options Indexes. Without this line, the virtual host will show the
contents of a directory only if it contains an index.html file.
5. Use service httpd restart to restart the Apache web server.
6. Start a browser, and browse to http://server1.example.com/install. You should now see the contents of
the installation DVD.
7. Start Virtual Machine Manager, and create a new virtual machine. Give the virtual machine the name
test netinstall, and select Network Install when asked how to install the operating system.
8. When asked for the installation URL, enter http://server1.example.com/install. The installation should
now be started.
9. You may now interrupt the installation procedure and remove the virtual machine. You have seen that
the installation server is operational. It’s time to move on to the next phase in the procedure