Wa0003.
Wa0003.
No : 01
Date : 01.03.2022
Aim:
Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.
Algorithm:
3. Perform the various Operations using Linux commands such as mv, rm. ls, etc.
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
5
Enter the number of choice
3
Displying the Content of copyfile using cat command......
Content of file 2
This is file 2 content
This is second line of the file
6
This is first line
This is second line
This is third line
This is last but one line
This is last line
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:
6. List the display the CPU information, CPU speed and Memory information.
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
Current shell
------------
/bin/bash
Home directory
--------------
/home/iiit
11
Enter your choice
3
Current login name for all users
--------------------------------
iiit tty7 2018-02-06 21:05 (:0)
12
Ex.No : 03
Date : 17.03.2022
Aim:
Write a Shell Script to implement the following: pipes, Redirection and tee commands.
Algorithm:
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
15
tee command Operations
-----------------------
Numbers before sorting
4
9
5
Numbers after Sorting
4
5
9
16
Ex.No : 04
Date : 25.03.2022
Write a shell script for displaying current date, user name, file listing and directories by
Algorithm:
3. List out the choice for users such as Display the Current Date, Current Time, Listing of
6. Repeat the operations until user quit the program with ‘q’
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 'data*' 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:
Algorithm:
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
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
Aim:
Write a shell script to remove the files which has file size as zero bytes.
Algorithm:
26
Coding:
clear
echo “###########################################”
ls -l
echo "List of the files after removing the zero byte files"
echo “####################################################”
ls -1
27
Output
###########################################
xsfileab
####################################################
28
Ex.No : 07
Date : 11.04.2022
Aim:
Write a shell script to find the sum of the individual digits of a given number.
Algorithm:
29
Coding:
clear
read n
while [ $n -gt 0 ]
do
n1=$((n % 10))
sum=$((sum + n1))
n=$((n / 10))
done
30
Output
5978
The Sum is 29
31
Ex.No : 08
Date : 12.04.2022
Aim:
Write a shell script to find the greatest among the given set of numbers using command
line arguments.
Algorithm:
4. Find the greatest number by comparing the numbers with each other.
32
Coding:
clear
echo “###############”
n=$#-1
big=$1
for ((i=1;i<=n;i=i+1))
do
if [ $big -lt $2 ]
then
big=$2
fi
shift
done
33
Output
./prg8.sh 5 2 1 9 7
Greatest number
###############
34
Ex.No : 09
Date : 21.04.2022
9. Palindrome checking.
Aim:
Algorithm:
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
malayalam
information
37
Ex.No : 10
Date : 22.04.2022
Aim:
Write a shell script to find the greatest among the given set of numbers using command
line arguments.
Algorithm:
38
Coding:
clear
echo “********************”
read m
read r
do
c=$(($n*$m))
done
39
Output
Multiplication table
********************
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