Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

ACROPOLIS INSTITUTE OF TECHNOLOGY

& RESEARCH, INDORE

LINUX (CS-505) PRACTICAL FILE


COMP. ENGG. (CO)
SESSION 2020-21

TO- BY-
Proff. Garima Kumrawat Gaurav Shardul
0827CO191017
3rd Year- 5th Sem

Course Learning Objectives (CLO)


The Learning Objectives of Linux are such that the student will understand and learn the
following things.

CLO1: To learn about the Basic Linux Concepts

CLO2: Students will become familiar with the Linux commands

CLO3: To learn Shell programming

CLO4: To learn about kernel

CLO5: Execute programs written in C under UNIX environment

Course Outcomes (CO)

At the end of the course, student would be able to

CO1: Understand the system calls

CO2: Compare between ANSI C AND C++ AND POSIX standards

CO3: To learn basic concepts of memory & scheduling

CO4: Mapping the relationship between UNIX Kernel support for files

CO5: Understand Kernel support for process creation and termination and memory allocation
Acropolis Institute of Technology and Research, Indore
Department of Computer Science and Information Technology

LAB MANUAL

LIST OF PROGRAMS

S.NO NAME OF EXPERIMENTS


1. To Study basic & User status Unix/Linux Commands.
2. Study & use of commands for performing arithmetic operations with unix/linux.

3. Create a file called wlcc.txt with some lines and display how many lines, words and characters are present
in that file.

4 Append ten more simple lines to the wlcc.txt file created above and split the appended file into 3 parts.
What will be the names of these split files? Display the contents of each of these files. How many lines
will be there on the last file?

5 Execute shell commands through vi editor.

6 Write a Shell Script that accepts a file name, starting and ending line numbers as arguments and
displays all lines between the given line numbers.
7 Write a shell script that deletes all lines containing a specific word in one or more files
supplied as arguments to it.

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

9 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.

10
write a shell script that computes the gross salary of an employee according to the following rules:

i) If basic salary is <1500 then HRA=10% of the basic and DA=90% of the basic

ii) If the basic salary is>=1500 then HRA=500/- and DA=98% of the basic
11
Write an interactive file –handling shell program. Let it offer the user the choice of copying removing,
renaming, or linking files. Once the user has made a choice, have the same program ask the user for the
necessary information, such as the file name ,new name and so on.

12
Write a shell script that accepts any number of arguments and prints them in the reverse order.

13
Write a Shell script to count the number of lines in a file that do not contain vowels.

14 Write a shell script which receives two file names as arguments. It should check whether the two file
contents are same or not. If they are same then second file should be deleted
15 Develop an interactive script that ask for a word and a file name and then tells how
many times that word occurred in the file.

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


i. To extract a sub-string from a given string.

ii. To find the length of a given string.

17 Write a shell script that accepts two integers as its arguments and computes the value of
first number raised to the power of the second number.

18 Write a shell script that takes a command –line argument and reports on whether it is
directory, a file, or something else.

19 Develop an interactive grep script that asks for a word and a file name and then tells
how many lines contain that word.

20 Write a shell script to list all of the directory files in a directory.

EXPERIMENT NO: 1.

AIM: To Study basic & User status Unix/Linux Commands.

COMMANDS: mkdir // for creating Directory / Folder. mkdir {foldername1, foldername2,


foldername3 } //for creating multiple folder at a time. ls //for listing all files and
folder present in the current location. cd //for changing directory. cd .. //for
getting back to the previous folder. pwd //it will print present working directory.
rmdir // to delete an empty directory.
rm –r // to delete a directory containing files.
EXPECTED OUTPUT

Gaurav
EXPERIMENT NO: 2.

AIM: Study & use of commands for performing arithmetic operations with unix/linux.

SOURCE CODE :

touch arithmeticOperation.sh chmod


+x arithmeticOperation.sh
./arithmeticOperation.sh

vi arithmeticOperation.sh

# take two integers from the


user echo "Enter two integers: "
read a b echo "Enter two
integers: " # perform addition
# compute subtraction result #
compute multiplication result
mul =`expr "$a * $b" | bc`

#!/bin/sh

# take two integers from the


user echo "Enter two integers: "
read a b

# perform addition
add=`expr $a + $b`

# compute subtraction result


sub= `expr "$a - $b" | bc`

# compute multiplication result


mul=`expr "$a * $b" | bc`

# compute division result


div=`expr "$a / $b" | bc -l`

# compute modulus result


mod=`expr "$a % $b" | bc`

# show result echo "Result of


Addition : $add" echo "Result of
Subtraction : $sub" echo "Result of
Multiplcation : $mul" echo "Result
of Divsion : $div"
echo "Result of Modulus : $mod

EXPECTED OUTPUT
EXPERIMENT NO: 3.

AIM: Create a file called wlcc.txt with some lines and display how many lines, words and
characters are present in that file.

SOURCE CODE

cat > wlcc.txt

AITR has always been at the forefront of adopting new technologies. CSIT department @
AITR has three courses those include the emerging areas as recommended by the AICTE
committee. As exponential growth in information on various platforms, future engineers
must have the expertise in core computer science as well as latest technologies. It
requires blending of emerging areas with core subject and therefore AICTE introduced as
new branch CSIT.

The courses @ CSIT incorporate both theory and practical / labs in the emerging areas of
AI, ML, IoT, Blockchain and Data Sciences as core subjects and Quantum Computing,
Virtual Reality and Cyber Security as Professional Electives. The above emerging areas
have been identified by the national level committee as part of National Perspective Plan
for Engineering Education and accepted by AICTE. AICTE has also prescribed the
model curriculum for the above emerging areas.

wc wlcc.txt

EXPECTED OUTPUT

EXPERIMENT NO: 4 .

AIM: Append ten more simple lines to the wlcc.txt file created above and split the appended file
into 3 parts. What will be the names of these split files? Display the contents of each of these
files. How many lines will be there on the last file?
SOURCE CODE

cat >> wlcc.txt


The course @ CSIT is in the AICTE nomenclature and accepted by them as equivalent to
CSE and IT. The courses bring the best of both CSE and IT together. The course @ CSIT
will be accepted as being similar / relevant to CSE and IT and will also be accepted for
campus recruitments, not only in private companies but also in government undertakings
etc. The graduates of the courses @ CSIT will be accepted even for any faculty
recruitments for the departments of CSE or IT or CSIT. The graduates will also be
eligible for higher education (both in India and abroad).
The graduates of the new course of CSIT will find employment not only in the regular
services industry but also in the emerging areas mentioned above; those are specialized in
nature and will command much higher salaries.

split -n 3 wlcc.txt ls

wc -l xac.txt

EXPECTED OUTPUT

xaa xab xac //Name of 3 Files


4 //Number of lines in last file

EXPERIMENT NO: 5.

AIM: Execute shell commands through vi editor.

SOURCE CODE
touch arithmeticOperation.sh vi
arithmeticOperation.sh
EXPECTED OUTPUT

EXPERIMENT NO: 6.

AIM: Write a Shell Script that accepts a file name, starting and ending line numbers as
arguments and displays all lines between the given line numbers.

SOURCE CODE
echo "enter the filename"

read fname

echo "enter the starting line number"


read
s
echo "enter the ending line number"
read
n
sed -n $s,$n\p $fname | cat > newline

cat newline

sed -n $s,$n\p $f

EXPECTED OUTPUT

touch lineReader.sh vi
lineReader.sh chmod +x
lineReader.sh
./lineReader.sh

EXPERIMENT NO: 7.

AIM: Write a shell script that deletes all lines containing a specific word in one or more files
supplied as arguments to it.

SOURCE CODE
if [ $# -lt 1 ]
then
echo “ Check the arguments once ”
exit fi
echo “Enter a word”
read word for file
in $* do
grep –iv “$word” $file | tee 1> /dev/null done
echo “ lines containing given word are deleted”

EXPERIMENT NO: 8.

AIM: Write a shell script that displays a list of files in current directory to which the user has
read, write and execute permissions.
SOURCE CODE
# Shell script to display list of file names # having
read, Write and Execute permission echo "The name of
all files having all permissions :"

# loop through all files in current directory


for file in * do

# check if it is a file
if [ -f $file ] then

# check if it has all permissions if [ -


r $file -a -w $file -a -x $file ] then

# print the complete file name with -l option


ls -l $file

# closing second if statement fi

# closing first if statement


fi done

EXPECTED OUTPUT
EXPERIMENT NO: 9.

AIM: 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.

SOURCE CODE

touch upper.sh vi
upper.sh

# get filename
echo -n "Enter File Name : " read
fileName
# make sure file exits for reading if
[ ! -f $fileName ]
then
echo "Filename $fileName does not exists"
exit 1 fi
# convert uppercase to lowercase using tr command tr
'[A-Z]' '[a-z]' < $fileName

chmod +x upper.sh bash upper.sh

EXPECTED OUTPUT

EXPERIMENT NO: 10.

AIM: write a shell script that computes the gross salary of an employee according to the following rules:

i) If basic salary is <1500 then HRA=10% of the basic and DA=90% of the basic

ii) If the basic salary is>=1500 then HRA=500/- and DA=98% of the basic

SOURCE CODE

touch gsalary.sh vi gsalary.sh


echo "enter the basic salary:"
read bsal if [ $bsal -lt 1500 ]
then
gsal=$((bsal+((bsal/100)*10)+(bsal/100)*90))
echo "The gross salary : $gsal" fi
if [ $bsal -ge 1500 ]
then gsal=$(((bsal+500)+(bsal/100)*98))
echo "the gross salary : $gsal" fi

chmod +x gsalary.sh bash


gsalary.sh

EXPECTED OUTPUT

EXPERIMENT NO: 11.

AIM: Write an interactive file –handling shell program. Let it offer the user the choice of copying
removing, renaming, or linking files. Once the user has made a choice, have the same program ask the user
for the necessary information, such as the file name ,new name and so on.

SOURCE CODE

vi menu.sh

while true
do
echo "*******MENU*********"
echo "
1. List of files.
2. Copying files.
3. Removing files.
4. Renaming files.
5. Linking files.
press [CTRL+C] TO
EXIT" echo "enter your
choice " read ch case "$ch"
in
1 ) echo "The list of file names."
ls -l || echo "These are file";; 2)
echo "Enter the old filename."
read ofile
echo "Enter the new file name."
read nfile
cp $ofile $nfile && echo "Copied sucessfully." || echo "Copied is not possible." ;;
3 ) echo "Enter the file name to remove."
read rfile
rm -f $rfile && echo "Successfully removed." ;;
4 ) echo "Enter the old file name." read ofile
echo "Enter the new file name."
read nfile
mv $ofile $nfile && echo "The file $ofile name renamed to $nfile." || echo "You
cann't Rename the file.".;;
5 ) echo "Enter the original filename." read
ofile
echo "Enter the new filename to link a file." read
lfile
ln $ofile $lfile && echo "Creat the linking file Sccessfully." || echo "You
cann't Linking the file.";; * ) echo "Invalid option." Echo " Enter correct
choice." esac
done

chmod +x gsalary.sh
bash gsalary.sh

EXPECTED OUTPUT
EXPERIMENT NO: 12.

AIM: Write a shell script that accepts any number of arguments and prints them in the reverse order.

SOURCE CODE

vi reverse.sh

a=$#
echo "Number of arguments are" $a
x=$* c=$a res=''
while [ 1 -le $c ]
do c=`expr $c -
1` shift $c
res=$res' '$1 set
$x done
echo Arguments in reverse order $res

bash reverse.sh Gaurav Shardul CO17

EXPECTED OUTPUT
Gaurav Shardul CO17

CO17 Shardul Gaurav

EXPERIMENT NO: 13.

AIM: Write a Shell script to count the number of lines in a file that do not contain vowels.

SOURCE CODE

vi linecounter.sh

#!/bin/bash echo
"Enter file name"
read file
awk '$0!~/[aeiou]/{ count++ }
END{print "The number of lines that does not contain vowels are: ",count}' $file

bash linecounter.sh

EXPECTED OUTPUT
EXPERIMENT NO: 14.

AIM: Write a shell script which receives two file names as arguments. It should check whether
the two file contents are same or not. If they are same then second file should be deleted.

SOURCE CODE

vi duplicateremover.sh

# compare the contents of two file , if same delete 2nd one

echo # compare the contents of two file , if same delete 2nd one

echo $0
echo $1

if [ `diff $0 $1 >/dev/null` -eq 0 ] then


echo Same

else
echo Different
rm $1
fi

bash duplicateremover.sh gsalary.sh GrossSalary.sh

EXPECTED OUTPUT

here gsalary.sh is removed

EXPERIMENT NO: 15.

AIM: Develop an interactive script that ask for a word and a file name and then tells how many
times that word occurred in the file.

SOURCE CODE
cat > words.txt
Computer Science is the study of computers and computational systems. Unlike electrical and
computer engineers, computer scientists deal mostly with software and software systems; this
includes their theory, design, development, and application.

Principal areas of study within Computer Science include artificial intelligence, computer
systems and networks, security, database systems, human computer interaction, vision and
graphics, numerical analysis, programming languages, software engineering, bioinformatics and
theory of computing.

vi wordcounter.sh

echo " Enter the word to be searched" read


word
echo "Enter the filename to be used" read
flname
echo "the no. of times the word ['$word'] occured in the file." grep
-o $word $flname|wc -l
echo " the no of line that contains ['$word'] is"
grep -c $word $flname

bash wordcounter.sh

EXPECTED OUTPUT
EXPERIMENT NO: 16.

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


iii. To extract a sub-string from a given string.

iv. To find the length of a given string.

SOURCE CODE

vi stringop.sh bash
stringop.sh

echo " Enter the String" read


string
echo "Enter the position for extraction of String" read
pos
echo "Enter the Length for extraction of String" read
len

echo "Length of String is "${#string}


echo "SubString is "${string:$pos:$len}

EXPECTED OUTPUT

EXPERIMENT NO: 17.

AIM: Write a shell script that accepts two integers as its arguments and computes the value of
first number raised to the power of the second number.

SOURCE CODE

vi power.sh

if [ $# -ne 2 ] then
echo "Invalid number of arguments"
exit fi
pwr=`echo $1^$2 |bc` echo "$1
raised to $2 is : $pwr"
bash power.sh 2 5

EXPECTED OUTPUT

EXPERIMENT NO: 18.

AIM: Write a shell script that takes a command –line argument and reports on whether it is
directory, a file, or something else.

SOURCE CODE

vi filetype.sh

echo "Enter the file name: "


read file if [
-f $file ]
then
echo $file "---> It is a ORDINARY
FILE." elif [ -d $file ] then
echo $file "---> It is a DIRCTORY."
else
echo $file "---> It is something else." fi

bash filetype.sh

mkdir Semester

bash filetype.sh

EXPECTED OUTPUT

EXPERIMENT NO: 19.

AIM: Develop an interactive grep script that asks for a word and a file name and then tells how
many lines contain that word.

SOURCE CODE

vi wordcontainer.sh

echo “Enter a word” read


word echo “Enter the
filename”
read file
nol="$(grep -c computer words.txt)" echo “
$nol times $word present in the $file”
bash wordcontainer.sh

EXPECTED OUTPUT

EXPERIMENT NO: 20.

AIM: Write a shell script to list all of the directory files in a directory.

SOURCE CODE cd Semester touch 1st 2nd 3rd 4rth 5th 6th 7th 8th mkdir
{Attendance,Certificates} vi listdirectory.sh

# !/bin/bash echo "enter


directory name"
read dir
if [ -d $dir ] then
echo "list of files in the directory"
ls –l $dir|egrep ‘^d’ else
echo "enter proper directory name"
fi

bash listdirectory.sh

EXPECTED OUTPUT

You might also like