Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linux Lab Manual

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

LAB MANUAL

PROGRAMMING LAB –
LINUX & SHELL PROGRAMMING

Course Code : 43P


Course Title : Programming Lab – Linux & Shell Programming
Course Coordinator : Dr J Loveline Zeema & Mr M Arun
Class : II BSc CS A& B Batch : 2020-2023
Academic Year : 2021-2022 EVEN Semester : 4
Table of Contents

S. No Title

Shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split,
1.
diff.
shell script to show the following system configuration :
a. currently logged user and his log name
b. current shell , home directory , Operating System type , current
2. Path setting , current working directory
c. show currently logged number of users, show all available shells
d. show CPU information like processor type , speed
e. show memory information
Shell Script to implement the following: pipes, Redirection and tee
3.
commands.
Shell script for displaying current date, user name, file listing and directories
4.
by getting user choice

5. Shell script to implement the filter commands.

6. Shell script to remove the files which has file size as zero bytes

7. Shell script to find the sum of the individual digits of a given number.

Shell script to find the greatest among the given set of numbers using
8.
command line arguments.

9. Shell script for palindrome checking

Shell script to print the multiplication table of the given argument using for
10.
loop.
Ex:1
Shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.

AIM:
To stimulate the file commands rm,cp,cat,mv,wc,split,diff.

ALGORITHM:
Step 1: Start the program.
Step 2: Using touch command create file 1 file2, file3 ,file4 and file 5 as textfiles.
Step 3: Using rm command remove file1. txt.
Step 4: Copy the file2 to file3 using cp command.
Step 5: Using cat command display the contents of file 3 and file 4. Step 6:
Using wc command return the number of lines of the file 3.
Step 7: Using diff command compare two file4 and file5

Shell script:-

rm file1.txt
echo "file1 successfully deleted"
echo " "
cp file2.txt file3.txt
echo "file2 successfully copied to file3"echo "
"
echo "content of file3 and file4"cat
file3.txt output file4.txt echo " "
echo "total no.of lines in a file3"wc
file3.txt
echo " "
echo "compare two files file4 and file5"diff
file4.txt file5.txt
echo " "

echo "split the file in terms of line4"echo


" "
split -b 22 file5.txt
note:- to split the file give the command in terminal split - -lines=2 (filename)

Output:

RESULT:

The above program has been executed and verified successfully.


Ex:No:2
Script to show the following system confirmation
A. Currently logged user and his log name
B. Current shell, home directory, os type, current path, current working
directory
C. Show currently logged number of user, show all available shell
D. Show the CPU information like process, type, speed etc.
E. Show the memory information.

AIM:
To script to show the following system configuration.
a) Currently logged user and his log name
b) Current shell, home directory, os type, current path, current workingdirectory.
c) show currently logged number of user, show all available shell
d)Show the CPU information like process, type, speed etc.
e) Show the memory information.

ALGORITHM:

Step 1:Start the process


Step 2:Use $(uname) to displaythe current shell
Step 3:Use $(uname) to displaycurrent user
Step 4:Use $ HOME to printhome directory
Step 5:Use $(uname) to print OStype
Step 6:Use $ PATH to displaycurrent path setting
Step 7:Use $(pwd) to display current working directory
Step 8:Use $(who) to display current login users andshells
Step 9:Use $(free) to show memory and informationStep 10:Concatenate /proc/
cprintfo to display CPUinformation.
Step 11: Stop the program.

Shell script:-
echo "current login user:"$(whoami) //echo "
"
echo "to print current shell:"$(uname) echo
" "
echo "to print home directory:"$HOMEecho "
"
echo "to print os type:"$(uname) echo "
"
echo "current path setting:"$PATH

echo " "


echo "current working directory:"$(pwd) echo "
"
echo "current login users and shells:"$(who) echo
" "
echo "show memory information"$(free)echo "
"
echo "cpu information"cat
/proc/cpuinfo
echo " "

Output:-
RESULT:

The above program has been executed and verified successfully.


Ex:No: 3
Script to implement the following commands: pipe, redirection and tee commands.AIM:
Aim:
To script to implement the following commands pipes,redirection,and teecommands.

ALGORITHM:

Step 1: Start the process


Step 2: Create a text file inputfile
Step 3: Add concept to that textfile
using
echo and echo GREEN APPLE
PINE APPLE
APPLE >input.txt

Step 4: Add piping to executegrp


command with keyword
APPLE
Step 5:Add piping to executews-l
command
Step 6:Update the input that filewith
keyword HANGO
Step 7:List all files starting with pin
current directory by tee
command
Step 8:Stop the process.

Shell script:
echo " Pipe Symbol Connect one or more
process"ls -1 *.sh | wc -l
echo "Redirect the output"
echo " "
echo "Redirect correct output to
newfile"sh job1.sh > correctoutput
echo " "

echo "Redirect error output to


newfile"sh job1.sh 2>
erroroutput
echo " "
echo "Redirect all output to
newfile" sh job1.sh &>
alloutput
echo " "
echo "final output to new file"
sh job1.sh &>
alloutput echo
" "
echo "new file"
sh job1.sh &> alloutput
echo "tee copies data from standard input to each FILE, and
also tostandard output"
ls -1 *.txt | wc -l | tee count.txt

Output:

RESULT:

The above program has been executed and verified successfully.


Ex: No :4
Script for displaying current date, username, file listing and directoriesby getting user
choice.

AIM:

To script for displaying current date, username, file listing and directoriesby getting
user choice.

ALGORITHM:
Step 1:Start the process Step 2:Use PS3 to enter the
string that displayed atconsole
Step 3:Use select and case comment to enter yourchoice and display it
Step 4:Use $(date) comment to display the date in option 1
Step 5:Use $(whoami) to printthe current user
Step 6:Use $(ls) to display filesand directories
Step 7:Stop the process.

Shell script:

#!/bin/bash

PS3='Please enter your choice: ' options=("Option


1""Option 2""Option 3""Quit")select opt in
"${options[@]}"
do
case $opt in
"Option 1")
echo “select choice 1”
echo "Date is " $(date)
;;
"Option 2")
echo “select choice 2”
echo "you username is " $(whoami)
;;
"Option 3")
echo “select choice $REPLY which is $opt”echo "to

list all files & directories" $(ls)


;;
"Quit")
break
;;
*) echo invalid option;;
esac
done

Output:

RESULT:

The above program has been executed and verified successfully.


Ex: No:5

Script to implement the filter command

Aim:
To script to implement the filter command.

Algorithm:
Step 1: Start the process.
Step 2: Using touch file create two files as see.txt and dee2.txt Step 3:
Enter the content into the files by using echo command .
Step 4: Use the filter command grep-n to display lines that match specifiedpattern.
Step 5: Use the command grep-o to display the no.of lines that match thepattern.
Step 6: Use the filter command grep-v to display the lines that do not match thespecified
pattern.
Step 7: Use wc command to count the word lines and characters.Step 8:
Stop the process.

Shell script:-

touch dee.txt.deel.txt
echo "hello
good morning
how are you
?"> dee.txt
echo "one two three four five alpha
beta gamma delta epsilon">
deel.txt
echo "filter commands"
echo " "
echo "display the lines that match the specified pattern"grep -n
"good" dee.txt
echo " "
echo "display the number of lines that match the pattern"grep -c
"good" dee.txt
echo " "
echo "display the lines that do not match the pattern"grep -v
"good" dee.txt
echo " "
echo "count of words,lines and character "wc
dee.txt
echo " "
echo "to extract a column" cut -f
2-4 deel.txt
cut -c 6 deel.txt
echo " "
echo "display the contents of file"cat
dee.txt
echo " "
echo "display in uppercase"
tr "[:lower:]""[:upper:]"< dee.txtecho "
"

Output:

RESULT:

The above program has been executed and verified successfully.


Ex:No:6

Script to implement the file which has 0 bytes

Aim:
To implement the file has which has 0 bytes.

Algorithm :

Step 1: Start the process


Step 2: enter any file name the user needs Step 3:
Choose the filenm with read statement
Step 4: Use the condition to find the file name which exit or not exit
Step 5: Use any variable like-e to exist file if condition gets true file gets existelse move
to next condition check the size of file
Step 6 :Remove the file using rm command which will be deletedStep 7
:Then use file not exist file not found
Step 8 :Stop the process.

Shell script:-

echo "Enter any file name: "


read filenm
if [ -e $filenm ]
then
echo $filenm" File exist"if [ -
s $filenm ]
then
echo $filenm" File has size > 0" elserm
$filenm
echo $filenm" File is Deleted which has size = 0"fi
else
echo "File not exist"fi

note:- to find the “file not exist” use the command


find . –type f –size 0b –delete

Output:-

RESULT:

The above program has been executed and verified successfully.


Ex:No:7
Script to find the sum of the individual digits of a given number
Aim
To find some of the individual digits of the given number.

Algorithm :

Step 1:Start the process


Step 2:Enter the number
Step 3:Read the input as n and declare r=0,sum=0
Step 4:Use while loop to cheak the condition toto
While[tn - gt 0]
Step 5: in do loop r=$
(( n%10 ))
n=$ ((n//10)) sum=$(($
sum € $ =r))
Step 6:Print sum of the digits
Step 7:Stop the process.

shell script:-

echo "Enter a Number:"


read n
temp=$n
sd=0
sum=0
while [ $n -gt 0 ]do
sd=$(( $n % 10 ))
n=$(( $n / 10 )) sum=$((
$sum + $sd ))done
echo "Sum is $sum"
Output:

RESULT:

The above program has been executed and verified successfully.


Ex:No:8

script to find the greatest among the given set of number using command line
argument:

Aim :

To find the greatest among the set of the given number

Algorithm :

Step 1:Start the process


Step 2:Enter the first number and read the i/f as A
Step 3:Enter the second number and read the i/f as B
Step 4:Enter the third number and read the i/f as C

Step 5:Use elefie to check whether A is greaterif


[$ A -gt $ B] && [$ A -gt $ C]
Step 6:Use elefie to check whether B is greaterif
[$ B -gt $ A] && [$ B- gt $ C]
Step 7:Use elefie to check whether C is greaterif
[$ C -gt $ A] && [$ C- gt $ B]
Step 8:Stop the process

Shell script:-

echo "enter the size of


array"read n
for((i=0;i<n;i++)
)do
echo "enter $((i+1))
number"read nos[$i]
done
echo "number
is"
for((i=0;i<n;i++)
)do
echo ${nos[$i]}
done
small=${nos[0]}
greatest=${nos[0
]}
for((i=0;i<n;i++))
do

if [ ${nos[$i]} -lt $small ];


then

small=${nos[$i]}
elif [ ${nos[$i]} -gt $greatest ];
then
greatest=${nos[$i
]}fi
done
echo "smallest number of array is $small"
echo "greatest number of array is $greatest"
Output:

RESULT:

The above program has been executed and verified successfully.


Ex:No:9

Script to check the given string is a palindrome

Aim:
To check the given string is a palindrome or not.

Algorithm:

Step 1: Start the process.


Step 2: Read the string name and reverse the string as names.
Step 3: Rev command is used to reverse the string
-if["$name" == "$name1"]
Step 4: If the given string is a palindrome then print the given string is a
palindrome.
Step 5: If the given string is not a palindrome then print the given string is not a
palindrome.
Step 6: Stop the process
Shell script:

echo "enter the


string/number"read s
echo $s>temp
rvs="$(rev
temp)"if [ $s =
$rvs ] then
echo "It is a
palindrome"else
echo "It is not a
palindrome"fi
Output:

RESULT:

The above program has been executed and verified successfully.


Ex:No:10

Script to print the multiplication table


Aim:

To print the multiplication table of the given argument.

Algorithm:

Step 1: Start the process.


Step 2: Enter a number that you want.
Step 3: Read the number and enter the no. Of multiple.
Step 4: Enter into for loop
- for(i=1;i<=10;i++)
Step 5: Use "$n*$i" ='expr$n\*$|' " which is used to print multiplication ofcertain
number.
Step 6: Stop the process.

Shell script:
echo "enter a
number"read n
echo "enter the number of multiples"
for((i=1;i<=10;i++))
do
echo "$n*$i = `expr $n \* $i`"
done
Output:

RESULT:

The above program has been executed and verified successfully.



You might also like