You are on page 1of 31

Core Lab 5: Operating System and LINUX Programming Lab

S# PROGRAMS Page No
1. FILE Commands
2. System Configuration
3. Pipes, Redirection and tee commands
4. Displaying Current date, user name, file listing and directories
5. Filter commands
6. Remove the files with Zero size
7. Sum of the individual digits
8. Greatest among the given set of numbers
9. Palindrome checking
10. Print the multiplication table
1.FILE Commands

Aim:

Write a shell script to stimulate the file commands: rm, cp, cat, mv, cmp, wc, split, diff.

Algorithm:

Step 1: Start the process.


Step 2: Open the terminal in linux.
Step 3: Type the rm command for removing file.
Step 4: Type cp command for copying one file to another.
Step 5: Type the cat command for displaying the content of file.
Step 6: Type the mv command for moving a file from one directory to another directory.
Step 7: Type the cmp command for compare two files if the files are equal nothing to display.
If files are different with display the different bytes & lines.
Step 8: Type the wc command for counting the bytes,characters and lines.
Step 9: Type the split command for spliting the contents into different files.wecan split a file in
the term of bytes or lines.
Step10: Type the different command for analyzies two files and prints the lines that are
different.
Step11: Stop the process.
Program :

$rm a
$cp a b
$cat a b
$mv a Desktop\ran
$cmp a b
$wc a
$split a -l 1 b
$diff a b

Output

ksgcas@ksgcas-Vertion-Series ~/Desktop $ rm one

ksgcas@ksgcas-Vertion-Series ~/Desktop $ cat xyz


ksg college
red
green
blue

ksgcas@ksgcas-Vertion-Series ~/Desktop $ cp xyz xyz1

ksgcas@ksgcas-Vertion-Series ~/Desktop $ cat xyz1


ksg college
red
green
blue

ksgcas@ksgcas-Vertion-Series ~/Desktop $ mv xyz ksgcas

ksgcas@ksgcas-Vertion-Series ~/Desktop $ cmp xyz1 mnp


xyz1 mnp differ:
byte 1, line 1

ksgcas@ksgcas-Vertion-Series ~/Desktop $ wc xyz1


5 5 28 xyz1

ksgcas@ksgcas-Vertion-Series ~/Desktop $ split xyz1 -l 1 sp


ksgcas@ksgcas-Vertion-Series ~/Desktop $ diff xyz1 mnp
1,4c1,2
< ksg college
< red
< green
< blue
---
> royal
> good
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:

Step 1: Start the process.


Step 2: Open the terminal in linux.
Step 3: Type the command “log name” for displaying currently logged user.
Step 4: Type the “echo $ SHELL” command for displaying current shell.
Step 5: Type the “echo $ HOME” command for displaying home directory.
Step 6: Type the “echo $ OSTYPE” command for displaying operating system type.
Step 7: Type the “echo $ PATH” command for displaying current path setting.
Step 8: Type the “pwd” command for displaying current working directory
Step 9: Type the “w” command for show the currently logged number of users.
Step10: Type the “less/proc/cpuinfo “ command for displaying the cpu information like
processor type and speed.
Step11: Type the “free –m “ for shows the memory information .
Step12: Stop the process.
Program:

a)currently logged user and his log name


$ logname
ksgcas
b.current shell , home directory , Operating System type , current Path setting , current
working directory
1.current shell
$ echo $SHELL
2.home directory
$echo $HOME
3.operating system type
$echo $OSTYPE
4.current path setting
$echo $PATH
5.current working directory
$pwd
c.show currently logged number of users, show all available shells
$w
$who -a
d.show CPU information like processor type and speed
$less /proc/cpuinfo
e.show memory information
$free -m
output:

ksgcas@ksgcas-Vertion-Series ~/Desktop $ logname


ksgcas

ksgcas@ksgcas-Vertion-Series ~/Desktop $ echo $SHELL


/bin/bash

ksgcas@ksgcas-Vertion-Series ~/Desktop $ echo $HOME


/home/ksgcas

ksgcas@ksgcas-Vertion-Series ~/Desktop $ echo $OSTYPE


linux-gnu

ksgcas@ksgcas-Vertion-Series ~/Desktop $ echo $PATH


/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

ksgcas@ksgcas-Vertion-Series ~/Desktop $ pwd


/home/ksgcas/Desktop

ksgcas@ksgcas-Vertion-Series ~/Desktop $ w
14:47:55 up 2:14, 2 users, load average: 1.20, 0.96, 0.57
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
ksgcas tty7 :0 12:33 2:14m 2:08 0.17s cinnamon-sessio
ksgcas pts/1 :0 13:51 1.00s 0.10s 0.00s w

ksgcas@ksgcas-Vertion-Series ~/Desktop $ who -a


system boot 2017-12-13 12:33
run-level 2 2017-12-13 12:33
LOGIN tty4 2017-12-13 12:33 759 id=4
LOGIN tty5 2017-12-13 12:33 761 id=5
LOGIN tty2 2017-12-13 12:33 767 id=2
LOGIN tty3 2017-12-13 12:33 768 id=3
LOGIN tty6 2017-12-13 12:33 771 id=6
LOGIN tty1 2017-12-13 12:33 1298 id=1
ksgcas + tty7 2017-12-13 12:33 old 1561 (:0)
ksgcas + pts/1 2017-12-13 13:51 . 2784 (:0)
pts/2 2017-12-13 13:51 0 id=/2 term=0 exit=0

ksgcas@ksgcas-Vertion-Series ~/Desktop $ less /proc/cpuinfo


ksgcas@ksgcas-Vertion-Series ~/Desktop $ free -m
total used free shared buffers cached
Mem: 991 886 105 207 6 460
-/+ buffers/cache: 419 571
Swap: 1012 228 784
3.Pipes, Redirection and tee commands
Aim:

Write a Shell Script to implement the following: pipes, Redirection and tee commands.

Algorithm:

Step 1: Start the process.


Step 2: Open the terminal in linux.
Step 3: By using pipe command (|) combine the linux commands, type the “ cat one | grep
amma “ is finding the amma is one file.
Step 4: The Redirection ( <,>,>>) commands helps to redirect the output.type the “ ls > out1 “
for redirect the list output to out1 file.
Step 5: Type the >>“ls >> out2 “ redirect the output to out2 file also display in the screen.
Step 6: Type the “ sort < one “ command sort the content of one file and output display to the
screen.
Step 7: Type the “ ls -1 | WC –l | tee out3 “ command for count the lines in given ls command
and it stored in out3 file also display in the screen using tee command.
Step 8: Stop the process
Program:

1.Pipe (|)
$cd Desktop
Desktop$ ls | head -3

Desktop$ ls | head -3 | tail -1

Desktop$ ls | head -3 | tail -1 > result


Desktop$cat Result

Desktop$ cat one | grep amma

2. Redirection (<, >, >>)


Desktop$ ls > out1
Desktop$cat out1

Desktop$ ls >>out1

Desktop$sort < one

3.tee
Desktop$ ls -1 | wc -l | tee out3
Desktop$cat out3
Output:

ksgcas@ksgcas-Vertion-Series ~ $ cd Desktop/

ksgcas@ksgcas-Vertion-Series ~/Desktop $ ls | head -3


abc
count.txt
de.sh

ksgcas@ksgcas-Vertion-Series ~/Desktop $ ls | head -3 | tail -1


de.sh

ksgcas@ksgcas-Vertion-Series ~/Desktop $ ls | head -3 | tail -1 > result

ksgcas@ksgcas-Vertion-Series ~/Desktop $ cat result


de.sh

ksgcas@ksgcas-Vertion-Series ~/Desktop $ cat one | grep amma


amma

ksgcas@ksgcas-Vertion-Series ~/Desktop $ ls > out1


ksgcas@ksgcas-Vertion-Series ~/Desktop $ cat out1
abc
count.txt
one
out1
three
xyz

ksgcas@ksgcas-Vertion-Series ~/Desktop $ ls >>out1


ksgcas@ksgcas-Vertion-Series ~/Desktop $ sort < one
amma
anna
appa
thambi

ksgcas@ksgcas-Vertion-Series ~/Desktop $ ls -1 | wc -l | tee out3


20
ksgcas@ksgcas-Vertion-Series ~/Desktop $ cat out3
20
4.Displaying Current date, user name, file listing and directories

Aim:

Write a shell script for displaying current date, user name, file listing and directories by getting
user choice.

ALGORITHM:
Step 1: Start the process.
Step 2: Open the terminal in linux.
Step 3: Type the gedit command type the program option1 for current date(date),option 2 for
username(who),option 3 for file listing and directories(ls) and option 4 for existing the
program.
Step 4: Save the file to pgm 4.sh
Step 5: Set the permission using chmod +x /Desktop/pgm4.sh
Step 7: Stop the process.
Program

choice=0
while [ $choice -ne 4 ]
do
tput clear

echo "1. Today date"


echo "2. Total login user"
echo "3. File Listing and Directories"
echo "4. Exit"
echo "Enter your choice"
read choice

if [ $choice -eq 1 ]
then
echo "Today is `date +%B` `date | cut -f 3 -d ""`, `date | cut -f 6 -d ""`"

elif [ $choice -eq 2 ]


then
echo "As of now `who` user are login to the system"

elif [ $choice -eq 3 ]


then
echo "`ls`"
fi
echo "Are you continue (1 for yes/0 for n)"
read temp

if [ $temp -eq 0 ]
then
choice=4
fi
done
Output

ksgcas@ksgcas-Vertion-Series ~ $ gedit Desktop/pgm4.sh


ksgcas@ksgcas-Vertion-Series ~ $ chmod +x Desktop/pgm4.sh
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/pgm4.sh

1. Today date
2. Total login user
3. File Listing and Directories
4. Exit
Enter your choice
2
As of now ksgcas tty7 2018-01-06 09:14 (:0)
ksgcas pts/1 2018-01-06 10:47 (:0) user are login to the system
Are you continue (1 for yes/0 for n)
5. Filter commands

Aim:
write a shell script to implement the filters commands

Algorithm:

Step 1 : Start the process.


Step 2 : Open the terminal.
Step 3 : Type the “ cat > test” command for create a file and type some content.
Step 4 : Type grep “ red “ test command for finding the “red “t est command forfinding the “red
“ text available in test file.
Step 5 : Type grep –i “red “ test command for finding the “red “ text [ignore case] available in
test file.
Step 6 : Type grep “red “ test * command for find the red text in all file name started with test.
Step 7 : Type grep –n “red “ test command to find the number of red text in the given test file.
Step 8 : Type grep –blue “ksg” test command find the “ksg” text in test file and displaying
blue color.
Step 9 : Type grep –v red test command to displaying the all content without red text.
Step 10: Stop the process.
Program

grep "red" test


grep -i "red" test
grep "red" test*
grep -n "red" test
grep -blue "ksg" test
grep -v red test
Output

ksgcas@ksgcas-Vertion-Series ~/Desktop $ cat >test


ksg college coimbatore
red
green
RED
blue
psg college coimbatore

ksgcas@ksgcas-Vertion-Series ~/Desktop $ grep "red" test


red

ksgcas@ksgcas-Vertion-Series ~/Desktop $ grep -i "red" test


red
RED

ksgcas@ksgcas-Vertion-Series ~/Desktop $ grep "red" test*


test:red
test1:red

ksgcas@ksgcas-Vertion-Series ~/Desktop $ grep -n "red" test


2:red

ksgcas@ksgcas-Vertion-Series ~/Desktop $ grep -blue "ksg" test


test

ksgcas@ksgcas-Vertion-Series ~/Desktop $ grep -v red test


ksg college coimbatore
green
RED
blue
psg college coimbatore
6.Remove the files with zero size

Aim:
write a shell script to remove the files which has file size as zero bytes

Algorithm:

Step 1: Start the process.


Step 2: Open the terminal process.
Step 3: Type the gedit command.
Step 4: Type the program for finding the zero size files by using find Desktop/-size 0 “
command.
Step 5: Prompt from user for removing the files using read command.
Step 6: If user prompt yes then delete the zero byte files using “rm {} “
Step 7: Stop the process.
Program:

echo "List of files with 0 size"


find Desktop/ -size 0
echo -n "Do you want delete 0 size files(y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
find Desktop/ -size 0 -exec rm {} \;
echo "0 size files are now deleted.."
fi
Output:

ksgcas@ksgcas-Vertion-Series ~ $ Desktop/de.sh
List of files with 0 size
Desktop/file1
Desktop/file5
Do you want delete 0 size files(y/n)? y
0 size files are now deleted..
7. sum of individual digits

Aim:
Write a shell script to find the sum of the individual digits of a given number.

Algorithm:
Step 1: Start the process.
Step 2: open the terminal in linux.
Step 3: type the gedit command.
Step 4: type the program for finding the sum of individual digits of a given number.
Step 5: Input number n
Step 6: Set sum=0,sd=0
Step 7: Find single digit in sd as n%10 it will give(left most digit).
Step 8: Construct sum no as sum=sum+sd
Step 9: Decrment n by 1
Step10: Is n is greater than zero, if yes goto step 3,otherwise next step.
Step11: print sum.
Step12: stop the process.
Program:

if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " I will find sum of all digit for given number"
echo " For eg. $0 123, I will print 6 as sum of all digit (1+2+3)"
exit 1
fi

n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum of digit for numner is $sum"
output:

ksgcas@ksgcas-Vertion-Series ~ $ gedit Desktop/pgm7.sh


ksgcas@ksgcas-Vertion-Series ~ $ chmod +x Desktop/pgm7.sh
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/pgm7.sh 123
Sum of digit for numner is 6
8.Greatest among the given set of numbers
Aim:

Write a shell script to find the greatest among the given set of numbers using command line
arguments.

Algorithm:

step 1: start the process.


Step 2: nopen the terminal in linux.
Step 3: type the gedit command.
Step 4: type the program for finding the greatest among the given set of numbers using
command line arguments.
Step 5: START: Take three nos as n1,n2,n3.
Step 6: Is n1 is greater than n2 and n3, if yes print n1 is bigest no goto step 5, otherwise goto
next step
Step 7: Is n2 is greater than n1 and n3, if yes print n2 is bigest no goto step 5, otherwise goto
next step
Step 8: Is n3 is greater than n1 and n2, if yes
print n3 is bigest no goto step 5, otherwise goto next step
step9: stop the process.
Program:

if [ $# -ne 3 ]
then
echo "$0: number1 number2 number3 are not given">&2
exit 1
fi
n1=$1
n2=$2
n3=$3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
then
echo "$n1 is Bigest number"
elif [ $n2 -gt $n1 ] && [ $n2 -gt $n3 ]
then
echo "$n2 is Bigest number"
elif [ $n3 -gt $n1 ] && [ $n3 -gt $n2 ]
then
echo "$n3 is Bigest number"
elif [ $1 -eq $2 ] && [ $1 -eq $3 ] && [ $2 -eq $3 ]
then
echo "All the three numbers are equal"
else
echo "I can not figure out which number is biger"
fi
OUTPUT
ksgcas@ksgcas-Vertion-Series ~ $ gedit Desktop/pgm8.sh
ksgcas@ksgcas-Vertion-Series ~ $ chmod +x Desktop/pgm8.sh
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/pgm8.sh 124 56 33
124 is Bigest number
ksgcas@ksgcas-Vertion-Series ~ $
9.Palindrome
Aim:
Write a shell script for palindrome checking.

Algorithm:
Step 1: start the process.
Step 2: open the terminal in linux.
Step 3: type the gedit command.
Step 4: type the program for finding the palindrome checking.
Step 5: get a string from user.
Step 6: find the reverse of the given string and store into the revstr.
Step 7: compare given string and reversed string, if they are equal print palindrome otherwise
print not print palindrome.
Program:
echo -n "Enter a string:"
read str
for i in $(seq 0 ${#str}) ; do
revstr=${str:$i:1}$revstr
done
echo "Given String" $str
echo "Reverse String" $revstr
if [ "$str" = "$revstr" ] ; then
echo "It is palindrome"
else
echo "Not a palindrome"
fi
output:

ksgcas@ksgcas-Vertion-Series ~ $ gedit Desktop/pgm9.sh


ksgcas@ksgcas-Vertion-Series ~ $ chmod +x Desktop/pgm9.sh
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/pgm9.sh
Enter a string:malayalam
Given String malayalam
Reverse String malayalam
It is palindrome
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/pgm9.sh
Enter a string:ksgcas
Given String ksgcas
Reverse String sacgsk
Not a palindrome
10.Multiplication table

Aim:
Write a shell script to print the multiplication table of the given argument using for loop

Algorithm:

step 1: start the process.


Step 2: open the terminal in linux.
Step 3: type the gedit command .
Step 4: type the program for print the multiplication table of the given number.
Step 5: Get the Table number from the user.
Step 6: Get the range from the user.
step 7: start the i from 1 to given range using while loop.
step 8: calculate the expression of the table.
step 9: Print the table expression .
step10: Increment i one by one.
step11: Repeate the steps 8 and 9 until reaching the range.
step12: After reaching the range exit from while loop.
step13: Stop the process.
Program
echo "Enter a Number"
read n

echo "Enter Range"


read r
i=1
while [ $i -le $r ]
do
echo " $i x $n = `expr $n \* $i`"
i=`expr $i + 1`
done
Output:

ksgcas@ksgcas-Vertion-Series ~ $ gedit Desktop/pgm10.sh


ksgcas@ksgcas-Vertion-Series ~ $ chmod +x Desktop/pgm10.sh
ksgcas@ksgcas-Vertion-Series ~ $ Desktop/pgm10.sh
Enter a Number
5
Enter Range
10
1x5=5
2 x 5 = 10
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
ksgcas@ksgcas-Vertion-Series ~ $

You might also like