0% found this document useful (0 votes)
39 views84 pages

Lab Manuals Final Suru

Uploaded by

Abi Narayanan
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)
39 views84 pages

Lab Manuals Final Suru

Uploaded by

Abi Narayanan
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/ 84

EX NO: 1

USERNAME EXISTS ON THIS SYSTEM

AIM

To Write a Linux Shell Script that accepts a username exists on this system.

ALGORITHM

Start the program.


• Clear the terminal screen.
• Prompt the user to enter a username.
• Read the username input by the user and store it in the variable username.
• Check if the user exists in the system by searching the /etc/password file:
• Use grep to search for the username in the /etc/password file.
• If a match is found:
• Output: "The user $username exists on this system."
• End the program (skip the next checks).
• If the user is not found in /etc/passwd, check if a directory for the username
exists in /home/:
• Use ls -d /home/$username to check if the directory exists.
• If the directory is found:
• Output: "The user $username does not exist on this system. However,
$username has a directory."
• End the program (skip the final check).
• "The user $username does not exist on this system."
• "And $username does not have a directory."
• End the program.
OUTPUT
PROGRAM CODE

!/usr/bin/bash
clear

echo Enter any username :


read username

if grep $username /etc/passwd


then
echo The user $username exists on this system.
echo
elif ls -d /home/$username
then
echo The user $username does not exist on this system
echo However, $username has a directory
echo
else
echo The user $username does not exist on this system
echo And $username does not have a directory
fi

RESULT
Thus The Program Has Been Executed Successfully
EX NO: 2
FILE IS EMPTY OR NOT

AIM

To Write a Linux Shell Script that accepts a filename to check whether file is empty or
not, if empty remove the file.

ALGORITHM

Start the program.


• Clear the terminal screen.
• Prompt the user to enter a filename.
• Read the filename entered by the user and store it in the variable file_name.
• Check if the file exists:
• Use the -f test to check if the file exists and is a regular file.
• If the file exists:
• Check if the file is empty:
• Use the -s test to check if the file has data in it.
• If the file has data:
• Output: "The $file_name file exists and has data in it."
• Output: "Will not remove this file."
• If the file is empty:
• Output: "The $file_name file exists, but is EMPTY."
• Ask the user: "Would you like to delete [y/n] : ".
• Read the user's choice (y or n).
• If the user chooses 'y':
• Delete the file using rm.
• Output: "$file_name file is Deleted."
• If the user chooses 'n':
• Output: "$file_name file is NOT Deleted."
• End the program (skip the next checks).
• If the file does not exist:
• Output: "$file_name file does NOT exist."
• End the program.
OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "Enter any file name to check whether file is empty or not and remove"
read file_name

if [ -f $file_name ]
then
if [ -s $file_name ]
then
echo "The $file_name file exists and has data in it."
echo "Will not remove this file."
else
echo "The $file_name file exists, but is EMPTY"
echo "Would you like to delete [y/n] : "
read choice
if [ $choice = "y" ]
then
rm $file_name
echo "$file_name file is Deleted."
else
echo "$file_name file is NOT Deleted."
fi
fi
else
echo "$file_name file does NOT exist."
fi

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 3

ACCEPTS A FILENAME

AIM

To Write a Shell script that accepts a filename, starting and ending line numbers as
arguments and displays all the lines between the given line numbers.

ALGORITHM
Start the program.

• Clear the terminal screen.


• Prompt the user to enter the following inputs:
• Existing filename (the file from which lines will be copied).
• New filename (the file where the selected lines will be saved).
• Starting line number (the first line to be copied).
• Ending line number (the last line to be copied).

• Extract the specified range of lines (from start_line to end_line) from the
filename using the sed command:

• Use sed -n $start_line,$end_line\p $filename to print the specified range of lines


from the existing file.

• Redirect the output of the sed command to create a new file (new_filename):
• Use | cat > $new_filename to save the output to the new file.
• Output the content of the new file:
• Use cat $new_filename to display the contents of the new file.
• End the program.
OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "Enter the existing filename: "


read filename
echo "Enter the new filename: "
read new_filename
echo "Enter the starting line number: "
read start_line
echo "Enter the ending line number: "
read end_line

sed -n $start_line,$end_line\p $filename | cat > $new_filename


echo
echo "File Content is : "
cat $new_filename

RESULT

Thus The Program Has Been Executed Successfully


EX NO : 4

CHECK TWO FILE CONTENT SAME OR NOT

AIM

To Write a shell script which receives two file names as arguments. It should check
whether the two file contents are same or not.

ALGORITHM

Start the program.

• Prompt the user to enter two filenames to compare:


• First filename (the first file to compare).
• Second filename (the second file to compare).
• Compare the two files using the cmp command:
• Use cmp -s $filename1 $filename2 to compare the two files. The -s option
makes cmp silent, meaning it does not output anything; it only returns a status
code:
• If the files are the same, cmp returns 0 (success).
• If the files are different, cmp returns a non-zero value (failure).
• Check the comparison result:
• If the files are the same (cmp returns 0):
• Output: "$filename1 and $filename2 files are same content".
• If the files are different (cmp returns non-zero):
• Output: "$filename1 and $filename2 files are Different content".

• End the program.


OUTPUT
PROGRAM CODE

#!/usr/bin/bash
#clear

echo "Enter First filename to compare: "


read filename1
echo "Enter second file name to compare: "
read filename2

if cmp -s $filename1 $filename2


then
echo "$filename1 and $filename2 files are same content"
else
echo "$filename1 and $filename2 files are Different content"
fi

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 5

WRITE AND EXECUTE PERMISSIONS

AIM

To Write a Shell script that displays list of all the files in the current directory to which
the user has read, write and execute permissions.

ALGORITHM

Start the program.

• Prompt the user to enter a directory name.


• Check if the directory exists:
• Use the -d test to check if the entered directory exists.
• If the directory exists:
• Change to the specified directory using cd $dir.
• List all files and directories in the specified directory using ls, and save the
output to a temporary file f.
• Open the temporary file f for reading using exec < f.
• Loop through each line (representing a file or directory) in the file f.
• For each line (i.e., each file or directory name)
• Check if it is a regular file using the -f test.
• If it is a regular file, check if it has read, write, and execute permissions:
• Use the -r, -w, and -x tests to check for read, write, and execute permissions.
• If the file has all permissions:
• Output: "$line has all permissions".
• If the file does not have all permissions:
• Output: "files not having all permissions".
• End the program.
OUTPUT
PROGRAM CODE

#!/usr/bin/bash

echo "Enter the directory name: "


read dir

if [ -d $dir ]
then
cd $dir
ls > f
exec < f
while read line
do
if [ -f $line ]
then
if [ -r $line -a -w $line -a -x $line ]
then
echo "$line has all permissions"
else
echo "files not having all permissions"
fi
fi
done
fi

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 6

FILE OR DIRECTORY REPORTS

AIM

To Write a Shell script that receives any number of file names as arguments checks if
every argument supplied is a file or a directory and reports accordingly. Whenever the
argument is a file, the number of lines on it is also reported.

ALGORITHM

Start the program.


• Prompt the user to enter a filename or directory name.
• Read the user input and store it in the variable filename_foldername.
• Loop through the entered names (in case the user enters multiple filenames or
directory names):

• For each entry (fname), do the following:


• Check if it is a regular file:
• Use the -f test to check if fname is a regular file.
• If it is a regular file:
• Output: "$fname is a File".
• Use the wc -l command to count the number of lines in the file and display the
result.
• Check if it is a directory:
• Use the -d test to check if fname is a directory.
• If it is a directory:
• Output: "$fname is a Directory".
• If neither a regular file nor a directory:
• Output: "Enter valid Filename OR Directory name".
• End the program.
OUTPUT
PROGRAM CODING

#!/usr/bin/bash

echo "Enter any filename or directory name: "


read filename_foldername

for fname in $filename_foldername


do
if [ -f $fname ]
then
echo "$fname is a File"
echo "Number of lines in the file are:"
wc -l $fname
elif [ -d $fname ]
then
echo "$fname is a Directory"
else
echo "Enter valid Filename OR Directory name"
fi
done

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 7

INTERACTIVE SCRIPT

AIM

To Write a Shell script to develop an interactive script that asks for a word and a file name
and then tells how many times that word occurred in the file.

ALGORITHM

• Start the program.


• Clear the terminal screen.
• Prompt the user to enter:
• Filename: the name of the file in which the word will be searched.
• Word: the word to search for in the file.
• Count the occurrences of the word:
• Use grep -o $word $filename to search for the word and output each occurrence
on a new line.
• Pipe the output to wc -l to count the number of occurrences of the word.
• Display the result (total number of occurrences of the word in the file).

• Count the number of lines that contain the word:


• Use grep -c $word $filename to count how many lines contain the word.
• Display the result (total number of lines containing the word).

• End the program.


OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "Enter the filename to be used : "


read filename
echo "Enter the word to be searched : "
read word

echo "The number of times the word ['$word'] occured in the file."
grep -o $word $filename|wc -l

echo "The number of line that contains ['$word'] is"


grep -c $word $filename

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 8

LIST OF ALL DIRECTORIES

AIM

To Write a Shell script to list all of the directory files in a directory.

ALGORITHM

• Start the program.


• Prompt the user to enter a directory name.
• Check if the entered name is a directory:
• Use the -d test to check if the entered name corresponds to an existing directory.
• If the directory exists:
• List the files and directories inside the specified directory using ls -l $dirname.
• Use egrep "^d" to filter and display only directories from the list.
• If the directory does not exist:
• Output an error message: "Enter Proper Directory name".

• End the program.


OUTPUT
PROGRAM CODE

# !/bin/bash
# clear

echo "Enter Directory name : "


read dirname

if [ -d $dirname ]
then
echo "List of files in the Directory: "
ls -l $dirname | egrep "^d"
else
echo "Enter Proper Directory name"
fi

RESULT

Thus The Program Has Been Executed Successfully.


EX NO: 9

UPPERCASE FILE IN CURRENT DIRECTORY

AIM

To Write a Shell script that accepts one or more file name as arguments and converts
all of them to uppercase, provided they exist in the current directory.

ALGORITHM

• Start the program.


• Prompt the user to enter the name of a file (filename).
• Display the contents of the file:
• Use the cat command to display the contents of the file specified by the user.
• Check if the file exists:
• Use -f $filename to check if the file exists and is a regular file.
• If the file does not exist, print an error message: "Filename $filename does not
exist" and exit the program with an error code 1.

• If the file exists:


• Use the tr command to convert all uppercase letters (A-Z) in the file to
lowercase letters (a-z).
• Display the converted content to the user.

• End the program.


OUTPUT
PROGRAM CODE

#!/usr/bin/bash

echo "Enter any Filename : "


read filename

echo "File content : "


cat $filename
echo

if [ ! -f $filename ]
then
echo "Filename $filename does not exists"
exit 1
fi

echo "File content after conversion: "


tr '[A-Z]' '[a-z]' < $filename

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 10

SHELL STRING OPERATION

AIM

To Write a shell script to perform the following string operations:


a. To find the length of a given string.
b. To extract a sub-string from a given string.

ALGORITHM

Start the program.

• Clear the terminal screen to provide a clean output.


• Prompt the user to enter a string.
• Calculate and display the length of the string:
• Use ${#string} to calculate the length of the string.
• Display the length of the string.

• Prompt the user to enter the starting position and ending position to extract a
substring.
• Extract and display the substring:
• Use the cut command to extract the substring based on the starting and ending
positions provided by the user.

• End the program.


OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "Enter any string to display the substring : "


read string

strlen=${#string}

echo "The length of the given string '$string' is: $strlen"

echo "Enter the Starting Possibion in the String : "


read start
echo "Enter the Ending Possition in the String : "
read end

echo "The sub-string is : "


echo $string | cut -c $start-$end

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 11

FACTORIAL FOR GIVEN NUMBER

AIM

To Write a Shell script to find factorial of a given integer.

ALGORITHM

• Start the program.


• Clear the terminal screen to provide a clean output.
• Prompt the user to enter a number for which the factorial is to be calculated.
• Initialize variables:

• Store the entered number in a variable (number1), which will be used for the
final output.
• Initialize the fact variable to 1. This variable will store the cumulative result of
the factorial calculation.

• Calculate the factorial using a while loop:


• As long as the value of number is greater than or equal to 1:
• Multiply fact by number.
• Decrement the value of number by 1.
• Display the result:
• Once the loop completes, print the factorial of the number.
• End the program
OUTPUT
PROGRAM CODE

# !/bin/bash
clear

echo "Enter any value for factorial number : "


read number

number1=$number
fact=1

while [ $number -ge 1 ]


do
fact=$(expr $fact \* $number)
number=$(expr $number - 1)
done
echo "Factorial of $number1 is $fact"

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 12

GROSS SALARY IN SHELL SCRIPT

AIM

To Write a shell script that computes the gross salary of a employee according to the
following rules:
a. If basic salary is < 15000 then HRA = 10% of the basic and DA = 90% of the
basic.
b. If basic salary is >=15000 then HRA = Rs5000 and DA = 98% of the basic.

ALGORITHM

• Start the program.


• Clear the terminal screen to provide a clean output.
• Prompt the user to enter the following details:
• Employee name.
• Employee number.
• Employee posting (job title).
• Basic salary.

• Display the entered employee details:


• Print the employee name, employee number, and posting.

• Check the basic salary:


• If the basic salary is less than 15,000:
• Calculate the gross salary using the formula:
Gross Salary = Basic Salary + 10% of Basic Salary + 90% of Basic
Salary.
• Display the calculated gross salary.
• If the basic salary is greater than or equal to 15,000:
• Calculate the gross salary using the formula:
Gross Salary = Basic Salary + 5,000 + 98% of Basic Salary.
• Display the calculated gross salary.
• End the program.
OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "Enter the Employee Name : "


read empname
echo "Enter Employee Number : "
read empno
echo "Enter Employee Posting : "
read posting
echo "Enter the Basic Salary : "
read bs

echo "Employee Name : $empname"


echo "Employee Number : $empno"
echo "Employee Posting: $posting"

if [ $bs -lt 15000 ]


then
gs=$((bs+((bs/100)*10)+(bs/100)*90))
echo "Gross Salary : $gs"
fi
if [ $bs -ge 15000 ]
then
gs=$(((bs+5000)+(bs/100)*98))
echo "Gross Salary : $gs"
fi

RESULT

Thus The Program Has Been Executed Successfully.


EX NO : 13

SHELL SCRIPT DESTINATION FILE

AIM

To write a shell-script that takes three command line arguments. The first argument is the
name of the destination file and the other two arguments are names of files to be placed in
the destination file.

ALGORITHM

• Start the program.


• Clear the terminal screen to provide a clean output.
• Prompt the user to enter the names of:
• The first source file (src1).
• The second source file (src2).
• The destination file (dest).
• Concatenate the contents of the two source files (src1 and src2) and redirect the
output into the destination file (dest) using the cat command.
• Check if the copy operation was successful:
• Use $? to capture the exit status of the cat command:
• If the status is 0 (indicating success), display the message "File Copied
Successfully" and display the content of the destination file using cat.
• If the status is non-zero (indicating an error), display the message "File
Not Copied!!!".

• End the program.


OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "Enter the Name of the First Source Filename : "


read src1
echo "Enter the Name of the Second Source file: "
read src2
echo "Enter the Name of the Destination file: "
read dest

cat $src1 $src2 > $dest


status=$?

if [ $status -eq 0 ]
then
echo “File Copied Successfully”
echo "File Content is : "
cat $dest
else
echo “File Not Copied!!!”
fi

RESULT
Thus The Program Has Been Executed Successfully.
EX NO : 14

SHELL SCRIPT LOG IN SYSTEM

AIM

To Write a Linux Shell Script to say Good Morning / Afternoon / Evening as you log in
to system

ALGORITHM

Start the program.


• Clear the terminal screen to provide a clean output.
• Get the current hour:
• Use the date command to fetch the current hour (%H).
• Check the time of day:
• If the hour is between 0 and 11 (inclusive), it's morning.
• If the hour is between 12 and 17 (inclusive), it's afternoon.
• If the hour is between 18 and 23 (inclusive), it's evening.
• Display a personalized greeting message based on the time of day, using the
current user's name:

• Use $USER to get the current username.


• Format the message to say: "Good Morning", "Good Afternoon", or "Good
Evening" depending on the time of day.

• Display additional information:


• Current date: Use the date command to print the current date in the format dd-
mm-yyyy.
• Hostname: Use the hostname command to display the system's hostname.
• Current directory: Use the pwd command to display the current directory.
• End the program.
OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

hour=$(date +%H)
if [ $hour -ge 0 -a $hour -lt 12 ]
then
message="$USER, Good Morning."
elif [ $hour -ge 12 -a $hour -lt 18 ]
then
message="$USER, Good Afternoon."
else
message="$USER, Good Evening."
fi
echo $message
echo "Today Date : $(date +%d-%m-%Y)"
echo "Your Hostname is : $(hostname)"
echo "Your Current Location is : $(pwd)"

RESULT
Thus The Program Has Been Executed Successfully.
EX NO : 15
SHELL SCRIPT CASE STATEMENT
AIM
Write a Linux Shell Script to develop a basic math calculator using case statement.
ALGORITHM
Start the program.

• Clear the terminal screen for clean output.


• Initialize the choice variable to 1 (to start the loop).
• Enter a while loop that continues as long as the choice is equal to 1 (i.e., the user
chooses to continue).

• Prompt the user to enter the first value (f1).


• Prompt the user to enter the second value (f2).
• Display a menu with the following options for mathematical operations:
• 1. Addition
• 2. Subtraction
• 3. Multiplication
• 4. Division
• Prompt the user to choose an operation by entering a number (n).
• Process the chosen operation using a case statement:
• If the user selects 1 (Addition):
• Perform the addition operation: f3 = f1 + f2.
• Display the result: The result is: f3.
• If the user selects 2 (Subtraction):
• Perform the subtraction operation: f4 = f1 - f2.
• Display the result: The result is: f4.
• If the user selects 3 (Multiplication):
• Perform the multiplication operation: f5 = f1 * f2.
• Display the result: The result is: f5.
• If the user selects 4 (Division):
• Perform the division operation: f6 = f1 / f2.
• Display the result: The result is: f6.store it in the choice variable.
OUTPUT
• If choice is 1, the loop repeats (asking for new numbers and performing another
operation).
• If choice is 0, the loop ends and the program exits.
• End the program.

PROGRAM CODE

#!/usr/bin/bash
clear
choice=1
while [ $choice -eq 1 ]
do
echo "Enter the First Value "
read f1
echo "Enter the Second Value :"
read f2
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "Enter your choice : "
read n
case "$n" in
1)
echo "Addition"
f3=$((f1+f2))
echo "The result is: $f3"
;;
2)
echo "Subtraction"
f4=$((f1 - $f2))
echo "The result is: $f4"
;;
3)
echo "Multiplication"
f5=$((f1 * $f2))
echo "The result is: $f5"
;;
4)
echo "Division"
f6=$((f1 / $f2))
echo "The result is: $f6"
;;
esac
echo "Do you want to continue [yes-1 / no-0]: "
read choice
done

RESULT
Thus The Program Has Been Executed Successfully.
EX NO : 16
MARK STATEMENT FOR STUDENT

AIM
Write a Linux Shell Script to generate marksheet of a student. Take 5 subjects,
calculate and display total marks, percentage and Class obtained by the student.
ALGORITHM

Start the Program

• Clear the terminal screen for clean output.


• Prompt for Student Information:
• Ask the user to input the following details:
• Student Name
• Roll Number
• Tamil Marks
• English Marks
• Maths Marks
• Science Marks
• Social Science Marks

• Calculate Total and Average:


• Compute the total marks by adding the individual marks:
total = m1 + m2 + m3 + m4 + m5
• Compute the average marks by dividing the total by 5:
avg = total / 5

• Display the Student Information:


• Print the following details:
• Student Name
• Roll Number
• Marks for each subject (Tamil, English, Maths, Science, Social Science)
• Total Marks
• Average Marks

• Check for Pass or Fail:


• If the marks in all subjects are greater than or equal to 35, then
OUTPUT
PROGRAM CODE
!/usr/bin/bash
clear
echo "Enter Student Name "
read name
echo "Enter Roll Number "
read rno
echo "Enter Tamil Marks "
read m1
echo "Enter English Marks "
read m2
echo "Enter Maths Marks "
read m3
echo "Enter Science Marks "
read m4
echo "Enter Social Science Marks "
read m5

total=$(expr $m1 + $m2 + $m3 + $m4 + $m5 )


avg=$(expr $total / 5)

echo "Student Name : $name"


echo "Roll Number: $rno"
echo "Tamil : $m1"
echo "English : $m2"
echo "Maths : $m3"
echo "Science : $m4"
echo "Social Science : $m5"
echo "Total : $total"
echo "Average is : $avg"

if [ $m1 -ge 35 ] && [ $m2 -ge 35 ] && [ $m3 -ge 35 ] && [ $m4 -ge 35 ] && [ $m5 -
ge 35 ]
then
echo "Result : PASS"
if [ $avg -ge 80 ] && [ $avg -le 100 ]
then
echo "Result is: Distinction"
elif [ $avg -ge 61 ] && [ $avg -le 79 ]
then
echo "Result is: First class"
elif [ $avg -ge 35 ] && [ $avg -le 60 ]
then
echo "Result is: Second class"
fi
else
echo "Result : FAIL"
fi

RESULT
Thus The Program Has Been Executed Successfully.
EX NO : 17
GIVEN NUMBER PALINDROME OR NOT

AIM
Write a Linux Shell Script to check whether a given number is Palindrome or NOT.

ALGORITHM
Start the program.
• Clear the terminal screen for clean output.
• Prompt the user to enter a number to check if it is a palindrome.
• Store the original number in a variable source (to compare later).
• Initialize a variable reverse to 0. This variable will store the reversed number.
• Reverse the number:
• While the number is not equal to 0:
• Calculate the remainder of the number when divided by 10 (this gives the
last digit of the number).
• Add the remainder to the reverse variable (shift the previous digits left and
append the new digit).
• Remove the last digit from the number by performing integer division by 10.
• After reversing the number, compare the original number (source) with the reversed
number (reverse):
• If the original number is equal to the reversed number, it is a palindrome.
• If they are not equal, the number is not a palindrome.
• Display the result:
• If the number is a palindrome, print "<number> is a Palindrome".
• Otherwise, print "<number> is NOT a Palindrome".
• End the program.
OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "Enter a number to check whether the given number is Palindrome or NOT: "
read number

reverse=0
source=$number

while [ $number -ne 0 ]


do
remainder=$(( $number % 10 ))
reverse=$(( $reverse * 10 + $remainder ))
number=$(( $number / 10 ))
done

if [ $source -eq $reverse ]


then
echo "$source is a Palindrome"
else
echo "$source is NOT a Palindrome"
fi

RESULT
Thus The Program Has Been Executed Successfully.
EX NO : 18
ARMSTRONG NUMBER

AIM

To Write a Program Armstrong number using while loop.

ALGORITHM

Start the program.

• Clear the terminal screen for clean output.


• Prompt the user to enter a number to check whether it is an Armstrong number
or not.
• Store the entered number in a variable a (to compare later).
• Initialize a variable s to 0. This variable will store the sum of the cubes of the
digits of the number.
• Extract digits of the number and calculate the sum of cubes:
• Use a while loop to repeatedly extract the last digit of the number.
• Calculate the remainder of the number when divided by 10 to get the last
digit.
• Cube the digit and add it to the sum (s).
• Remove the last digit by dividing the number by 10.

• After the loop, check if the original number (a) is equal to the sum of cubes (s):
• If they are equal, print that the number is an Armstrong number.
• Otherwise, print that the number is not an Armstrong number.

• End the program.


OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo -n "Enter any number to check Armstrong or Not : "


read n

a=$n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + \( $r \* $r \* $r \)` n=`expr $n / 10`
done
if [ $a -eq $s ]
then
echo "$n is a Armstrong Number."
else
echo "$n is NOT a Armstrong Number"
fi

RESULT
Thus The Program Has Been Executed Successfully.
EX NO : 19
MULTIPLICATION OF GIVEN NUMBER

AIM

To Write a Linux Shell Script to display multiplication table of given number.

ALGORITHM

Start the program.


• Clear the terminal screen for clean output.
• Display the heading for the multiplication table.
• Prompt the user to enter the table number (the number for which the
multiplication table will be generated).
• Prompt the user to enter how many rows they want in the multiplication table.
• Initialize a variable i to 1. This variable will be used to loop through each row of
the table.
• Generate the multiplication table:
• Use a while loop to repeat the process until i reaches the value of rows.
• In each iteration, calculate the product of the current row number (i) and the
table number (table_no).
• Display the multiplication result: "i x table_no = result".
• Increment i by 1 to move to the next row.
• End the program.
OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "-----------------------------------"
echo "\tMultiplication Table"
echo "-----------------------------------"
echo "Enter table number : "
read table_no
echo "Enter how many rows : "
read rows
i=1
while [ $i -le $rows ]
do
k=$(expr $i \* $table_no)
echo "$i x $table_no = $k"
i=$(expr $i + 1)
done

RESULT
Thus The Program Has Been Executed Successfully.
EX NO: 20

SHELL SCRIPT MENU & ECECUTE THE GIVEN TASK

AIM
Write a menu driven Linux Shell Script which will print the following menu and execute
the given task.

ALGORITHM

Start the program.

• Clear the terminal screen for clean output.


• Display the Main Menu:
• Show a list of available options:
• Option 1: Display the calendar of the current month.
• Option 2: Display today’s date and time.
• Option 3: Display usernames of users currently logged in to the system.
• Option 4: Display your system's hostname.
• Option 5: Display the system's IP address.

• Prompt the user to enter a choice (a number between 1 to 5).


• Process the user's input:
• Based on the user's input ($i), use a case statement to execute the corresponding
command:
• If the user chooses 1, display the calendar for the current month using cal.
• If the user chooses 2, display the current date and time using date.
• If the user chooses 3, display the current logged-in username using whoami.
• If the user chooses 4, display the system's hostname using hostname.
• If the user chooses 5, display the system's IP address using ip -br a.
• Handle invalid input:
• If the user enters anything other than 1-5, display an error message: "Enter valid
input".

• End the program.


OUTPUT
PROGRAM CODE

#!/usr/bin/bash
clear

echo "MAIN MENU"


echo "========="
echo "1. Display calendar of current month"
echo "2. Display today’s date and time"
echo "3. Display usernames those are currently logged in the system"
echo "4. Display your hostname"
echo "5. Display the IP Address "
echo "Please Enter your choice : "
read i
case $i in
1) echo "Calendar is: "
cal ;;
2) echo "Today Date is : "
date ;;
3) echo "Username is "
whoami ;;
4) echo "Hostname is : "
hostname ;;
5) echo "IP Address is : "
ip -br a ;;
*)
echo “Enter valid input” ;;
esac

RESULT
Thus The Program Has Been Executed Successfully.

You might also like