You are on page 1of 41

Linux

Practical File

Integrated Institute of
Technology
Dwarka Sec – 9, New Delhi – 110077

Name : Prashant Kumar


Enrolment Number : 01350102019
Subject : Linux
Course : BCA – 6th sem
Submitted To : Miss. Pooja Malhotra
INDEX
S No. Aim Date Sign.
1 Introduction to Linux Based System
2 Write a linux command to display the list of users who are currently
using linux server
3 Write a linux command to display your system details
4 Write the linux command to perform calculations
5 Write the linux command to show the current working directory
6 Write the linux command to get help with various options
7 Write the linux command to display what all users are currently doing
8 Write the linux Command to copy a file to some other Iocation
9 Write the linux command to move a file to some different location
10 Write the linux command to compare the contents of two files
11 Write the linux command to show the difference between the contents
of two files
12 Write the linux command to show the common data of two file
13 Write the linux command to view the exiting aliases
14 Witte the linux command to display the hostname of the system and
OS
15 Write the linux command to search for specific pattern in a file
16 Write the linux command to show the use of basic regular expressions
using grep Command
17 Write the linux command to copy specified pattern of one file in other
file using sed command
18 Write the command to show the use of Basic Regular Expressions
using sed Command
19 Write the linux command to show the use of awk Command
20 Write a shell script to find the largest of two numbers
21 Write a shell script to check whether a number is even or odd
22 Write a shell script to find largest of three given number
23 Write a shell script to greet the user according to the day of the week
24 Write a shell script to display the number of vowels and consonants in
a string
25 Write a shell script to perform arithmetic operation using case--
-esac
26 Write a shell script to count words, lines and characters in a file
27 Write a shell script to check whether the year entered is leap or not
28 Win a shell script to find factorial of a number
29 Write a shell script to generate a multiplication table
30 Write a shell script to add the digits of a number
31 Write a shell script to reverse string
32 Write a shell script to check whether a number entered is prime or not
33 Write a shell script to accept a filename as command line argument and
print its contents

Aim 1 :
Introduction To Linux Based System

Linux is one of popular version of UNIX operating System. It is open


source as its source code is freely available. It is free to use. Linux was
designed considering UNIX compatibility. Its functionality list is quite
similar to that of UNIX.

Components of Linux System

Linux Operating System has primarily three components


• Kernel − Kernel is the core part of Linux. It is responsible for all major
activities of this operating system. It consists of various modules and
it interacts directly with the underlying hardware. Kernel provides the
required abstraction to hide low level hardware details to system or
application programs.
• System Library − System libraries are special functions or programs
using which application programs or system utilities accesses
Kernel's features. These libraries implement most of the
functionalities of the operating system and do not requires kernel
module's code access rights. • System Utility − System Utility
programs are responsible to do specialized, individual level tasks.
Kernel Mode vs User Mode

Kernel component code executes in a special privileged mode called


kernel mode with full access to all resources of the computer. This code
represents a single process, executes in single address space and do not
require any context switch and hence is very efficient and fast. Kernel runs
each processes and provides system services to processes, provides
protected access to hardware to processes.
Support code which is not required to run in kernel mode is in System
Library. User programs and other system programs works in User Mode
which has no access to system hardware and kernel code. User
programs/ utilities use System libraries to access Kernel functions to get
system's low level tasks.
Let us study them in detail:
There are two modes of operation in the operating system to make sure it
works correctly. These are user mode and kernel mode.
They are explained as follows −
User Mode
The system is in user mode when the operating system is running a user
application such as handling a text editor. The transition from user mode
to kernel mode occurs when the application requests the help of operating
system or an interrupt or a system call occurs.
The mode bit is set to 1 in the user mode. It is changed from 1 to 0 when
switching from user mode to kernel mode.
Kernel Mode
The system starts in kernel mode when it boots and after the operating
system is loaded, it executes applications in user mode. There are some
privileged instructions that can only be executed in kernel mode.
These are interrupt instructions, input output management etc. If the
privileged instructions are executed in user mode, it is illegal and a trap is
generated.
The mode bit is set to 0 in the kernel mode. It is changed from 0 to 1 when
switching from kernel mode to user mode.
An image that illustrates the transition from user mode to kernel mode and
back again is −

In the above image, the user process executes in the user mode until it
gets a system call. Then a system trap is generated and the mode bit is
set to zero. The system call gets executed in kernel mode. After the
execution is completed, again a system trap is generated and the mode
bit is set to 1. The system control returns to kernel mode and the process
execution continues.
Necessity of Dual Mode (User Mode and Kernel Mode) in Operating
System The lack of a dual mode i.e user mode and kernel mode in an
operating system can cause serious problems. Some of these are −

• A running user program can accidentally wipe out the operating


system by overwriting it with user data.
• Multiple processes can write in the same system at the same time,
with disastrous results.
These problems could have occurred in the MS-DOS operating system
which had no mode bit and so no dual mode.

Basic Features

Following are some of the important features of Linux Operating System.


• Portable − Portability means software can works on different types of
hardware in same way. Linux kernel and application programs supports
their installation on any kind of hardware platform.
• Open Source − Linux source code is freely available and it is
community based development project. Multiple teams work in
collaboration to enhance the capability of Linux operating system
and it is continuously evolving.
• Multi-User − Linux is a multiuser system means multiple users can
access system resources like memory/ ram/ application programs
at same time. • Multiprogramming − Linux is a multiprogramming
system means multiple applications can run at same time.
• Hierarchical File System − Linux provides a standard file structure
in which system files/ user files are arranged.
• Shell − Linux provides a special interpreter program which can be
used to execute commands of the operating system. It can be used
to do various types of operations, call application programs. etc.
• Security − Linux provides user security using authentication
features like password protection/ controlled access to specific files/
encryption of data.

Architecture

The following illustration shows the architecture of a Linux system –


The architecture of a Linux System consists of the following layers − •
Hardware layer − Hardware consists of all peripheral devices (RAM/
HDD/ CPU etc).
• Kernel − It is the core component of Operating System, interacts
directly with hardware, provides low level services to upper layer
components.
• Shell − An interface to kernel, hiding complexity of kernel's functions
from users. The shell takes commands from the user and executes
kernel's functions.
• Utilities − Utility programs that provide the user most of the
functionalities of an operating systems.
Aim 2 :
Write a linux command to display the list of users who are
currently using linux server

$who
Aim 3:
Write a linux command to display your system details

$lscpu
Aim 4:
Write the linux command to perform calculations

$bc
Aim 5:
Write the linux command to show the current working directory

$pwd
Aim 6:
Write the linux command to get help with various options

$help
$man {command-
name} eg. $man ls
Aim 7:
Write the linux command to display what all users are currently
doing

$w
Aim 8:
Write the linux Command to copy a file to some other Iocation

$cp {filename} {directoryname}


eg. $cp red.txt .. {.. means
previous dir}
Aim 9:

Write the linux command to move a file to some different


location

$mv {filename}
{directoryname} eg. $mv
redYellow.txt darkcolor

Aim 10:
Write the linux command to compare the contents of two files

$cmp {firstfilename}
{secondfilename} eg. $cmp s1 s2

Aim 11:
Write the linux command to show the difference between the
contents of two files

$diff {filename1}
{filename2} eg. $diff s1 s2
Aim 12:
Write the linux command to show the common data of two file

$comm -12 <(sort {Filename1}) <(sort


{Filename2}) eg. $comm -12 <(sort s1) <(sort
s2)
Aim 13:
Write the linux command to view the existing aliases

$alias
Aim 14:
Write the linux command to display the hostname of the
system and OS

$hostname
Aim 15:
Write the linux command to search for specific pattern in a file

$grep “pattern”
{filename} eg. $grep
“am” s1
Aim 16:
Write the linux command to show the use of basic regular
expressions using grep Command

$grep “regular expressions”


{filename} eg. $grep “….ed” s2
Aim 17:
Write the linux command to copy specified pattern of one file
in other file using sed command

$sed -n {head},{tail}p {filename} >


{secondfilename}
$sed -n 1,2p s1 > s3
Aim 18:
Write the command to show the use of Basic Regular
Expressions using sed Command

$sed -n ‘regular expressions’


{filename} eg. $sed -n ‘regular
expressions’ {filename}
Aim 19:
Write the linux command to show the use of awk Command

$awk ‘{print $1,$4}’ test-file.txt


Aim 20:
Write a shell script to find the largest of two numbers.

#!/bin/bash
echo "a shell script to find out the large between two number."
echo -n "First number:"
read fst_num
echo -n "Second number:"
read snd_num
if test $fst_num -gt $snd_num
then
echo $fst_num is greater than $snd_num.
else
echo $snd_num is greater than $fst_num.
fi

Output :
Aim 21:
Write a shell script to check whether a number is even or odd

#!/bin/bash
echo " Enter a number to check whether it is a even or odd : "
read num
if [[ $((num%2)) == 0 ]]
then
echo " $num is an even number "
else
echo " $num is a odd number "
fi

Output :
Aim 22:
Write a shell script to find largest of three given number

#!/bin/bash
echo "Greatest of three : "
echo "Enter three numbers : "
read num1
read num2
read num3
if [[ $num1 -eq $num2 || $num1 -gt $num2 ]]
then
if [ $num1 -lt $num3 ]
then
max=$num3
else
max=$num1
fi
elif [ $num2 -gt $num1 ]
then
if [ $num2 -lt $num3 ]
then
max=$num3
else
max=$num2
fi
fi
echo " max : $max"
echo $'\n'

Output :
Aim 23:

Write a shell script to greet the user according to the day of the
week

#!/bin/bash
day=$(date '+%Y-%m-%d')
echo "Have a nice $day, sir"

Output :
Aim 24:
Write a shell script to display the number of vowels and
consonants in a string

#!/bin/bash
echo "Enter a string : "
read string
vowelCount=$(echo $string | grep -o -i "[aeiou]" | wc --lines)
consonantCount=$(echo $string | grep -o -i "[bcdfghjklmnpqrstvwxyz]" | wc --
lines)
echo "Total number of vowel are : $vowelCount"
echo "Total number of consonants are : $consonantCount"

Output :
Aim 25:
Write a shell script to perform arithmetic operation using case-
--esac

# !/bin/bash

# Take user Input


echo "Enter Two numbers : "
read a
read b

# Input type of operation


echo "Enter Choice :"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read ch

# Switch Case to perform


# calculator operations
case $ch in
1)res=`echo $a + $b | bc`
;;
2)res=`echo $a - $b | bc`
;;
3)res=`echo $a \* $b | bc`
;;
4)res=`echo "scale=2; $a / $b" | bc`
;;
esac
echo "Result : $res"

Output :
Aim 26:
Write a shell script to count words, lines and characters in a
file

#!/bin/bash
echo "Enter the file name to count words, line and character: "
read file
lines=$(wc --l < $file)
words=$(wc --w < $file)
chars=$(wc --c < $file)
echo "lines = $lines"
echo "words = $words"
echo "chars = $chars"

Output :
Aim 27:
Write a shell script to check whether the year entered is leap
or not

#!/bin/bash
echo "Enter the year : "
read year
if [ $((year%4)) -eq 0 ]
then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi

Output :
Aim 28:
Write a shell script to find factorial of a number

#!/bin/bash
echo "Enter a Number : "
read num
factorial=1
while (($num >= 1))
do
factorial=$((factorial*num))
num=$((num-1))
done
echo " factorial of the number is : $factorial"

Output :
Aim 29:
Write a shell script to generate a multiplication table

#!/bin/bash
echo "Table of any number"
read -p "Enter a number : " num
echo
for i in {1..10..1}
do
table=$((num*i))
echo " $num x $i = $table "
done

Output :
Aim 30:
Write a shell script to add the digits of a number

#!/bin/bash
read -p"Enter a number : " number
sum=0
dup=number
while [[ $dup != 0 ]]
do
rem=$((dup%10))
sum=$((sum+rem))
dup=$((dup/10))
done
if [ $number -gt 0 ]
then
echo "Sum of all the digits of $number : $sum "
else
echo "Sum of all the digits of $number : $((-sum))"
fi

Output :
Aim 31 :
Write a shell script to reverse string

#!/bin/bash
read -p"Eneter the String : " string
echo "Reverse of the string : "
RED='\033[0;31m'
echo -e "${RED} $(rev <<< $string)"

Output :
Aim 32 :
Write a shell script to check whether a number entered is
prime or not

#!/bin/bash
echo " Enter a number to check whether it is prime or composite "
read num
counter=0
i=2
div=$((num/2))
while [ $i -le $div ]
do
if [ $((num%i)) == 0 ]
then
counter=1
break
fi
i=$((i+1))
done
if [ $counter == 0 ]
then
echo " $num is a prime number "
else
echo " $num is a composite number "
fi

Output :
Aim 33 :
Write a shell script to accept a filename as command line
argument and print its contents

#!/bin/bash
echo "Enter file name : "
read file
cat < $file

Output :
The End

You might also like