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

Yuktha-Unix Final Report

The document contains the lab records of a student for a Unix and Shell Programming course. It has details of various labs conducted like file operations, text processing, pattern matching, shell scripting etc. Each lab contains the tasks done, codes and outputs. In total there are 10 labs covered in the record.

Uploaded by

Tejaswini N Teju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views25 pages

Yuktha-Unix Final Report

The document contains the lab records of a student for a Unix and Shell Programming course. It has details of various labs conducted like file operations, text processing, pattern matching, shell scripting etc. Each lab contains the tasks done, codes and outputs. In total there are 10 labs covered in the record.

Uploaded by

Tejaswini N Teju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

B.M.S.

COLLEGE OF ENGINEERING
(Autonomous College under VTU, Approved by AICTE, Accredited by NAAC)
MASTER OF COMPUTER APPLICATIONS
(Accredited by NBA for 5 years 2019 - 2024)

UNIX & SHELL PROGRAMMING


(22MCA1PCUS)

LAB RECORD

SUBMITTED BY

Tejaswini N (1BM23MC103)

UNDER THE GUIDANCE OF

S Shilpa
(Assistant Professor)
B.M.S. COLLEGE OF ENGINEERING
(Autonomous College under VTU, Approved by AICTE, Accredited by NAAC)
MASTER OF COMPUTER APPLICATIONS
(Accredited by NBA for 5 years 2019 - 2024)

LABORATORY CERTIFICATE

This is to certify that Tejaswini N (1BM23MC103) has satisfactorily

completed the course of practical in “Unix & Shell Programming –

22MCA1PCUS” Laboratory prescribed by BMS College of

Engineering (Autonomous college under VTU) 1st Semester MCA

course in this college during the year 2022 - 2023.

Signature of Batch in charge Signature of HOD

S Shilpa Dr. Ch. Ram Mohan Reddy

Examiner:
1.

2.
Table of Contents

Content Pa
ge
N
o.
Lab-1
1. Display the calendar of the year 2023.
2. Display the current date in “dd/mm/yy” format.
3. Display time as hh:mm
4. Display the following with a single echo command:
*********
Unix Lab
*********
5. Display terminal name and login name

Lab-2
1. Display all the properties of regular files in current directory
2. Create two regular files and concatenate both the files.
3. Rename a file and copy the contents of one file to the other file.
4. Create a directory and delete a directory
Lab-3
1. Display octal equivalent for every character along with characters in the file
2. Convert the upper case characters in a file to lower case.
3. Display contents of file after squeezing the multiple blank lines into a single blank
line.
4. Display with message no.of lines,Words and characters in a file.
Lab-4
1. Create two files and display the common lines between those two files.
2. Sort the contents of the file in descending order.
3. Display the first line and last line of the file.
4. Display the lines from 4 to 6 of a file
Lab-5
1. Create a file student with entries for (USN, Name, C1_marks, C2_marks, C3_marks,
C4_marks) in each row.
2. Display for all the students, the USN, C2_marks, C4_marks
3. Join two student files vertically
4. Split the file student into different files containing 4 lines each.
5. Display the students whose name starts with a or b.
6. Display the students whose last name is either Gupta or Kumar.
7. Display the students who are born after the year 2000 or afterwards.
Lab-6 Write Shell scripts for the following:
1. Read 3 numbers from the user and find the largest and smallest.
2. Read a string and check if it is empty. Display message appropriately.
3. Read a filename as input from command line and check the type of the file it is
(regular,
qqqdirectory, device, link).
4. Find the simple interest.
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.
2. Write a shell script to read a file name, search pattern, replace pattern
from command line and execute accordingly.
3. Read a file name from the command line as absolute path name and check if it has
execute permission for all 3 types of users.
4. Read the user choice and compute basic arithmetic operations based on the choice.
Lab-8
1. Create a file of employee in the following format use awk programming to display
contents of the input file.
Emp-id|Name|DOB|Job|Department|Salary
2. Display the employee details who works for either Administration or Sales
department.
3. Display the employee who earn between 1-2 lakhs.
4. Display the employee who are born after 20th century.
5. Display the names of employees and their department whose names start with S or A
6. Find the total no. of employees of the company.
Lab-9:
1. Write a awk script to read cie marks and see marks of a student is a particular course
and rrrrfind out the total marks and grade he or she has got.
2. Write an awk program to find total number of employees exists for each designation.
Lab-10
1. Write an awk script to calculate the HRA, DA and the gross salary of
an employee. If the basic is less than 6000/- then HRA is 50% of the basic, DA is 25%
yyyof the basic otherwise the HRA is 40% and DA is 1000/-
22MCA1PCUS: Unix & Shell Programming

Lab-1

1. Display the calendar of the year 2023.


Code:
$ cal 2023

Output:

2. Display the current date in “dd/mm/yy” format.


Code:
$ date +%d/%m/%y

Output:

Department of Computer Application, BMSCE Page | 6


22MCA1PCUS: Unix & Shell Programming
3. Display time as hh:mm
Code:
$ date +%H:%M

Output:

4. Display the following with a single echo command:


*********
Unix Lab
*********
Code:
$ echo -e "*********\nUnix Lab\n*********"

Output:

5. Display terminal name and login name


Code:
$ tty ; whoami

Output:

Department of Computer Application, BMSCE Page | 7


22MCA1PCUS: Unix & Shell Programming
Lab-2

1. Display all the properties of regular files in current directory


Code:
$ ls -l|grep "^-"

Output:

2. Create two regular files and concatenate both the files.


Code:
$ cat f1.txt f2.txt > f3.txt

Output:

Department of Computer Application, BMSCE Page | 8


22MCA1PCUS: Unix & Shell Programming
3. Rename a file and copy the contents of one file to the other file.
Code:
$ mv f1.txt file1.txt
$ cat file1.txt > file2.txt
Output:

4. Create a directory and delete a directory


Code:
$ mkdir mydir1
$ rmdir mydir1
Output:

Lab-3
1. Display octal equivalent for every character along with characters in
the file
Code:
$ od -bc f2.txt
Output:

Department of Computer Application, BMSCE Page | 9


22MCA1PCUS: Unix & Shell Programming
2. Convert the upper case characters in a file to lower case.
Code:
$ tr 'A-Z' 'a-z' <f2.txt> newf2.txt

Output:

3. Display contents of file after squeezing the multiple blank lines into a single blank line.
Code:
$ tr -s '\n' < f4.txt

Output:

4. Display with message no.of lines,Words and characters in a file.


Code:
echo "Filename: $1"
cmd1=$(wc -l $1)
echo "Lines: $cmd1"
cmd2=`wc -w $1`
echo "Words: $cmd2"
cmd3=$(wc -c $1)
echo "Characters: $cmd3"

Output:

Department of Computer Application, BMSCE Page | 10


22MCA1PCUS: Unix & Shell Programming
Lab-4

1. Create two files and display the common lines between those two files.
Code:
$ grep -Fx -f l4c4 l4c3

Output:

2. Sort the contents of the file in descending order.


Code:
$ sort -r l4c2.txt

Output:

3. Display the first line and last line of the file.


Code:
$ head -n 1 l4c2.txt ; tail -1 l4c2.txt

Output:

Department of Computer Application, BMSCE Page | 11


22MCA1PCUS: Unix & Shell Programming
4. Display the lines from 4 to 6 of a file
Code:
$ sed -n '4,6p' l4c4.txt

Output:

Lab-5

1. Create a file student with entries for (USN, Name, C1_marks, C2_marks,
C3_marks, C4_marks) in each row.
Code:
$ gedit student.txt
USN |Name |C1_Marks |C2_Marks |C3_Marks |C4_Marks
B0101|Julie |60 |70 |75 |80
B0102|Abhi |90 |100 |98 |90
B0103|Bhumi |96 |87 |93 |90
B0104|Bhanu |79 |76 |94 |88
B0105|Arun |80 |95 |98 |90
B0106|Sindhu |60 |50 |56 |77
B0107|Tanu |90 |70 |86 |68
B0108|Kavya |59 |80 |76 |77
B0109|Jenny |89 |70 |89 |88
B0110|Ashwith |78 |85 |99 |96

Output:

Department of Computer Application, BMSCE Page | 12


22MCA1PCUS: Unix & Shell Programming
2. Display for all the students, the USN, C2_marks, C4_marks
Code:
$ cut -d '|' -f 1,4,6 student.txt
Output:

3. Join two student files vertically


Code:
$ cut -d '|' -f 1 student.txt > field1.txt
$ cut -d '|' -f 3,6 student.txt > field2.txt
$ paste -d ':' field1.txt field2.txt
Output:

4. Split the file student into different files containing 4 lines each.
Code:
$ split -l 4 student.txt stud-
Output:

Department of Computer Application, BMSCE Page | 13


22MCA1PCUS: Unix & Shell Programming
5. Display the students whose name starts with a or b.
Code:
$ grep -E '\|(A|B)' student.txt

Output:

6. Display the students whose last name is either Gupta or Kumar.


Code:
$ grep -Ew '(kumar|gupta)' student.txt
#or
$ grep -E ' (kumar|gupta)\|' student.txt

Output:

7. Display the students who are born after the year 2000 or afterwards.
Code:
echo "All the students whose are born in the year 2000 or afterwards:"
grep "[0-9][0-9]/[0-9][0-9]/200[0-9]" stud1.sh

Output:

Department of Computer Application, BMSCE Page | 14


22MCA1PCUS: Unix & Shell Programming
Lab-6 Write Shell scripts for the following:

1. Read 3 numbers from the user and find the largest and smallest.
Code:
echo "Enter 3 numbers seperated by space:"
read a b c
#to find largest number among 3
if [ $a -ge $b ] && [ $a -ge $c ]
then
echo "$a is the largest number"
elif [ $b -ge $a ] && [ $b -ge $c ]
then
echo "$b is the largest number"
else
echo "$c is the largest number"
fi

#to find smallest number among 3


if [ $a -lt $b ] && [ $a -lt $c ]
then
echo "$a is the smallest number"
elif [ $b -lt $a ] && [ $b -lt $c ]
then
echo "$b is the smallest number"
else
echo "$c is the smallest number"
fi
Output:

2. Read a string and check if it is empty. Display message appropriately.


Code:
echo "Enter the string:"
read str
if [ -z $str ];
then
echo "Given string is empty"
else
echo "Given string is: $str"
fi
# -z operator returns true if the string is empty else return false
#-n operator returns true if string is not null else return true

Department of Computer Application, BMSCE Page | 15


22MCA1PCUS: Unix & Shell Programming
Output:

3. Read a filename as input from command line and check the type of the file it is (regular,
directory, device, link).
Code:
#creating a linkfile
#ln -s sourceFile targetFile

# Check if a file name is provided using $#


if [ $# -eq 0 ]; then
echo "Please provide the file name in command line"
exit 1
fi

# Check if the file exists


if [ -e $1 ]; then

# Check if the file is a directory


if [ -d $1 ]; then
echo "$1 is a directory"

# Check if the file is a device file


elif [ -b $1 ]; then
echo "$1 is a block device file"

elif [ -c $1 ]; then
echo "$1 is a character device file"

# Check if the file is a symbolic link


elif [ -h $1 ]; then
echo "$1 is a symbolic link"

# Check if the file is a regular file


elif [ -f $1 ]; then
echo "$1 is a regular file"

else
echo "$1 is of an unknown type"
fi
else
echo "$1 does not exist"
fi

Department of Computer Application, BMSCE Page | 16


22MCA1PCUS: Unix & Shell Programming
Output:

4. Find the simple interest.


Code:
# Read the principle amount, time and rate of interest
echo "Enter the principle amount:"
read pAmt
echo "Enter the time (in years):"
read time
echo "Enter the rate of interest (in percentage):"
read rate

# Calculate the simple interest


interest=$((pAmt * rate * time / 100))

# Print the result


echo "The simple interest is: $interest"

Output:

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.
Code:
# Check if a file name is provided using $#
if [ $# -eq 0 ]; then
echo "Please provide the file name in command line"
exit 1
fi

Department of Computer Application, BMSCE Page | 17


22MCA1PCUS: Unix & Shell Programming

# Check if the file exists


if [ -e $1 ]; then

echo "Before removing blank line:"


cat $1
sed -i '/^$/d' $1
echo "After removing blank line:"
cat $1

else
echo "$1 does not exist"
fi

Output:

2. Write a shell script to read a file name, search pattern, replace pattern
from command line and execute accordingly.
Code:
# Read the file name, search pattern, and replace pattern from the command line
file=$1
search_pattern=$2
replace_pattern=$3

echo "Before replacing $2 with $3"


cat $file

# Use sed to replace the search pattern with the replace pattern in the file

sed -i "s/${search_pattern}/${replace_pattern}/g" $file

#-i used to edit original file itself

echo "After replacing $2 with $3"


cat $file

#example: sed "s/hello/hi/g" file to replace all occurrences of "hello" with "hi"
#Here's what each part does:

Department of Computer Application, BMSCE Page | 18


22MCA1PCUS: Unix & Shell Programming

#s: This is the substitute command.


#/hello/: This is the text to find.
#/hi/: This is the text to replace it with.
#g : This refers global which tells sed to replace all occurrences of "hello" in each line, not just
the first one.

Output:

3. Read a file name from the command line as absolute path name and check if it has execute
permission for all 3 types of users.
Code:
# Check if file name is provided in command line or not using $#
if [ $# -eq 0 ]; then
echo "Please provide the file name in command line"
exit 1
fi

# Read the file name from the command line and name it as file1
file1=$1

# Check if the file exists and is a regular file


if [ -f "$file1" ]; then
# Check if the file has execute permission for owner, group, and others
if [ -x "$file1" ]; then
echo "File $file has execute permission for all three types of users."
else
echo "File $file does not have execute permission for all three types of users."
fi
else
echo "File $file does not exist or is not a regular file."
fi
# use --> chmod u+x file_name [to grant executable permission for user owner]
# use --> chmod u-x file_name [to remove executable permission for user owner]
# similary use g+x and g-x for group user
# o+x and o-x other user
# a+x and a-x for granting and removing permission for all 3 users

Department of Computer Application, BMSCE Page | 19


22MCA1PCUS: Unix & Shell Programming

Output:

4. Read the user choice and compute basic arithmetic operations based on the choice.
Code:
while true; do
echo "Choose an arithmetic operation:"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Exit"

read -p "Enter your choice: " choice

case $choice in
1)
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
result=$((num1 + num2))
echo "Result: $result"
;;
2)
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
result=$((num1 - num2))
echo "Result: $result"
;;
3)
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
result=$((num1 * num2))
echo "Result: $result"
;;
4)
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
result=$(echo "scale=2; $num1 / $num2" | bc)
echo "Result: $result"
;;

Department of Computer Application, BMSCE Page | 20


22MCA1PCUS: Unix & Shell Programming

5)
echo "Exiting..."
exit 0
;;

*)
echo "Invalid choice! Please choose a valid option."
;;
esac
done

Output:

Department of Computer Application, BMSCE Page | 21


22MCA1PCUS: Unix & Shell Programming

Lab-8

1. Create a file of employee in the following format use awk programming to


display contents of the input file.
Emp-id|Name|DOB|Job|Department|Salary
Code:
$ gedit emp.txt

empid|name |designation|department |dob |salary


1001 |Shishira |director |marketing |12-06-2002|150000
1002 |Vanith |manager |production |20-01-2002|200000
1003 |Bhavana |executive |marketing |26-04-2001|16000
1004 |Anushree |director |account |02-05-2002|10000
1005 |Pavan |manager |administration|01-02-2001|15000
1006 |Prasad |dgm |sales |02-03-2002|230000
1007 |Kavan |executive |administration|06-04-2002|20000
1008 |Smitha |dgm | sales |02-07-2002|180000

#to display file


$awk '{print}' emp.txt

Output:

2. Display the employee details who works for either Administration or Sales
department.
Code:
$awk -F'|' '$4 ~ /administration|sales/ {print}' emp.txt

Output:

Department of Computer Application, BMSCE Page | 22


22MCA1PCUS: Unix & Shell Programming

3. Display the employee who earn between 1-2 lakhs.


Code:
echo "Employee who earns from 100000 to 200000"
awk -F '|' '$6 >= 100000 && $6 <= 200000 {print}' emp.txt

Output:

4. Display the employee who are born after 20th century.


Code:
awk -F "|" '$5 ~ /200[1-9]/' emp.txt
Output:

5. Display the names of employees and their department whose names start with S or A
Code:
awk -F "|" '$2 ~ /^[SA]/ {print $2, "|", $4}' emp.txt
Output:

Department of Computer Application, BMSCE Page | 23


22MCA1PCUS: Unix & Shell Programming
6. Find the total no. of employees of the company.
Code:
awk 'END {print NR-1}' emp.txt
Output:

Lab-9:
1. Write a awk script to read cie marks and see marks of a student is a particular course and find
out the total marks and grade he or she has got.
Code:
awk -F "|" '{total = ($3 + $4)/2; print $0, "Total Marks:", total, "Grade:", (total >= 90 ? "A" : total >= 75 ?
"B" : "C")}' student.txt
Output:

2. Write an awk program to find total number of employees exists for jj nn each designation.
Code:
awk -F "|" '{ count[$3]++ } END { for (desig in count) print desig, ":", count[desig], "employees" }'
emp.txt
Output:

Lab-10
1. Write an awk script to calculate the HRA, DA and the gross salary of
an employee. If the basic is less than 6000/- then HRA is 50% of the basic, DA is 25% of the basic
otherwise the HRA is 40% and DA is 1000/-
Code:
awk -F'|' '{if ($6 < 6000) {HRA=$6*0.5; DA=$6*0.25} else {HRA=$6*0.4; DA=1000};
Gross=$6+HRA+DA; print $0, "HRA:", HRA, "DA:", DA, "Gross:", Gross}' emp.txt
Output:

Department of Computer Application, BMSCE Page | 24


22MCA1PCUS: Unix & Shell Programming

Department of Computer Application, BMSCE Page | 25

You might also like