0% found this document useful (0 votes)
21 views17 pages

Semester IV Linux Lab Record

Linux lab

Uploaded by

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

Semester IV Linux Lab Record

Linux lab

Uploaded by

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

1.

Basic Linux Commands


Aim:
Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. Perform the various Operations using Linux commands such as mv, rm. ls, etc.
4. Display the result
5. Terminate the process.
Coding:
clear
echo "Linux Commands"
echo "**************"
echo "1.Removing Files"
echo "2.Copying Files"
echo "3.File Display - cat"
echo "4.Move the file"
echo "5.Comparing Files"
echo "6.Word Count"
echo "7.Split the file"
echo "8.Find the Difference"
echo "Enter the number of choice"
read a
if [ "$a" = 1 ]
then
echo "List of files"
echo "-------------"
ls
rm removefile
echo "List of files after removing a file with rm command"
echo "---------------------------------------------------"
ls
elif [ "$a" = 2 ]
then
echo "Content of the originalfile"
echo "---------------------------"
cat originalfile
echo " "
echo "Copying oringialfile to copyfile using cp command......"
echo " "
cp originalfile copyfile
echo "Content of the copyfile"
echo "-----------------------"
cat copyfile
elif [ "$a" = 3 ]
then
echo "Displying the Content of copyfile using cat command......"
echo " "
cat copyfile
elif [ "$a" = 4 ]
then
echo "Moving the Content of the file using mv command"
echo "---------------------------------------------"
echo "Move the content of copyfile to movefile........"
mv copyfile movefile
echo " "
cat movefile
elif [ "$a" = 5 ]
then
echo "Content of file 1"
cat file1
echo " "
echo "Content of file 2"
cat file2
echo " "
echo "Comparing two files using cmp commands....."
cmp file1 file2
elif [ "$a" = 6 ]
then
echo "Content of originalfile"
echo " "
cat originalfile
echo " "
echo "Word counting of the originalfile using wc...."
wc originalfile
elif [ "$a" = 7 ]
then
echo "Content of the file 3"
cat file3
echo ""
split -3 file3 xsfile
echo "File successfully spitted........."
echo "Split File 1"
echo "------------"
cat *aa
echo "Split File 2"
echo "------------"
cat *ab
elif [ "$a" = 8 ]
then
echo ""
echo "Content of file 1....."
cat file1
echo ""
echo "Content of file 2....."
cat file2
echo " "
echo "Difference between file 1 and file 2"
diff file1 file2
else
echo "Invalid Value"
fi
Output
Linux Commands
**************
1.Removing Files
2.Copying Files
3.File Display - cat
4.Move the file
5.Comparing Files
6.Word Count
7.Split the file
8.Find the Difference

Enter the number of choice


1
List of files
-------------
Copyfile file1 file2 file3 movefile numbers
Orifile out11.txt prg10.sh prg1.sh prg2.sh prg3.sh
prg4.sh prg5.sh prg6.sh prg7.sh prg8.sh prg9.sh
removefile xsfileaa xsfileab

List of files after removing a file with rm command


---------------------------------------------------
Copyfile file1 file2 file3 movefile numbers
Orifile out11.txt prg10.sh prg1.sh prg2.sh prg3.sh
prg4.sh prg5.sh prg6.sh prg7.sh prg8.sh prg9.sh
xsfileaa xsfileab

Enter the number of choice


2
Content of the originalfile
---------------------------
This is original file which has to be copied

Copying oringialfile to copyfile using cp command......

Content of the copyfile


-----------------------
This is original file which has to be copied

Enter the number of choice


3
Displying the Content of copyfile using cat command......

This is original file which has to be copied

Enter the number of choice


4
Moving the Content of the file using mv command
-----------------------------------------------
Move the content of copyfile to movefile........

This is original file which has to be copied

Enter the number of choice


5
Content of file 1
This is file 1 content
This is second line of the file

Content of file 2
This is file 2 content
This is second line of the file

Comparing two files using cmp commands.....


file1 file2 differ: byte 14, line 1

Enter the number of choice


6
Content of originalfile

This is original file which has to be copied

Word counting of the originalfile using wc....


1 9 45 originalfile

Enter the number of choice


7
Content of the file 3
This is first line
This is second line
This is thrid line
This is last but one line
This is last line

File successfully splited.........


Split File 1
------------
This is first line
This is second line
This is thrid line
Split File 2
------------
This is last but one line
This is last line

Enter the number of choice


8
Content of file 1.....
This is file 1 content
This is second line of the file

Content of file 2.....


This is file 2 content
This is second line of the file

Difference between file 1 and file 2


1c1
< This is file 1 content
---
> This is file 2 content
2. System Configuration
Aim:
Write a 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 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.
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. List the logged user details
4. Display the shell, home directory, OS type, etc
5. Display the all the shells available.
6. List the display the CPU information, CPU speed and Memory information.
7. Terminate the process.
Coding:
clear
echo "1.Current user and his login name"
echo "2.Current shell, Home directory, Operating system, Current
path, Current working directory"
echo "3.Current login number of all users, show all available shells"
echo "4.CPU information like process type , speed"
echo "5.Memory information like program type"
echo "Enter your choice"
read a
if [ "$a" = 1 ]
then
echo " Current user and his login name"
whoami
elif [ "$a" = 2 ]
then
echo "Current shell"
echo "------------"
echo "$SHELL"
echo "Home directory"
echo "--------------"
echo "$HOME "
echo ""
echo "Operating system type"
echo "---------------------"
uname -a
echo ""
echo "Current path setting"
echo "--------------------"
echo "$PATH"
echo ""
echo "Current working directory"
echo "-------------------------"
pwd
elif [ "$a" = 3 ]
then
echo "Current login name for all users"
echo "--------------------------------"
who
echo ""
echo "Show all available shells"
echo "-------------------------"
cat /etc/shells
elif [ "$a" = 4 ]
then
grep "model name" /proc/cpuinfo
elif [ "$a" = 5 ]
then
echo "Memory information,process type"
vmstat
else
echo "invalid Value"
fi

Output
1.Current user and his login name
2.Current shell, Home directory, Operating system, Current path, Current
working directory
3.Current login number of all users, show all available shells
4.CPU information like process type , speed
5.Memory information like program type

Enter your choice


1
Current user and his login name
iiit

Enter your choice


2

Current shell
------------
/bin/bash

Home directory
--------------
/home/iiit

Operating system type


---------------------
Linux iiit 4.13.0-32-generic #35~16.04.1-Ubuntu SMP Thu Jan 25 10:13:43 UTC
2018 x86_64 x86_64 x86_64 GNU/Linux

Current path setting


--------------------
/home/iiit/bin:/home/suresh/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin
:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Current working directory


-------------------------
/home/iiit
Enter your choice
3
Current login name for all users
--------------------------------
iiit tty7 2018-02-06 21:05 (:0)

Show all available shells


-------------------------
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash

Enter your choice


4

model name : Intel(R) Pentium(R) CPU P6200 @ 2.13GHz


model name : Intel(R) Pentium(R) CPU P6200 @ 2.13GHz

Enter your choice


5
Memory information, process type
procs-------memory-------------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 2280940 94768 894800 0 0 342 30 158 506 5 2 88 6 0

Enter your choice


9
INVALID VALUE
3. Pipes, Redirection and tee command
Aim:
Write a Shell Script to implement the following: pipes, Redirection and tee commands.
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. Display the list of commands to users
4. Based on selection of command perform the operations
5. Terminate the process.
Coding:
clear
echo "1.Pipes"
echo "2.Redirection"
echo "3.tee command"
echo "Enter your choice"
read a
case ${a} in
1)
echo "Pipes Operation - Combing ls and sort command"
echo "----------------------------------------------"
ls|sort
;;
2)
echo "Redirection Operations"
echo "----------------------"
echo "content of the file file8"
cat file8
echo "content of the file file10"
cat file10
cat file8>file10
echo "content of the file file10 after redirection"
cat file10
;;
3)
echo "tee command Operations"
echo "----------------------"
echo "Numbers before sorting"
cat numbers
echo "Numbers after Sorting "
sort numbers | tee sort-number
;;
*)
echo "Invalid Choice"
;;
esac
4. Displaying Current Date, User Name and File Listing
Aim:
Write a shell script for displaying current date, user name, file listing and directories by getting
user choice
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. List out the choice for users such as Display the Current Date, Current Time, Listing of files and
currently working directory.
4. Read the choice of the user
5. Based on the user choice perform the operation
6. Repeat the operations until user quit the program with ‘q’
7. Terminate the process
Coding:
clear
while true
do
echo "Enter The Choice:"
echo "(Press 'q' to Exit)"
echo "1.To Display Current date"
echo "2.To Display Current User"
echo "3.To Display File List"
echo "4.To Display The Present Working Directory"
read choice
case $choice in
1) date;;
2) who;;
3) ls;;
4) pwd;;
q) break;;
*) echo "Incorrect Choice";;
esac
done

Output
1.Pipes
2.Redirection
3.tee command
Enter your choice
1
Pipes Operation - Combing ls and sort command
-----------------------------------------------
file1 file2 file3 movefile numbers prg10.sh
prg1.sh prg2.sh prg3.sh prg4.sh prg5.sh prg6.sh
prg7.sh prg8.sh prg9.sh xsfileaa xsfileab

Enter your choice


2
Redirection Operations
----------------------
content of the file file3
This is first line
This is second line
This is thrid line
This is last but one line
This is last line

content of the file refile

content of the file file10 after redirection


This is first line
This is second line
This is thrid line
This is last but one line
This is last line

Enter your choice


3
tee command Operations
-----------------------
Numbers before sorting
4
9
5
Numbers after Sorting
4
5
9
5. Filter commands
Aim:
Write a shell script to implement the filter commands.
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. List out the various filters
4. Apply the filter operation selected by the user
5. Terminate the process.
Coding:
clear
echo "Filter Commands"
echo "%%%%%%%%%%%%%%%"
echo "1.grep Filter"
echo "2.wc Filter"
echo "3.cut Filter"
echo "4.tr Filter"
echo "enter your choice"
read a
if [ "$a" = 1 ]
then
echo "grep Filter"
echo "~~~~~~~~~~~"
grep "find" data*
elif [ "$a" = 2 ]
then
echo "wc Filter"
echo "~~~~~~~~"
echo "content of the file file1"
cat file1
echo "Word count of the file file1"
wc file1
elif [ "$a" = 3 ]
then
echo "cut Filter"
echo "~~~~~~~~~"
echo "content of the file2"
cat file2
echo "extracting first 5 character from the file2"
cut -c1-5 file2
elif [ "$a" = 4 ]
then
echo "tr Filter"
echo "~~~~~~~~~"
echo "Changing the case"
echo "~~~~~~~~~~~~~~~~~"
tr "[a-z]" "[A-Z]"
else
echo "Invalid Value"
fi
Output
Filter Commands
%%%%%%%%%%%%%%%
1.grep Filter
2.wc Filter
3.cut Filter
4.tr Filter

enter your choice


1
grep Filter
~~~~~~~~~~~
data1:Find the largest number of give set
data3:Find the sum of the digit

Enter your choice


2
wc Filter
~~~~~~~~
content of the file file1
This is file 1 content
This is second line of the file
Word count of the file file1
2 12 55 file1

enter your choice


3
cut Filter
~~~~~~~~~
content of the file2
This is file 2 content
This is second line of the file
extracting characters from the file2
This is file 2
This is second

enter your choice


4
tr Filter
~~~~~~~~~
Changing the case
~~~~~~~~~~~~~~~~~
bharathidasan college of arts and science
BHARATHIDASAN COLLEGE OF ARTS AND SCIENCE
erode
ERODE
6. Removing the Zero Bytes files
Aim:
Write a shell script to remove the files which has file size as zero bytes.
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. Find the files which size of aero
4. Delete the files of zero size
5. Display the rest the files
6. Terminate the process.
Coding:
clear
echo "Removing Zero Byte Files"
echo "List of file before removing the zero bytes"
echo “###########################################”
ls -l
for i in `ls`
do
if [ -f$i -a ! -s $i ]
then
echo "file $i is removed"
rm $i
fi
done
echo "List of the files after removing the zero byte files"
echo “####################################################”
ls -1

Output
Removing Zero Byte Files
List of file before removing the zero bytes
###########################################
data1 data2 data3 numbers prg10.sh prg1.sh
prg2.sh prg3.sh prg4.sh prg5.sh prg6.sh prg7.sh
prg8.sh prg9.sh rmfile1 rmfile2 sort-number xsfileaa
xsfileab
file rmfile1 is removed
file rmfile2 is removed
List of the files after removing the zero byte files
####################################################
data1 data2 data3 numbers prg10.sh prg1.sh
prg2.sh prg3.sh prg4.sh prg5.sh prg6.sh prg7.sh
prg8.sh prg9.sh sort-number xsfileaa xsfileab
7. Sum of Individual Digits
Aim:
Write a shell script to find the sum of the individual digits of a given number.
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. Read a number which has more than one digit
4. Find the sum of the digit
5. Display the sum of the digit
6. Terminate the process.

Coding:
clear
echo "Enter The Number"
read n
while test $n -ne 0
do
n1 = `expr $n % 10`
sum = `expr $sum + $n1`
n=`expr $n / 10`
done
echo "The Sum is ${sum}"

Output
Enter The Number
5978
The Sum is 29
8. Finding the Greatest Number
Aim:
Write a shell script to find the greatest among the given set of numbers using command line
arguments.

Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. Read the numbers from command line
4. Find the greatest number by comparing the numbers with each other.
5. Display the greatest number
6. Terminate the process.

Coding:
clear
echo "Greatest number"
echo “###############”
n=$#-1
big=$1
for ((i=1;i<=n;i=i+1))
do
if [ $big -lt $2 ]
then
big=$2
fi
shift
done
echo "The greatest number is $big"

Output
chmod u+x prg8.sh
./prg8.sh 5 2 1 9 7

Greatest number
###############
The greatest number is 9
9. Palindrome checking.
Aim:
Write a shell script for palindrome checking.
Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. Read a string from the user
4. Check whether given string is palindrome or not
5. Display the result
6. Terminate the process.

Coding:
clear
echo "Enter the String"
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
l=`expr $len / 2`
ctr=1
flag=0
while [ $ctr -le $l ]
do
a=`echo $str | cut -c$ctr`
b=`echo $str | cut -c$len`
if [ $a != $b ]
then
flag=1
break
fi
ctr=`expr $ctr + 1`
len=`expr $len - 1`
done
if test $flag -eq 0
then
echo "The given String is Palindrome"
else
echo "The given String is not a Palindrome"
fi

Output
Enter the String
malayalam

The given String is Palindrome

Enter the String


information

The given String is not a Palindrome


10. Multiplication Table
Aim:
Write a shell script to find the greatest among the given set of numbers using command line
arguments.

Algorithm:
1. Start the Process
2. Open a new shell script using vi editor
3. Read the number for which multiplication table to be generated
4. Display the multiplication table.
5. Terminate the process.

Coding:
clear
echo "Multiplication table"
echo “********************”
echo "Enter the number for which table to be generated: "
read m
echo "Enter the range"
read r
for (( n=1; n<=r; n++ ))
do
c=$(($n*$m))
echo " $n * $m=$c"
done

Output
Multiplication table
********************
Enter the number for which table to be generated:
9
Enter the range
10

1 * 9=9
2 * 9=18
3 * 9=27
4 * 9=36
5 * 9=45
6 * 9=54
7 * 9=63
8 * 9=72
9 * 9=81
10 * 9=90

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy