Unix Lab Programs
Lab – 5
1. Take input of 3 numbers from the user and find the largest and smallest
among 3.
echo "enter 3 number to find largest and smallest of three numbers "
read a b c
echo "largest number "
if [ $a -gt $b ] && [ $a -gt $c ];
then
echo "The largest number is $a"
elif [ $b -gt $a ] && [ $b -gt $c ];
then
echo "the largest number is $b"
else
echo "the largest number is $c"
fi
echo "Smallest number "
if [ $a -lt $b ] && [ $a -lt $c ];
then
echo "The smallest number is $a"
elif [ $b -lt $a ] && [ $b -lt $c ];
then
echo "the smallest number is $b"
else
echo "the smallest number is $c"
fi
2. Read a string and check if it is empty. Display msg appropriatly.
echo "Enter a string "
read s1
if [ -z "$s1" ];
then
echo "the string is empty "
else
echo "the string is not empty"
fi
str_len=${#s1}
echo "the length of the string is $str_len"
3. Read a filename as a input from command line and check the type of
filename it is
if [ -e "$1" ];
then
echo "the file $1 exists"
if [ -f "$1" ];
then
echo "the file is a regular file"
elif [ -d "$1" ];
then
echo "the file is a directory"
elif [ -p "$s1" ];
then
echo "the file is a named pipe "
elif [ -L "$1" ];
then
echo "the file is symbolic link"
elif [ -s "$1" ];
then
echo " the file is a socket"
else
echo "unknown file type"
fi
else
echo "the file $1 does not exist"
fi
LEARN WHAT IS FOR DEVICE FILE
4. Find the simple interest.
echo "enter the principal value :"
read p
echo "enter the rate of interest "
read r
echo "Enter the time period"
read t
s=`expr $p \* $t \* $r / 100`
echo "simple interest is : "
echo $s
Lab – 6
1. Read a directory name[absolute path name] and then display the
properties of regular files only.
echo “Enter the directory name (absolute path name) : “
read dir1
if test -d $dir1
then
echo “Directory is present “
ls –l $dir1 > newfile
grep ‘^-’ newfile
else
echo “directory does not exist”
fi
2. Read a file name from the command line as absolute path name as if it
has execute permission for all 3 types of users
echo “Enter a file name as a absolute path name “
read f1
if test –f $f1
then
grep ‘^…x..x..x’ $f1
echo “File has execute permission for all 3 users”
else
echo “No such file”
fi
3. In a student information file display all the students of the mca
program.
if [ $# -eq 0 ]
then
echo “No file name provided”
else
if [ -e “$1” ]
then
echo “Students of MCA program “
grep –n “mca” $1
else
echo “ No such file”
fi
fi
4. Display the students whose name starts with a or b.
if [ $# -eq 0 ]
then
echo “No file name provided”
else
if [ -e “$1” ]
then
stud=$(grep –iE “(^a|^b)” $1
echo “ List of students whose name starts with a or b”
echo “$stud”
else
echo “ No such file”
fi
fi
5. Display the students whose last name is either Gupta or kumar.
if [ $# -eq 0 ]
then
echo “No file name provided”
else
if [ -e “$1” ]
then
stud=$(grep –iE “(Kumar|gupta)” $1
echo “ List of students whose last name is Gupta or Kumar ”
echo “$stud”
else
echo “ No such file”
fi
fi
6. Display the students who are born in the year 2000 or afterwards.
echo “Enter the file name “
read file1
if test –f “$file1”
then
echo “ list of students who are born in the year 2000 or afterwards”
grep -E ‘(200[0-9]|20[1-9][0-9])’ $file1
else
echo “No such file”
fi
Lab – 7
1. Write a shell script to read a file name from command line and delete all
the blank lines in the file display all the contents of the file.
if [ $# -eq 0 ]
then
echo “No file provided “
else
if [ -f “$1” ]
then
sed -i ‘/^$/d’ “$1”
echo “Content of file after deleting all blank lines “
cat $1
else
echo “No such file “
fi
fi
2. Write a shell script to read a file name search pattern, replace pattern
from command line and execute accordingly.
echo “Enter a file name”
read file1
echo “Enter the search string”
read s
echo “Enter the replace string”
read r
sed -i “s/$s/$r/” $file1
echo “Content of new file after replacing the string”
cat $file1
LAB -08
1. Create a file of employee in the following format use awk programming to
display contents of input file
employees(cat > employees)
Name|Age|Gender|Salary|Department|Designation|BirthYear
John|32|Male|150000|Marketing|Manager|1989
Emily|28|Female|100000|Accounts|Accountant|1993
David|45|Male|250000|Marketing|Senior Manager|1978
Susan|37|Female|180000|Accounts|Manager|1986
Michael|53|Male|220000|Marketing|Director|1968
[Link] employee details who work for marketing and accounts department
awk -F"|" '$5=="Marketing" || $5=="Accounts" {print} ' employees
[Link] employee who earn between 1lakh and 2 lakh
awk -F"|" '$4>=100000 && $4<=200000 {print}' employees
c. Display employee who are born in 20th century
awk -F"|" '$7>=1900 && $7<=1999 {print}' employees
[Link] the names, department, designation of employee who earn more
than 2 lakh
awk -F"|" '$4>200000 {print $1, $5, $6}' employees
e. Display the odd line numbers and complete details of employees
awk 'NR%2==1' employees
[Link] total number of employees working for the company
awk 'END {print NR}' employees
[Link] a awk program to read cie marks and see marks of a student in
particular course and find total marks and grade he or she has got:-
Inputfile:-(cat > Inputfile)
1 CS101 40 60
2 CS101 50 70
3 CS101 45 65
4 CS102 35 55
5 CS102 55 75
6 CS102 60 80
[Link]:-
BEGIN {
course_code = "CS101"
student_id = 2
total_marks = 0
grade = ""
}
$2 == course_code && $1 == student_id {
total_marks = $3 + $4
if (total_marks >= 90) {
grade = "A+"
} else if (total_marks >= 80) {
grade = "A"
} else if (total_marks >= 70) {
grade = "B"
} else if (total_marks >= 60) {
grade = "C"
} else if (total_marks >= 50) {
grade = "D"
} else if (total_marks >= 40) {
grade = "E"
} else {
grade = "F"
# print the results
END {
printf "Student ID: %d\n", student_id
printf "Course Code: %s\n", course_code
printf "CIE Marks: %d\n", $3
printf "SEE Marks: %d\n", $4
printf "Total Marks: %d\n", total_marks
printf "Grade: %s\n", grade
Terminal:-
awk -f [Link] inputfile
3. write a shell script that folds long lines into 40 columns any line that
exceeds 40 characters should be broken after 40th column, a* has to be
appended at the end as an indication of folding and remaining characters
should be continued in the next line, the input should be text file:-
fold_lines.sh:-
#!/bin/bash
# Check if input file is provided
if [ -z "$1" ]
then
echo "Error: Please provide an input file."
exit 1
fi
# Read input file line by line and fold long lines
while read -r line
do
# Check if line is longer than 40 characters
if [ ${line} -gt 40 ] ->${#line}
then
# Fold line into 40 columns and append "*a" at the end
echo "${line:0:40}*a"
echo "${line:40}"
else
# Print line as is
echo "$line"
fi
done < "$1"
terminal:-
sh [Link] [Link]
sh fold_lines.sh [Link] > [Link]
LAB -08
1. Write a shell script to calculate the HRA ,DA and the gross salary of an
employee. If the basic is less than 6000 than HRA is the 50% of the basic
salary then DA is 20% of basic salary ,otherwise HRA is 40% and DA is
1000
cat>emp
name|designation|basic salary|
arpitha|accountant|5000|
ajay|manager|25000|
priya|manager|15000|
divya|accountant|1300|
john|manager|35000|
awk -F "|" '{ if ($3 <6000)
hra =0.5 * $3
da=0.2 * $3
else{
hra=0.4 * $3
da= 1000
gross=$3 + hra + da
print $0 , "|",hra,"|",da,"|",gross
}' emp
2. Write awk program to find total number of employee exist for each
designation.
cat >emp1
john|sales|19000
jane|marekting|23000|
bob|sales|12000|
alice|hr|12000
arpitha|marketing|12000|
awk -F "|" '{ count[$2]++} END { for (design in count) print
design,count[design]}' emp
output:
manager 3
accountant 2
designation 1