0% found this document useful (0 votes)
71 views40 pages

Wa0003.

The document describes a shell script that simulates various Linux commands like rm, cp, cat, mv, cmp, wc, split, and diff. The script provides a menu for the user to select which command to run and then executes the corresponding Linux command to perform the requested operation on sample files. It outputs the results of running each command, demonstrating their functionality.

Uploaded by

Shri harini
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)
71 views40 pages

Wa0003.

The document describes a shell script that simulates various Linux commands like rm, cp, cat, mv, cmp, wc, split, and diff. The script provides a menu for the user to select which command to run and then executes the corresponding Linux command to perform the requested operation on sample files. It outputs the results of running each command, demonstrating their functionality.

Uploaded by

Shri harini
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/ 40

Ex.

No : 01

Date : 01.03.2022

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.

1
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"

2
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 ]

3
then
echo "Content of the file 3"
cat file3
echo " "
split -l 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

4
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

5
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

6
This is first line
This is second line
This is third 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 third 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

7
Ex.No : 02

Date : 09.03.2022
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.

8
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

9
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

10
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

11
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

12
Ex.No : 03

Date : 17.03.2022

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.

13
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"

14
;;
esac

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 file8
This is first line
This is second line
This is third line
This is last but one line
This is last line

content of the file file10

content of the file file10 after redirection


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

Enter your choice


3

15
tee command Operations
-----------------------
Numbers before sorting
4
9
5
Numbers after Sorting
4
5
9

16
Ex.No : 04

Date : 25.03.2022

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

17
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

18
OUTPUT
Enter the choice
(press q to Exit)
1.To display current current date
2.To display current user
3.To display file list
4.To display the present working directory
1
Mon May 9 09:42:50 IST 2022
Enter the choice
(press q to Exit)
1.To display current current date
2.To display current user
3.To display file list
4.To display the present working directory
2
user21 pts/0 2022-05-09 09:09 (192.168.18.5)
user10 pts/1 2022-05-09 09:34 (192.168.18.42)
user17 pts/2 2022-05-09 09:10 (192.168.18.2)
user15 pts/3 2022-05-09 09:34 (192.168.18.26)
user7 pts/4 2022-05-09 09:40 (192.168.18.24)
user14 pts/5 2022-05-09 09:11 (192.168.18.22)
user9 pts/6 2022-05-09 09:11 (192.168.18.14)
user12 pts/9 2022-05-09 09:23 (192.168.18.16)
user19 pts/10 2022-05-09 09:35 (192.168.18.4)
user1 pts/11 2022-05-09 09:36 (192.168.18.3)
Enter the choice
(press q to Exit)
1.To display current current date
2.To display current user
3.To display file list
4.To display the present working directory
3
aa.txt c.txt ex6.sh file3.txt pro10.sh pro5.sh program1.sh shpro1.sh
shpro6.sh
shpro.sh swap.sh
ab.txt &#39;data*&#39; file10 file8.txt pro1.sh pro6.sh programs
shpro2.sh shpro7.sh sort-
number vet.txt

19
add.sh editor file10.txt numbers.xtx pro2.sh pro7.sh read.sh shpro3.sh
shpro8.sh
sort-numbers.txt xsfile.txt
b.txt ex file1.txt originalfile.txt pro3.sh pro8.sh s1 shpro4.sh
shpro9.sh sort-
numbers.xtx xsfile.txtaa
copy.txt ex2.sh file2.txt p1.sh pro4.sh pro9.sh shpro10.sh shpro5.sh
shprog.sh
ss.txt
Enter the choice
(press q to Exit)
1.To display current current date
2.To display current user
3.To display file list
4.To display the present working directory
4
/home/user9
Enter the choice
(press q to Exit)
1.To display current current date
2.To display current user
3.To display file list
4.To display the present working directory
q

20
Ex.No : 05

Date : 01.04.2022

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.

21
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 "~~~~~~~~~"

22
echo "Changing the case"
echo "~~~~~~~~~~~~~~~~~"
tr "[a-z]" "[A-Z]"
else
echo "Invalid Value"
fi

23
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

24
enter your choice
4
tr Filter
~~~~~~~~~
Changing the case
~~~~~~~~~~~~~~~~~
VET Institute of arts and science college
VET INSTITUTE OF ARTS AND SCIENCE COLLEGE
erode
ERODE

25
Ex.No : 06

Date : 04.04.2022

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.

26
Coding:

clear

echo "Removing Zero Byte Files"

echo "List of file before removing the zero bytes"

echo “###########################################”

ls -l

find ./ -type f -size o -exec rm -f {} \;

echo "List of the files after removing the zero byte files"

echo “####################################################”

ls -1

27
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

28
Ex.No : 07

Date : 11.04.2022

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.

29
Coding:

clear

echo "Enter The Number"

read n

while [ $n -gt 0 ]

do

n1=$((n % 10))

sum=$((sum + n1))

n=$((n / 10))

done

echo "The Sum is” $sum

30
Output

Enter the Number

5978

The Sum is 29

31
Ex.No : 08

Date : 12.04.2022

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.

32
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"

33
Output

chmod u+x prg8.sh

./prg8.sh 5 2 1 9 7

Greatest number

###############

The greatest number is 9

34
Ex.No : 09

Date : 21.04.2022

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.

35
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

36
Output

Enter the String

malayalam

The given String is Palindrome

Enter the String

information

The given String is not a Palindrome

37
Ex.No : 10

Date : 22.04.2022

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.

38
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

39
Output
Multiplication table

********************

Enter the number for which table to be generated:

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

40

You might also like