Lab Manuals Final Suru
Lab Manuals Final Suru
AIM
To Write a Linux Shell Script that accepts a username exists on this system.
ALGORITHM
!/usr/bin/bash
clear
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
#!/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
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.
• Extract the specified range of lines (from start_line to end_line) from the
filename using the sed command:
• 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
RESULT
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
#!/usr/bin/bash
#clear
RESULT
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
#!/usr/bin/bash
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
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
#!/usr/bin/bash
RESULT
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
#!/usr/bin/bash
clear
echo "The number of times the word ['$word'] occured in the file."
grep -o $word $filename|wc -l
RESULT
AIM
ALGORITHM
# !/bin/bash
# clear
if [ -d $dirname ]
then
echo "List of files in the Directory: "
ls -l $dirname | egrep "^d"
else
echo "Enter Proper Directory name"
fi
RESULT
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
#!/usr/bin/bash
if [ ! -f $filename ]
then
echo "Filename $filename does not exists"
exit 1
fi
RESULT
AIM
ALGORITHM
• 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.
#!/usr/bin/bash
clear
strlen=${#string}
RESULT
AIM
ALGORITHM
• 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.
# !/bin/bash
clear
number1=$number
fact=1
RESULT
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
#!/usr/bin/bash
clear
RESULT
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
#!/usr/bin/bash
clear
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
AIM
To Write a Linux Shell Script to say Good Morning / Afternoon / Evening as you log in
to system
ALGORITHM
#!/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.
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
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
RESULT
Thus The Program Has Been Executed Successfully.
EX NO : 18
ARMSTRONG NUMBER
AIM
ALGORITHM
• 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.
#!/usr/bin/bash
clear
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
ALGORITHM
#!/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
AIM
Write a menu driven Linux Shell Script which will print the following menu and execute
the given task.
ALGORITHM
#!/usr/bin/bash
clear
RESULT
Thus The Program Has Been Executed Successfully.