Page 1
www.dharanyadevi.blogspot.in
INTRODUCTION OF LINUX
LINUX is now a widely used operating system.
For a general user there can many reasons to prefer LINUX OS for everyday use.
It is free, it comes with many programs including an pc suit,
Security in LINUX is high,
It is an open source operating system, you can change the system as your wish; even
create your own private LINUX distribution.
For a scientific user we may add three more.
It is a very suitable system – LINUX can easily handle multiple tasks and run for a long
time without crashing.
It allows parallel programming, it turns ordinary PC to a LINUX Workstation.
BASIC OF LINUX
1. USER RIGHTS AND FILE PERMISSIONS:-
Linux is a multi-tasking operating system.
Thus the system can be used by more than one user in the same time and can perform
multiple operations simultaneously.
The user of the system has limited rights to use the system and these rights are given
by the system administrator who has access to the root account 1.
Each user has a private directory called home, in the system.
This is the main working directory of the user.
The users cannot reach and modify the others directories and files except for shared
ones.
On the other hand the root has unlimited rights.
It can reach and modify all the directories and files.
There three types of permissions:
Read permission(r),
Page 2
www.dharanyadevi.blogspot.in
Write permission (w),
Execute permission(x)
Permission determine:
Who can read the file or the directory?
Who can write the file or the directory?
Or who can execute the file
Should be noted that in LINUX any file can be executable and it is not necessary to
have an extension like,
To see the file permissions in the current directory.
The 1s command can be used with the -1 flag.
2. CREATING A USER ACCOUNT:-
The easiest way to create a user account is to use the graphical interface (GUI).
If the default home directory should not be changed.
Management of the user accounts and file permissions can only be done by the
root.
The most used shell and also the default is the bash shell, so it should also be left
unchanged.
A shell is a program that interprets the command you have given to the system.
A group can be added to the system with a in similar way to adding a user.
Several users can be grouped together to give them specific group rights.
LINUX COMMANDS
GENERAL PURPOSE COMMANDS
DIRECTORY HANDLING COMMANDS
FILE HANDLING COMMANDS
PATTERN SEARCHING COMMANDS
Page 3
www.dharanyadevi.blogspot.in
INPUT AND OUTPUT REDIRECTION, WILD CARD PATTERNS AND
SECURITY COMMANDS
PIPES AND FILTERS
Ex No: 8
Date : LINUX COMMANDS
1.To display the date as mm/dd/yy.
[admin@localhost ~]$date “+%d/%m/%y”
OUTPUT:
08/05/2011
2.To display the time as HH:MM:SS(A.M/P.M)
[admin@localhost ~]$date “+%r”
OUTPUT:
11:13:41 PM
3.To display the current day in abbreviated form.
[admin@localhost ~] $date ”+%A”
OUTPUT:
Friday
4.To display the calendar May 2009..
[admin@localhost ~]$cal jan 2006
OUTPUT:
January 2006
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
5.To display the complete name of the terminal.
[admin@localhost ~]$tput longname
Page 4
www.dharanyadevi.blogspot.in
OUTPUT:
[admin@localhost ~]
6.To display about your operating system.
[admin@localhost ~]$ uname –a
OUTPUT:
Linux Localhost.localdomain 2.6.18 – 53.e15
RESULT:
Thus the Linux command for general purpose command has been executed successfully.
II.DIRECTORY HANDLING COMMANDS
7.To view the name of the current directory
[admin@localhost ~]$pwd
OUTPUT:
/home/admin
8.To display all the files including hidden files.
[admin@localhost ~]$ls –a
OUTPUT:
area.sh a.txt com.sh fahren.sh lines powers.sh vowel.sh
arith.sh basic.sh echo leap.sh lines.sh stu.sh w.sh
ari.txt big.sh echo.sh libdata odd sumeven.sh
a.tct b.txt fact.sh lib.sh odd.sh three.sh
9.To display all the files with its inode number.
[admin@localhost ~]$ls –i
OUTPUT:
11633005 5as.c 11632958s.sh
10.To display all the files including files in subdirectories.
[admin@localhost ~]$ls –R
OUTPUT:
5as.c Ajjj.c
A.c a.out
11. To create a directory called stud. Change to the stud. Directory
[admin@localhost ~]$mkdir stud
Page 5
www.dharanyadevi.blogspot.in
[admin@localhost ~]$cd stud
OUTPUT:
[admin@localhost ~stud]
12.To verify whether you have actually changed to the stud directory, return to your original
directory
[admin@localhost ~]$pwd
[admin@localhost ~]$cd..
OUTPUT:
/home/admin/stud
[admin@localhost ~]
RESULT:
Thus the Linux command for directory handling commands has been executed successfully.
III. FILE HANDLING COMMANDS
12. To create a directory called marks inside the stud director
[admin@localhost ~]$cd stud
[admin@localhost ~]$mkdir marks
OUTPUT:
[admin@localhost ~stud]
13. To create a file in the stud directory called slaec and enter the data for the slaec file
[admin@localhost ~stud]$cat >slaec
1. Arun cse
2. Anna ece
3. Anbu eee
Note : [ after entering the data in the files press ctrl + z key ]
14. To change to the marks directory and copy the files slaec from stud directory to marks
directory. To verify whether the files has been copied to the mark directory.
Page 6
www.dharanyadevi.blogspot.in
[admin@localhost ~stud]$cd marks
[admin@localhost ~marks]$cp/admin/home/stud/slaec slaec2
[admin@localhost ~marks]$1s
Slaec1
15.To rename the file slaec to slaeccse. To verify the file name has been changed.
[admin@localhost ~stud]$ln slaec1 slaeccse
[admin@localhost ~stud]$ls
OUTPUT:
Slaeccse
16. To move the file slaec1 from marks directory to stud directory. To verify whether the file has
been moved to the stud directory.
[admin@localhost ~]$mv slaec1/ramdisk/home/knoppix/stud
[admin@localhost ~]$cd ..
[admin@localhost ~]$1s
[admin@localhost ~]$cd marks
[admin@localhost ~]$1s
OUTPUT:
/home/admin/stud/marks/
17. To display the total numbers of lines, words and characters in a file
[admin@localhost ~]$wc slaeccse
OUTPUT:
4 12 45 slaeccse
18. To display the list of files starting from our current directory
[admin@localhost ~]$find.-print
OUTPUT:
Page 7
www.dharanyadevi.blogspot.in
./slaeccse
RESULT :
Thus the linux file handling commands has been executed successfully.
IV. PATTERN SEARCHING COMMANDS
18. To display students under “cse”department in the file named slaeccse
[admin@localhost ~]$cat slaeccse
1. Arun cse
2. Anna ece
3. Anbu eee
[admin@localhost ~]$grep “cse” slaeccse
OUTPUT:
Arun cse
19. To display the words ending with a or b or c in file
[admin@localhost ~]$cat >raj
This is a new file.
There are two types of files newa, newb and newc.
The second is new1, new2, new3.
[admin@localhost ~]$grep “new[a-c]” raj
OUTPUT:
There are two types of files newa, newb, and newc.
20. To display the students details whose names are arun, anna and anbu in the file named
[admin@localhost ~]Slaeccse
[admin@localhost ~]$egrep “arun|anna|anbu” slaeccse
OUTPUT:
1. Arun cse
2. Anna ece
Page 8
www.dharanyadevi.blogspot.in
3. Anbu eee
21. To display the lines in the file raj to which has exact string “there are”
[admin@localhost ~]$fgrep “there are” raj
OUTPUT:
There are two types of files newa, newb, and newc.
RESULT:
Thus the Linux commands pattern searching commands has been executed successfully.
V. INPUT AND OUTPUT REDIRECTION, WILD CARD PATTERNS AND
SECURITY COMMANDS
22. To redirect the system calendar contents to the file a1.
[admin@localhost ~]$ cal > a1
[admin@localhost ~]$ cat a1
OUTPUT:
January 2006
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
23. To move the contents of the named a1 to a new file by using redirection operator
[admin@localhost ~]$ cat < a1 > a2
[admin@localhost ~]$cat a2
OUTPUT:
January 2006
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
24. List the files starting with „a‟
[admin@localhost ~]$ ls a*
Page 9
www.dharanyadevi.blogspot.in
OUTPUT:
a1 a2 a1
25. To find the file names ending with “1”
[admin@localhost ~]$ ls * 1
OUTPUT:
a1 raj1
26. To find the file starting with “vignesh” and then followed by any one character and with any
extension of one character
[admin@localhost ~]$ls vignesh.?
OUTPUT:
Vignesh2.c
27. To revoke the execute function for the file name vignesh and granting all permissions to file
named raj
[admin@localhost ~]$chmode 777 vignesh
[admin@localhost ~]$ls -1
OUTPUT:
1localhost admin 168 may 1 03:47 vignesh
28. To revoke the execute permission for the user of the file vignesh
[admin@localhost ~]$ -rwxrwxrwx 1 localhost localhost 168 jan 1 03:47 vignesh
[admin@localhost ~]$ chmod u-x vignesh
[admin@localhost ~]$ -rw-rwxrwx 1 localhost localhost 168 jan 1 03:47 vignesh
Page 10
www.dharanyadevi.blogspot.in
RESULT:
Thus the linux commands for input and output redirection, wildcard patterns and security
commands has been executed successfully.
VI PIPES AND FILTERS
29. To display the permissions of the group for the files whose name being withv.
[admin@localhost ~]$ ls –l v *|tr –s “”|cut –d “” –c5-7
OUTPUT:
r - -
r - -
r - -
r - -
30. To create a file department with some student details
[admin@localhost ~]$ cat > dept
OUTPUT:
4001 mohan 80
4002 raj 65
4003 mani 75
31. To display the names of the students in the file department in alphabetical order
[admin@localhost ~]$sort + 1f -2 dept
OUTPUT:
4003 mani 75
4001 mohan 80
4002 raj 65
32. To display the list of students who have scored marks between 50 and 80
[admin@localhost ~]$cat dept | grep “[5-7][0-9]
OUTPUT:
4002 raj 65
4003 mani 75
33. To display the first line of the file raj
[admin@localhost ~]$head -1 raj
OUTPUT:
Page 11
www.dharanyadevi.blogspot.in
This is a new file.
RESULT:
Thus the unix pipes and filters commands has been executed successfully.
SHELL
PROGRAMMING
SHELL INTRODUCTION
A shell is a command language interpreter. It is possible to write and run your own shell.
As shells are no different than any others programs as far as the system is concerned.
This manual deals with the details of one particular shell, called shell script.
There are various graphical interfaces available for linux the still is a very neat tool.
The shell is not just a collection of commands but a really good programming language.
It is often possible to perform simple tasks using these scripts without writing a program
in a language such as c, by using the shell to selectively run others programs.
Many shell variables have special meaning to the shell.
There three mainly shells with UNIX
BOURN SHELL,
KORN SHELL,
C SHELL.
The shell is very good for system administrator tasks.
FUNCTION OF SHELL
It accepts commands from the user and interpreter it.
It starts executing the appropriate executable file.
Page 12
www.dharanyadevi.blogspot.in
It request the kernel to carry out 1\0 transfer. Thus the shell acts middle man between the
kernel and the user.
The shell implements a number of substitutions where sequences indicated by Meta
character.
The shell function is unlike any other than the wildcard function in that it communication
with of the world outside make.
The shell function performs the same function that back quotes (“ ”) perform in most
shells : it does command expansion.
This means that it takes as an argument a shell command and evaluates to the output of
the command expansion.
This means that it takes as an argument a shell command and evaluates to the output of
the command.
The only processing make does on the result is to convert each newline to a single space.
If there is a trailing newline it will simply be removed.
The command run by calls to the shell function are run when the function calls are
expanded.
SHELL PROGRAMMING
Even though there are various graphical interfaces available for linux the shell still is
a very neat tool.
The shell is not just a collection of command but a really good programming
language.
You can automate a lot of task with it, the shell is very good for system administrator
task.
You can quickly try out if your ideas work which makes it very useful for simple
prototyping.
WORKING WITH SHELL PROGRAM
The shell may be used to read and execute commands contained in a file.
Page 13
www.dharanyadevi.blogspot.in
The shell to read commands to read commands from file. Such a file is called a command
procedure of shell procedure.
When you login, the shell is started by the system in your home directory and begins by
reading commands from a file.
A login shell executed after you login to the system. Login also in your home directory.
The file contains several commands to be executed by linux each time two login.
The first is set command which is interpreted directly by the shell.
Ex No: 2
Date : Factorial Of a Given Numbers
AIM:
To find the factorial of numbers using shell script
ALGORITHM:
STEP1: Start the program
STEP2: Give the numbers is real
STEP3: Fact =1 ; i =1
STEP4: Do the while loop [ $i – u $n ]
STEP5: Give the condition do fact = `expr $i \* $fact`
STEP6: Stop the program.
Page 14
www.dharanyadevi.blogspot.in
PROGRAM
echo "enter the number"
read n
fact=1
i=1
while [ $i -le $n ]
do
fact=`expr $i \* $fact`
i=`expr $i + 1`
done
echo "the factorial number of $n is $fact”
OUTPUT
[administrator@localhost ~] $ sh fact.sh
enter the number 4
the factorial number of 4 is 24
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 3
Date : Leap Year Or Not
AIM:
To find the given year is leap or not using shell script
ALGORITHM:
Page 15
www.dharanyadevi.blogspot.in
STEP1: Start the program
STEP2: Give the year
STEP3: Read the year
STEP4: If loop [expr $y % 4 –eq 0]
STEP5: If the remainder is zero the tear is leap or not leap year.
STEP6: Stop the program.
PROGRAM
echo "Enter The Year"
read y
if [ `expr $y % 2` -eq 0 ]
then
echo "the year is leap"
else
echo "the year is not leap"
fi
OUTPUT
[admin@localhost ~] $ sh leap.sh
Enter The Year 2008
the year is leap
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 4
Date : SUM OF EVEN NUMBERS
Page 16
www.dharanyadevi.blogspot.in
AIM:
To write a program to find the sum at even numbers upto n.
ALGORITHM:
STEP1: Start the program
STEP2: Enter the upper limit
STEP3: Give the limit and real n.
STEP4: Do the while loop [ $i – u $h ]
STEP5: Give the condition as run.
STEP6: Stop the program.
PROGRAM
echo " ENTER THE UPPER LIMIT "
read n
i=2
while [ $i -lt $n ]
do
sum=`expr $sum + $i `
i=`expr $i + 2 `
done
echo " SUM OF EVEN NUMBERS UPTO $n IS $sum"
OUTPUT
[administrator@localhost ~] $ sh sumeven.sh
ENTER THE UPPER LIMIT 10
SUM OF EVEN NUMBERS UPTO 10 IS 20
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 5
Page 17
www.dharanyadevi.blogspot.in
Date : SUM OF N NUMBERS
AIM:
To write a shell script to find the sum of n numbers
ALGORITHM:
STEP1: Start the program.
STEP2: Enter the upper limit.
STEP3: read n
STEP4: Do the while condition
Give condition [ $i –le $h ]
STEP5: Sum = expr $sum + $i
i = expr $i + 1
STEP6: Stop the program.
PROGRAM
echo "enter the upper limit "
read n
i=1
sum=0
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=` expr $i + 1`
done
echo "the sum of numbers is $sum"
OUTPUT
[administrator@localhost ~] $ sh upperlim.sh
enter the upper limit 5
the sum of numbers is 15
Page 18
www.dharanyadevi.blogspot.in
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 6
Date : MENU DRIVEN PROGRAM WITH UNIX COMMANDS
AIM:
To write a shell program to generate menu driver program with unix commands.
ALGORITHM:
STEP 1: Start the program.
STEP2: Read and display the (1) list of files (2) today‟s date (3) process status
(4) logged in users (5) display the current working directory (6) quit.
STEP 3: Read ch.
STEP 4:condition case “$ch”
STEP 5:condition satisfied to display the choice.
STEP 6:* invalid choice.
STEP 7: execute the program
STEP 8: stop the program.
PROGRAM
echo "menu"
echo "1.list of files"
echo "2.today's date"
echo "3.process status"
echo "4.logged in users"
echo "5.display the current working directory"
Page 19
www.dharanyadevi.blogspot.in
echo "6.Quit"
echo -n "enter our choice"
read ch
case "$ch" in
1) ls -1 ;;
2) date ;;
3) ps ;;
4) who ;;
5) pwd ;;
6) exit ;;
*) echo "invalid choice"
exit ;;
esac
OUTPUT:
Menu
1.List of files
2.Todays date.
3.Process status
4.Logged in users
5.Display the current working directory.
6.Quit
Enter your choice: 2
22/11/2010
Enter your choice: 5
localhost/admin
RESULT:
Page 20
www.dharanyadevi.blogspot.in
Thus the given shell script has been executed successfully.
Ex No: 7
Date : FIBONACCI SERIES
AIM:
To write a program to display the Fibonacci series.
ALGORITHM:
STEP1: Start the program.
STEP2: Read limit after enter limit.
STEP3: Do the while loop [$var –ie `expr $lim-2`]
STEP4: Give the condition.
do
n3= `expr $n1+$n2`
n1= `expr $n2`
n2= `expr $n3`
var= `expr $var+1`
STEP5: Stop the program.
PROGRAM
echo " ENTER THE LIMIT FOR FIBONACCI SERIES"
read
lim
n1=0
n2=1
var=0
Page 21
www.dharanyadevi.blogspot.in
echo "FIBONACCI SERIES IS "
echo "$n1"
echo "$n2"
while [ $var -lt `expr $lim - 2` ]
do
n3=`expr $n1 + $n2 `
n1=`expr $n2 `
n2=`expr $n3 `
var=`expr $var + 1
` echo "$n2"
done
OUTPUT
[administrator@localhost ~]$ sh fibonacci.sh
ENTER THE LIMIT FOR FIBONACCI SERIES 5
FIBONACCI SERIES IS
0
1
1
2
3
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 8
Date : SUM AND PRODUCT FOR THE GIVEN TWO NUMBERS
Page 22
www.dharanyadevi.blogspot.in
AIM :
To write a shell program to find the sum and products for the given two numbers
ALGORITHM :
STEP1: Start the program.
STEP2: Enter the value of a and b.
STEP3: read a, b;
STEP4: To find sum = product write sentences
Sum = ‟expr $a +$b‟
Product = „expr $a -$b‟
STEP5: To give the result write this
Sum = $sum\ +product -$prod
STEP6: Stop the program.
PROGRAM:
echo -n "enter the values for a & b"
read a b
sum=`expr $a + $b`
prod=`expr $a \* $b`
echo -n "sum=$sum, product=$prod"
OUTPUT
Enter the values for a and b
2 3
Sum = 5
Product = 6
Page 23
www.dharanyadevi.blogspot.in
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 9
Date : TEMPERATURE TO FAHRENHEIT.
AIM
To write a program to convert temperature in Fahrenheit to degree centigrade
ALGORITHM:
STEP1: Start the program
STEP2: Enter the temperature in Fahrenheit
STEP3: read f
STEP4: expr $f -32
expr $t /*5/9
STEP5: Stop the program.
PROGRAM
echo " ENTER THE TEMPERATURE IN FAHRENHEIT "
read f
t=`expr $f - 32 `
c=`expr $t \* 5 / 9`
echo "$c"
OUTPUT
[administrator@localhost ~] $ sh temp2far.sh
Page 24
www.dharanyadevi.blogspot.in
ENTER THE TEMPERATURE IN FARENHEIT 39
3
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 10
Date : VOWELS OF A STRING.
AIM:
To write a program to find the value of a string
ALGORITHM:
STEP1: Start the program
STEP2: read the string
STEP3: Do the while loop[$1 –gto]
STEP4: Give the condition
Do
Temp = „expr $str act –c$‟
Case $ temp in
STEP5: else condition is bring out
1 = „expr $l –I”
STEP6: Stop the program.
PROGRAM:
Page 25
www.dharanyadevi.blogspot.in
echo "enter the string"
read str
l=`expr length $str`
vowel=0
while [ $l -gt 0 ]
do
temp=`expr $str | cut -c $l`
case $temp in
a|A) vowel=`expr $vowel + 1`;;
e|E) vowel=`expr $vowel + 1`;;
i|I) vowel=`expr $vowel + 1`;;
o|O) vowel=`expr $vowel + 1`;;
u|U) vowel=`expr $vowel + 1`;;
esac
l=`expr $l - 1`
done
echo "the string has $vowel vowel"
OUTPUT
Enter the string
Operating System
The string has 5 vowels
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 11
Page 26
www.dharanyadevi.blogspot.in
Date : SUM OF DIGITS FOR GIVEN INTEGER
AIM:
To write a shell program to find the sum of digit for given integer.
ALGORITHM:
STEP1: Start the program
STEP2: Read n
STEP3: Assign sol = 0
STEP4: Condition while[$n –gto]
STEP5 : Compute a = „expr $n%10‟
Sd = „expr $sd + $a‟ and
n= „expr $n/10‟
STEP6: Print the sum of the digits in the given number is d.
STEP7: Stop the program.
PROGRAM:
echo "enter the number"
read n
sd=0
while [ $n -gt 0 ]
do
a=`expr $n % 10`
sd=`expr $sd + $a`
n=`expr $n / 10`
done
echo "the sum of digits in the given number is $sd"
OUTPUT
Page 27
www.dharanyadevi.blogspot.in
Enter the number: 1 2 3 4 5
The sum of digits for the given number is: 15
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 12
Date: BASIC SALARY
AIM:
To write the program to find the basic salary.
ALGORITHM:
STEP1: Start the program.
STEP2: Enter the basic salary.
STEP3: Real b.
STEP4: Give the condition.
da=`expr $b \* 10/100`
hr=`expr $b \* 20/100`
gr=`expr $b + $da + $h`
STEP5: Exit the program salary=$gr.
STEP6: Stop the program.
PROGRAM
echo " ENTER THE BASIC SALARY OF THE EMPLOYEE "
read b
da=`expr $b \* 10 / 100 `
Page 28
www.dharanyadevi.blogspot.in
hr=`expr $b \* 20 / 100 `
gr=`expr $b + $da + $hr `
echo " GROSS SALARY = $gr "
OUTPUT
[administrator@localhost ~] $ sh bsal.sh
ENTER THE BASIC SALARY OF THE EMPLOYEE 900
GROSS SALARY = 1170
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 13
Date: COMBINATION OF 123
AIM:
To write a shell script program to print the combination of 123.
ALGORITHM:
STEP1: Start the program.
STEP2: Give the three condition for i&j&k in 123.
STEP3: Echo “$i $j $k”
STEP4: Stop the program.
PROGRAM
for i in 1 2 3
Page 29
www.dharanyadevi.blogspot.in
do
for j in 1 2 3
do
for k in 1 2 3
do
echo $i $j $k
done
done
done
OUTPUT
[administrator@localhost ~] $ sh comb.sh
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
Page 30
www.dharanyadevi.blogspot.in
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 14
Date: POWER OF A NUMBER
AIM:
To write a shell script to find the power of a number.
AIGORITHM:
STEP1: Stort the program.
STEP2: To find xn enter X=N value.
STEP3: Do the while loop [$i-u $h].
STEP4: Give the condition
X=expr $ x\* $ temp
I=expr $ i+1
STEP5: Stop the program.
PROGRAM
echo "Enter the value of X & N to find X^N "
read x n
temp=`expr $x` i=1
while [ $i -lt $n ]
Page 31
www.dharanyadevi.blogspot.in
do
x=`expr $x \* $temp`
i=`expr $i + 1`
done
echo "RESULT IS $x"
OUTPUT
[administrator@localhost ~] $ sh power.sh
Enter the value of X & N to find X^N 8 2
RESULT IS 64
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 15
Date: NUMBER OF CHARACTER LINES IN A FILE
AIM:
To write a program to find the number of character lines,words in a file.
ALGORITHA:
STEP1: Start program.
STEP2: Enter the file name.
STEP3: Give the if condition
If (file $file) then
STEP4: Bring the else condition.
STEP5: Stop the program.
Page 32
www.dharanyadevi.blogspot.in
PROGRAM:
echo "enter the file name"
read file
if ( file $file )then
echo "file exists"
echo "no of lines"
wc -l $file
echo "no of characters"
wc -c $file
echo "no of words"
wc -w $file
else echo "file does not exists"
fi
OUTPUT
[administrator@localhost ~] $ sh check.sh
enter the file name area.sh
file exists.
no of lines 11
no of characters 36
no of words 150
[administrator@localhost ~] $ sh check.sh
enter the file name focus.sh
file does not exists.
RESULT:
Thus the given shell script has been executed successfully.
Ex No: 16
Page 33
www.dharanyadevi.blogspot.in
Date : CHECK WHETHER THE NUMBER IS ZERO OR
POSITIVE OR NEGATIVE
AIM:
To write a program to check the given number is positive or negative.
ALGORITHA:
STEP1: Start program.
STEP2: Enter the integer.
STEP3: Give the if condition
if [ $n -eq 0 ] then
STEP4: Condition satisfied the given number is zero
STEP5: Bring the else if condition
elif [ $n -gt 0 ]
STEP6:If the given numer is greater than zero print positive or else negative
STEP7: Stop the program.
PROGRAM
echo "enter any number"
read n
if [ $n -eq 0 ]
then
echo "number is zero "
elif [ $n -gt 0 ]
then
echo "number is positive"
else
echo "number is negative"
fi
Page 34
www.dharanyadevi.blogspot.in
OUTPUT
[administrator@localhost ~]$ sh posneg.sh
enter any number 4
number is positive
[administrator@localhost ~]$ sh posneg.sh
enter any number -98
number is negative
[administrator@localhost ~]$ sh posneg.sh
enter any number 0
number is zero
RESULT:
Thus the given shell script has been executed successfully.
C
PROGRAMMING
Ex.No: 17
DATE: DISPLAY THE CONTENTS OF A FILE
AIM:
To write the C program to display the contents of a file
ALGORITHM:
Page 35
www.dharanyadevi.blogspot.in
STEP 1:start
STEP 2:write number of command line arguments
STEP 3:open a file in a read mode
STEP 4: display the content of the file
STEP 5: stop
PROGRAM:
#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fptr;
char ch;
int i;
printf("no of arguments %d \n",argc);
for(i=1;i<argc;i++)
{
fptr=fopen(argv[i],"r");
ch=getc(fptr);
while(ch!=EOF)
{
putchar(ch);
ch=getc(fptr);
}
}
}
Page 36
www.dharanyadevi.blogspot.in
OUTPUT
[admin@localhost ~]$ cc disp.c
[admin@localhost ~]$ ./a.out one.txt
no of arguments 2
This is linux laboratory.
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 18
DATE: COPY THE CONTENTS OF ONE FILE TO ANOTHER
FILE
AIM:
To write the C program to copy the contents of a file to another file.
ALGORITHM:
STEP 1:start
STEP 2: check no of command line arguments is equal to 3 if not exit
STEP 3:if so, open two files one in read mode and other in write mode
STEP 4:copy the contents of file in read mode to file in write mode
STEP 5:stop
PROGRAM
Page 37
www.dharanyadevi.blogspot.in
#include<stdio.h>
int main( int argc,char *argv[ ] )
{
FILE *fptr1,*fptr2;
char ch;
int i;
if(argc>3)
{
printf("too many arguments");
return(0);
}
else if(argc<2)
{
printf("\n parameter required");
return(0);
}
else
{
fptr1=fopen(argv[1],"r+");
fptr2=fopen(argv[2],"w+");
ch=getc(fptr1);
while(ch!=EOF)
{
putc(ch,fptr2);
ch=getc(fptr1);
}
printf(“the contents of %s is successfully copied to %s \n”,argv[1],argv[2]);
}
}
Page 38
www.dharanyadevi.blogspot.in
OUTPUT
[admin@localhost ~]$ cc copy.c
[admin@localhost ~]$ ./a.out one.c two.c
the contents of one.c is successfully copied to two.c
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 19
DATE: TO COUNT THE CHARACTERS AND LINES IN A
FILE
AIM:
To write the C program to count the characters and lines in a file.
ALGORITHM:
STEP 1: Start the program
STEP 2: Declare a file pointer fp.
STEP 3: Assign nlines=0, and nc=0.
STEP 4: Read the filename.
STEP 5: Open the file –filename under read mode assigned to fp.
STEP 6: If (fp1 == NULL) then print cannot open filename for reading and
then go to step 15.
Page 39
www.dharanyadevi.blogspot.in
STEP 7: Assign c=getc(fp)
STEP 8: Repeat the steps 9 to 10 until while (c!=EOF)
STEP 9: if (c ==‟\n‟) then nlines=nlines+1 else nc=nc+1.
STEP 10: Assign c=getc(fp)
STEP 11: Close the file-fp
STEP 12: If (nc!=0) then step 13 else step 14.
STEP 13: Print the total no of characters and total of lines in the file then goto
step 15.
STEP 14: Print the file is empty
STEP 15: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE *fp;
int c,nc,nlines;
char filename[40];
nlines=0;
nc=0;
clrscr();
printf("\n\n\t\t To count the characters and lines in a given file \n");
printf("\n\n Enter a file name: ");
gets(filename);
fp=fopen(filename,"r");
Page 40
www.dharanyadevi.blogspot.in
if(fp==NULL)
{
printf("\n\n Cannot open %s for reading \n", filename);
getch();
exit(1);
}
c=getc(fp);
while(c!=EOF)
{
if(c=='\n')
nlines++;
else
nc++;
c=getc(fp);
}
fclose(fp);
if(nc!=0)
{
printf("\n\n There are %d characters in %s \n",nc,filename);
printf("\n\n There are %d lines %n",nlines);
}
else
printf("\n\n The File %s is empty \n",filename);
getch();
}
OUTPUT
[admin@localhost ~]$ cc count.c
Page 41
www.dharanyadevi.blogspot.in
[admin@localhost ~]$ ./a.out
To count the characters and lines in a given file
Enter the file name:strcopy.c
There are 345 characters in strcopy.c
There are 25 lines
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 20
DATE: FACTORIAL OF A GIVEN NUMBER
AIM:
To write a C program to find the factorial of a given number using recursive
function.
ALGORITHM
Main() program
STEP 1: Start the program
STEP 2: Read the value of num.
STEP 3: Assign fact1=call fact(num) function
STEP 4: print fact1
STEP 5: Stop
Fact() function
Page 42
www.dharanyadevi.blogspot.in
STEP 1: Start the program
STEP 2: if(n==1||n==0) then step 3 else step 4
STEP 3: return (1)
STEP 4: return (n*fact(n-1))
PROGARM
#include<stdio.h>
long fact(int n);
main()
{
int num;
long fact1;
printf("\n Enter a number");
scanf("%d",&num);
fact1=fact(num);
printf("The funtion of %d is %d",num,fact1);
}
long fact(int n)
{
if(n==1||n==0)
return(1);
else
return(n*fact(n-1));
getch();
}
OUTPUT
Page 43
www.dharanyadevi.blogspot.in
[admin@localhost ~]$ cc fact.c
[admin@localhost ~]$ ./a.out
Enter a number: 4
The factorial of 4 is 24
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 21
DATE: FIBONACCI SERIES
AIM:
To write a C program to print Fibonacci Series using recursive function.
ALGORITHM
main() program
STEP 1: Start the program
STEP 2: Read the value of n.
STEP 3: call fib(0, 1, n) function
STEP 4: Stop
fib() function
STEP 1: Start the program
Page 44
www.dharanyadevi.blogspot.in
STEP 2: if(num!=0) then step 3 else step 5
STEP 3: print n1
STEP 4: call fib(n2, n2+n1, num-1)
STEP 5: return
PROGRAM
#include<stdio.h>
void fib(unsigned int n1,unsigned int n2,unsigned int num);
main()
{
int n;
printf("\n Enter the limit :");
scanf("%d",&n); fib(0,1,n);
}
void fib(unsigned int n1,unsigned int n2,unsigned int num)
{
if(num!=0)
{
printf("%u \n",n1); fib(n2,n2+n1,num-1);
}
}
OUTPUT
[admin@localhost ~]$ cc fibo.c
[admin@localhost ~]$ ./a.out
Enter the limit: 8
0
Page 45
www.dharanyadevi.blogspot.in
1
1
2
3
5
8
13
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 22
DATE: IMPLEMENTATION OF LS COMMAND
AIM:
To write a C program to implement ls Command
ALGORITHM:
STEP 1: Read the file to be displayed through command Line
STEP 2: Check whether the argument matches with file available in the current directory
STEP 3: If no matches display error
STEP 4: Else display the list of files which matches the given input by utilizing the
system
command
Page 46
www.dharanyadevi.blogspot.in
PROGRAM
#include<stdio.h>
main(int argc,char *argv[])
{
FILE *fp;
int i;
for (i=0;i<argc;i++)
{
if((fp=fopen(argv[i],"r"))==(FILE*)NULL)
{
perror(argv[i]);
}
else
printf("%s file is present",argv[i]);
}
exit(0);
}
OUTPUT
[admin@localhost ~]$ cc ls.c
[admin@localhost ~]$ ./a.out process.c
file is present
process.c file is present
[admin@localhost ~]$ cc ls.c
[admin@localhost ~]$ ./a.out area.sh
a.sh: No such file or directory
RESULT:
Thus the program has been executed successfully and the output is verified.
Page 47
www.dharanyadevi.blogspot.in
Ex.No: 23
DATE: LINEAR SEARCH
AIM:
To write a C program to perform linear search using function.
ALGORITHM:
main() program
STEP 1 : Start the program.
STEP 2 : Read the value of n.
STEP 3 : Assign i=0.
STEP 4 : repeat the step 5 to 6 until i < n.
STEP 5 : Read a[i].
STEP 6 : Compute i = i + 1.
STEP 7 : Read the value of x.
STEP 8 : Call linear(a,n,x) function.
STEP 9 : Stop.
linear() function
STEP 1 : Start the program.
STEP 2 : Assign i=0 and flag=0.
STEP 3 : Repeat the step 4 to 5 until i < n.
STEP 4 : if(x==a[i]) then flag=1 and exit from loop.
STEP 5 : Compute i = i + 1.
Page 48
www.dharanyadevi.blogspot.in
STEP 6 : if ( flag==1) then print element found else print element not found.
STEP 7 : Return to the main program.
PROGRAM
#include<stdio.h>
void linear(int [], int, int);
main()
{
int i,n,x,a[20];
printf("\n Enter the limit : ");
scanf("%d",&n);
printf("\n Enter the Elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the elements to be searched :");
scanf("%d",&x);
linear(a,n,x);
}
void linear(int a[],int n,int x)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(x==a[i])
{
flag=1; break;
}
}
Page 49
www.dharanyadevi.blogspot.in
if(flag==1)
printf("\n The element %d is found in the position %d",a[i],i+1);
else
printf("\n The element is not found in the list");
}
OUTPUT
[admin@localhost ~]$ cc linear.c
[admin@localhost ~]$ ./a.out
Enter the limit : 5
Enter the elements : 27 39 63 18 12
Enter the elements to be searched : 63
The element 14 is found in the position 2
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 24
DATE: BINARY SEARCH
AIM:
To write a C program to perform binary search using function.
ALGORITHM:
STEP 1 : Start the program.
STEP 2 : Read the value of n.
Page 50
www.dharanyadevi.blogspot.in
STEP 3 : Assign i =0.
STEP 4 : Repeat the step 5 to 6 until i < n.
STEP 5 : Read a[i].
STEP 6 : Compute i = i + 1.
STEP 7 : Read the value of s.
STEP 8 : Call binary(q,n,s) function.
STEP 9 : Stop.
binary() function
STEP 1 : Start the program.
STEP 2 : Assign f=0, l=n-1 and flag=0.
STEP 3 : Repeat the step 4 to 6 until f <= n.
STEP 4 : Compute m=(f+1)/2.
STEP 5 : if(s < a[m]) then l=m-1 else step 6.
STEP 6 : if(s > a[m]) then f=m+1 else flag=1 exit from loop.
STEP 7 : if ( flag==1) then print element found else print element not found.
PROGRAM
#include<stdio.h>
void binary(int [], int, int);
main()
{
int s,i,n,a[100];
Page 51
www.dharanyadevi.blogspot.in
printf("\n\t\t To search for an element using binary search \n");
printf("\n Enter the no of elements : ");
scanf("%d",&n);
printf("\n Enter the Elements :");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the element to be searched :");
scanf("%d",&s);
binary(a,n,s);
}
void binary(int a[],int n,int s)
{
int f,l,m,flag=0;
f=0;
l=n-1;
while(f<=1)
{
m=(f+l)/2;
if(s<a[m])
l=m-1;
else if(s>a[m])
f=m+1; else
{
flag=1;
break;
}
}
if(flag==1)
Page 52
www.dharanyadevi.blogspot.in
printf("\n\n %d is found \n",s);
else
printf("\n\n %d is not found \n",s);
}
OUTPUT
[admin@localhost ~]$ cc binary.c
[admin@localhost ~]$ ./a.out
To search for an element using binary search
Enter the no. of elements : 5
Enter the elements : 65 85 25 95 45
Enter the element to be searched: 95
95 is found.
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 25
DATE: STRING CONCATENATION
AIM:
To write a C program to concatenate two strings.
ALGORITHM:
STEP 1 : Start the program.
STEP 2 : Declare the character pointer variables as s1, s2, and s3.
Page 53
www.dharanyadevi.blogspot.in
STEP 3 : Allocate memory for s1, s2, and s3 character pointer variables using
malloc function.
STEP 4 : Read the string s1 and s2.
STEP 5 : Assign i=0.
STEP 6 : Repeat the steps 7 and 8 until ((c=*(s1+i))!=‟\0‟).
STEP 7 : Assign s3[i]=0.
STEP 8 : Compute i=i+1.
Step 9 : Assign k=0.
STEP 10 : Repeat the steps 11 to 12 until ((c=*(s1+i))!=‟\0‟).
STEP 11 : Assign s3[i+k]=0.
STEP 12 : Compute k=k+1.
STEP 13 : Assign s3[i+k]=‟\0‟.
STEP 14 : Print the concatenated string s3.
STEP 15 : Stop.
PROGRAM
#include<stdio.h>
#include<malloc.h>
#define length 20
main()
{
char *s1,*s2,*s3,c;
Page 54
www.dharanyadevi.blogspot.in
int i,k;
s1=(char *)malloc(length *sizeof(char));
s2=(char *)malloc(length *sizeof(char));
s3=(char *)malloc(2*length *sizeof(char));
printf("\n Enter the String1 : ");
scanf("%s",s1);
printf("\n Enter the String2 : ");
scanf("%s",s2);
i=0;
while((c=*(s1+i)) != '\0')
{
s3[i]=c; i++;
}
k=0;
while((c=*(s2+k)) != '\0')
{
s3[i+k]=c;
k++;
}
s3[i+k]='\0';
printf("\n Concatenated String is %s ",s3);
getch();
}
OUTPUT
[admin@localhost ~]$ cc strcon.c
[admin@localhost ~]$ ./a.out
Page 55
www.dharanyadevi.blogspot.in
Enter the String1 : Raja
Enter the String2 : Sekaran
Concatenated String is : RajaSekaran
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 26
DATE: STRING COPY
AIM:
To write a C program to copy the contents of one string to another.
ALGORITHM:
STEP 1 : Start the program.
STEP 2 : Declare the character pointer variables as s1 and s2.
STEP 3 : Allocate memory allocation for s1 and s2 using malloc() function.
STEP 4 : Read s1.
STEP 5 : Assign i=0.
STEP 6 : Repeat the steps 7 and 8 until ((c=*(s1+i))!=‟\0‟).
STEP 7 : Assign s2[i]=0.
Page 56
www.dharanyadevi.blogspot.in
STEP 8 : Compute i=i+1.
STEP 9 : Assign s2[i]=‟\0‟.
STEP 10 : Print s2.
STEP 11 : Stop.
PROGRAM
#include<stdio.h>
#include<malloc.h>
#define length 30
main()
{
char *s1,*s2,c;
int i;
s1=(char *)malloc(length *sizeof(char));
s2=(char *)malloc(length *sizeof(char));
printf("\n Enter the String1 : ");
scanf("%s",s1);
printf("\n The entered String1 is %s",s1);
i=0;
while((c=*(s1+i)) != '\0')
{
s2[i]=c;
i++;
}
s2[i]='\0';
printf("\n The Copied String1 in String2 is %s ",s2);
getch();
}
Page 57
www.dharanyadevi.blogspot.in
OUTPUT
[admin@localhost ~]$ cc strcpy.c
[admin@localhost ~]$ ./a.out
Enter the string1 : ASHOK
The entered string1 is ASHOK
The copied String1 in String2 is ASHOK
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 27
DATE: FORK SYSTEM CALL
AIM:
To write a C program to view the main value using the fork function.
ALGORITHM:
STEP 1:start
STEP 2:initialize the variable fork, value.
STEP 3:when the value is equal to the fork, print the main value as value.
STEP 4:stop
PROGRAM
#include<stdio.h>
Page 58
www.dharanyadevi.blogspot.in
int main(void)
{
int fork(void),value;
value=fork();
printf("main:value =%d\n",value);
return 0;
}
OUTPUT
[admin@localhost ~]$ cc fork.c
[admin@localhost ~]$ ./a.out
main:value =0
main:value =5545
RESULT:
Thus the program has been executed successfully and the output is verified.
Ex.No: 28
DATE: WAIT SYSTEM CALL
AIM:
To write a C program to display the waiting process state.
ALGORITHM:
STEP 1: start
STEP 2: initialize the variable pid,status,exitch
Page 59
www.dharanyadevi.blogspot.in
STEP 3: if the pid is equal to fork and it satisfy the condition that it equal
to -1,then print the error message
STEP 4: when pid is equal to other print the child process else print the
parent process
STEP 5: when exit is equal to status and it is equal to -1 then print perror is in
wait else parent existing
STEP 6:stop
PROGRAM
#include<stdio.h>
#include<unistd.h>
int main(void)
{
int pid,status,exitch;
if((pid=fork())==-1)
{
perror("error");
return (0);
}
if(pid==0)
{
sleep(1);
printf("child process");
return (0);
}
else
{
printf("parent process\n");
if((exitch=wait(&status))==-1)
{
Page 60
www.dharanyadevi.blogspot.in
perror("during wait()");
return(0);
}
printf("parent existing\n");
return (0);
}
}
OUTPUT
[admin@localhost ~]$ cc wait.c
[admin@localhost ~]$ ./a.out
parent process
child process
parent existing
RESULT:
Thus the program has been executed successfully and the output is verified.