1
Unix & Shell Programming
Unix & Shell Programing
Session - 2009 -10
Submitted by: Submitted to:
Name: Prabhat Kumar Mr. Ashu Verma
Class: BCA IInd Concerned Lect. in Operating system
Class Roll no. 06
Examination Roll no………………... Ashoka college of Computer Education
Ashoka College of Computer Education
2
Unix & Shell Programming
Certificate
This is certified that the
practical file belongs to
Prabhat Kumar, Class roll
no. 06 and Examination
Roll no…………of session
2009-10 and has
completed all the
practical in the college
computer lab under our
guidance and his
behavior in the college
is good.
Ashoka College of Computer Education
3
Unix & Shell Programming
Teacher Incharge
Principal
Index
Ashoka College of Computer Education
4
Unix & Shell Programming
S. Name of Practical Page Remarks
no. no.
1 Unix, Types of Unix. 1
2 Kernel, Shell, Programs. 2
3 Unix Commands. 3 - 12
4 WAP to find addition of two numbers. 13
5 WAP to find greater between two 14
numbers.
6 WAP to find greatest between three 15
numbers.
7 WAP to find factorial of a number. 16
8 WAP to generate Fibonacci series. 17
9 WAP to enter the marks and obtain 18
grade.
10 WAP to find n natural numbers using 19
loop.
11 WAP to generate n natural number in 20
inverse series.
12 WAP to accept number from user and 21
display it.
13 WAP whether the number is even or 22
odd.
14 WAP to print a table of any number. 23
15 WAP to calculate the T.A , H.R.A and 24
D.A of any employee.
16 WAP to show state of first character 25
of word.
17 WAP to print the contents of a table. 26
18 WAP to enter any character from 27
user.
Ashoka College of Computer Education
5
Unix & Shell Programming
UNIX
UNIX is an operating system which was first
developed in the 1960s, by Ken Thompson. It is a
popular operating system available for micro and
main frame for scientific and commercial use. By
operating system, we mean the suite of programs
which make the computer work. It is a stable, multi-
user, multi-tasking system for servers, desktops and
laptops. UNIX is designed for effectiveness of
Application Software Development, System Software
Development etc.
UNIX systems also have a graphical user interface
(GUI) similar to Microsoft Windows which provides an
easy to use environment. However, knowledge of
UNIX is required for operations which aren't covered
by a graphical program, or for when there is no
windows interface available, for example, in a telnet
session. In 1974, UNIX was licensed to Universities
for educational purposes
Types of UNIX
There are many different versions of UNIX, although
they share common similarities. The most popular
varieties of UNIX are Sun Solaris, GNU/Linux, and
MacOS X.
The UNIX operating system is made up of three
parts; the kernel, the shell and the programs.
Ashoka College of Computer Education
6
Unix & Shell Programming
Kernel
The kernel of UNIX is the hub of the operating
system: it allocates time and memory to programs
and handles the file store and communications in
response to system calls.
Shell
The shell acts as an interface between the user and
the kernel. When a user logs in, the login program
checks the username and password, and then starts
another program called the shell. The shell is a
command line interpreter (CLI). It interprets the
commands the user types in and arranges for them
to be carried out. The commands are themselves
programs: when they terminate, the shell gives the
user another prompt.
Programs
A program is the collection of instructions which are
collectively arranged and organized for execution for
performing certain operations. UNIX is made up of
no. of programs, shell and kernel.
UNIX COMMANDS
ls command: - This command lists your files.
Syntax: - # ls
Output: -
Ram Dushyant bharat kittu
Prabhu rakesh vikas Kishore
ls-l command :- Lists your files in ‘long format’, which contains lots
of useful information, e.g. the exact size of file ,who owns the file and
who has the right to look at it, and when it was last modified.
Syntax:- #ls -l
Ashoka College of Computer Education
7
Unix & Shell Programming
Output:-
akhil
Rohit
pankaj
sunil
raka
ls -a command :- Lists all files, including the ones whose filenames
begin in a. there are any more options. For example to list files by size,
by date, recursively etc.
Syntax: - # ls [a]*
Output:-
Akhil Aryan Aniket Atinder
Is [a e i o u]*:- This command is used to open the file which started
with vowels.
Syntax :- # Is [a e i o u]*
Output:-
Akhil Ankush Irani
Uma orkut umang
Is [0-9]*:- This command is used to open the file which starts with
number.
Syntax:- # Is[0-9]*
Output:-
786 2bca 1bca 86
UNAME command: - This command is used to know about the
operating system on which
we work.
Syntax ::--- #uname
Output: - Linux
Uname – r command: - This command is used to know about the
version of operating system.
Syntax: - #uname-r
Output:-
Ashoka College of Computer Education
8
Unix & Shell Programming
2.6.18-53.el5
ls – fx command:- This command is used to open the which list of
executable file.
Syntax:- #ls-fx
Output: -
arap.exe window.exe bcap.exe
maon.exe rifgt.exe prog.exe
BC command: - This command is used to calculator.
Syntax: - # bc
Output:-
10+21
= 21
^D
xcalc command :- This command is used to display the calculator
graphically.
Syntax :- # xcalc
mkdir command :- This command is used to make directory.
Syntax : - #mkdir filename.
Example : - #mkdir dushyant
Remove command :- This command is used to remove the file.
Syntax: - #rm filename
Example : - #rm prabhu
Output ::-
remove regular file ‘prabhu’? Y
CAT > command: - It is used to create a file.
Syntax: - #cat > filename
Example: - #cat > khan
Output: -
My name is khan. Shahrukh khan
^d {Ctrl + D}
CAT command: - It is also used to view a file.
Syntax: - #cat filename
Example: - #cat khan
Output :- My name is khan. Shahrukh khan
Ashoka College of Computer Education
9
Unix & Shell Programming
who command : - This command is used to display the user name,
terminal no. and login time
Syntax: - #who
Output: -
romy .tnfs 44.03
atin .tpgs 23.90
pwd command: - This command is used to know about the present
working directories.
Syntax: - #pwd
Output: -
Root prabhu
tput clear command ::--- This command is used to clear the screen.
Syntax: - #tput clear
man command ::--- This command is used when we need help then
we use man command.
Syntax: - #man ls
ls -axf command: - This command is used to view the hidden files.
Syntax: - #ls-axf
Output: - prabhat Kishore
cd command: - This command is used to change directories.
Syntax: - #cd filename
Example: - #cd prabhat
ps command: - This command is used to see all the processes.
Syntax: - #ps
Output: -
PID TTY TIME CMD
9216 pts/0 00.00.00 bash
9824 pts/0 00.00.00 ps
wc command: - This command is used to display the lines, word and
character in a file.
Syntax: - #wc file name
Example: - #wc dushyant
Output: - 2 23 10:dushyant
Ashoka College of Computer Education
10
Unix & Shell Programming
Is *.c command: - This command is used to display all the files with
extension .c .
Syntax: - #ls *.c
Output: - Dushyant prabhat amar
Touch command: - This command is used to create the empty file.
Syntax ::--- #touch
Example ::--- #touch dushyant
Output ::--- Empty file
cmp command: - This command is used to compare the files.
Syntax: - #cmp filename1 filename2
Example: - #cmp Kishore prabhu
Output: -
Kishore prabhu: same: line=15
Date command: - This command is used to see the current date.
Syntax: - #date
Output: - 26-jan-2010
cal command: - This command is used to display the current
calendar.
Syntax: - #cal
Output: - January
Sun mon tue wed thu fri
sat
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 31
tty command: - This command is used to know about the terminal
number.
Syntax: - tty
Output: -
/device/pts/0
tty command: - This command is used for setting the terminal.
Syntax: - #stty
Output: - Speed=54630 baud;
Line=0;
Eol=m_^?
cat > command: - This command is used to create the hidden files.
Ashoka College of Computer Education
11
Unix & Shell Programming
Syntax: - #cat > .filename
Example: - #cat > prabhu
move command: - This command is used to move the contents of one
file to another file.
Syntax: - #mv source destination
Example: - #mv Kishore prabhat
Output: - File moved.
cp command: - This command is used to copy the contents of one file
to another file.
Syntax: - #cp source file destination file
Example: - #cp Kishore prabhu
Output: - File copied.
Time command: - This command used to show the cumulative
amount of c.p.u time used.
Syntax: - #time
Output: - 12:30 am
ps-a command: - This command is used to list all processes
belonging to any terminal .
Syntax: - #ps-a
Output: - PID TTY TIME CMD
2912 pts/0 00.00.00 cat
3241 pts/0 00.00.00 cat
ps-e command: - This command used to display the list of everything
i.e. all processes.
Syntax: - #ps-e
ps-f command: - This command is used fully, instead of brief, listing :
gives additional detail.
Syntax: - #ps-f
ps-I command: - This command is used for long listing ,gives more
detail, including priority, nice value etc.
Syntax: - #ps-l
kill command: - This command can be used by the owner of a process
to terminate its execution.
Syntax: - #kill process number
Example: - #kill 43
Ashoka College of Computer Education
12
Unix & Shell Programming
Output: - Process killed.
sleep command: - This command delays the execution for a specified
no. of seconds it can be used to start a command after a
specified pause.
Syntax: - #sleep 10; ls
history command: - This command is used to know about the details
of whole commands used by the user after login.
Syntax: - #history
Output: - 24 cat > prabhu
25 Is
grep command: - This command is used to searches for specified
patterns of characters with in its input. If the
pattern is found it display the line containing the patterns.
Syntax: - # grep ‘on’ cities
find command: - This command is useful facility for operational users,
it enables files conforming to some
name or attribute description to be located within a directory tree
structure.
Syntax: - # find
chmod command: - This command changes the permission
information associated with a file.
Every file (including directories, which Unix treats
as files) on a Unix system is stored with records indicating who has
permission to read, write or execute the file, abbreviated as r, w and x.
These permissions are broken down for three categories of user: first,
the owner of the file; second, a group with which both the user and the
file may be associated; and third, and all other users. These categories
are abbreviated as u for owner (or user), e.g. for group and o for other.
Syntax: - chmod u,o,g+prabhu
To allow yourself to execute a file that you own named my
file, enter:
Syntax: - chmod u+x rakesh
To allow anyone who has access to the directory in which my
file is stored to read or execute my file, enter:
Ashoka College of Computer Education
13
Unix & Shell Programming
Syntax: - chmod o + rx roop
vi command: - This command starts the vi text editor to edit a file
named my file in the
current directory, enter
Syntax: - vi prabhat
Rename command: - You can also use the mv command to rename
and to move
directories. When you type the command
Syntax: - mv prabhu prabhat
The directory called prabhu will be given the
new name prabhat as long as a directory called prabhat already
existed before the mv command was issued, the result would be to put
the directory prabhu and its files into the directory prabhat.
Shell Scripting
➢ WAP to find addition of two numbers: -
#vi prabhu
Echo “enter first number”
Read $a
Echo “enter second number”
Read $b
Echo ‘expr $a + $b’
Ashoka College of Computer Education
14
Unix & Shell Programming
:wq
#sh prabhu
Output
Enter first number 2
Enter second number 1
3
➢ WAP to find greater between two numbers:-
# vi prabhu
Echo “enter first number”
Read $a
Echo “enter second number”
Read $b
If $a-gt $b
Then
Echo “a is greater”
Else “b is greater”
Fi
:wq
#sh prabhu
Ashoka College of Computer Education
15
Unix & Shell Programming
Output: -
Enter the first number 31
Enter the second number 13
a is greater
➢ WAP to find greatest between three numbers:-
# vi honey
Echo “enter first number”
Read $a
Echo “enter second number”
Read $b
Echo “enter third number”
Read $c
If [$a-gt $b]-a [$a-gt $c]
Echo “a is greater “
Else if [$b-gt $a]-a [$b-gt $c]
Echo “b is greater”
Else
Echo “c is greater”
fi
:wq
#sh honey
Output:-
Enter first number 8
Enter second number 5
Enter third number 9
c is greater
Ashoka College of Computer Education
16
Unix & Shell Programming
➢ WAP to find factorial of a number:-
#vi honey
Echo “enter a number”
Read $a
If [$n-lt0]
Echo “no factorial of negative number.”
Else if[$n==0]
Echo “factorial of zero is 1.”
Else
While[$n-gt1]
Do
fact=expr $fact/*$n
n=$n – 1
Done
Echo “factorial is fact” $fact
fi
:wq
#sh honey
Output: -
Enter a number 0
factorial of zero is 1.
Enter a number 5
factorial is 120
➢ WAP to generate Fibonacci series :-
#vi prabhu
Echo “enter the limit”
Read limit
a=0
b=1
c=0
while($c – lt $limit)
Ashoka College of Computer Education
17
Unix & Shell Programming
do
c=’expr $a+$b’
echo “$c”
a=”$b”
b=”$c”
done
:wq
#sh prabhu
Output: -
Enter the limit 10
0 1 1 2 3 5 8 13 21 34
➢ WAP to enter the marks and obtain grade:-
# vi prabhu
Echo ”enter the marks of five subjects”
Read “m1 m2 m3 m4 m5”
Per=’expr\($m1+$m2+$m3+$m4+$m5|)/*5’
If [per-ge75]
Then
Echo “Distinction”
fi
if[per-ge60-a le74]
then
echo “grade A”
fi
if[per-ge50-a le59]
then
echo “grade B”
fi
if[per-ge33-a le49]
Ashoka College of Computer Education
18
Unix & Shell Programming
then
echo “grade C”
else
echo fail
fi
:wq
#sh prabhu
Output: -
Enter the marks 60
grade A
➢ WAP to find n natural number using for loop: -
#vi prabhu
i=1
While($i-le10)
Do
Echo $i
i=’expr $i+1’
Done
:wq
#sh prabhu
Output: -
Ashoka College of Computer Education
19
Unix & Shell Programming
1 1 2 3 4 5 6 8 9
➢ WAP to generate n natural number in inverse series: -
#vi prabhu
i=10
While($i-le1)
Do
Echo $i
i=’expr $i-1’
Done
:wq
$sh prabhu
Output:-
9 8 7 6 5 4 3 2 1 0
➢ WAP to accept number from user and display it: -
#vi prabhu
Echo “enter the number from 1 to 3”
Read a
Ashoka College of Computer Education
20
Unix & Shell Programming
Case $a in
1).echo you have entered 1
;;
2). echo you have entered 2
;;
3). echo you have entered 3
;;
*). Echo you have entered wrong choice I say 1 to 3
;;
esac
done
:wq
$sh prabhu
Output: -
Enter the number 5
You have entered wrong choice I say 1 to 3
➢ WAP whether the number is even or odd: -
#vi prabhu
Echo “enter the number”
Read a
If ($a%2–eq0)
Echo “a is even”
Else
Echo “a is odd”
fi
:wq
$sh prabhu
Ashoka College of Computer Education
21
Unix & Shell Programming
Output: -
Enter the number 8
a is even
➢ WAP to print a table of any number: -
#vi honey
Echo “enter the number”
Read n
Echo “enter the limit”
Read c
C=1
While($c-le10)
X=’expr $n”*” $c=$x”
:wq
$sh honey
Output: -
Enter the number 3
Enter the limit 10
3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
Ashoka College of Computer Education
22
Unix & Shell Programming
3*7=21
3*8=24
3*9=27
3*10=30
➢ WAP to calculate the T.A , H.R.A and D.A of any employee: -
#vi prabhu
Echo ”enter basic salary”
Read $bs
If[bs-lt1500]
Then
H.R.A=’echo $bs\*10/100/bc’
D.A=’echo $bs\*90/100/bc’
Else
H.R.A=500
D.A=’echo $bs\*98/100/bc’
fi
G.S=’echo $bs + $H.R.A + $D.A/bc’
Echo”gross salary=Rs.$G.S”
Done
:wq
$sh prabhu
Output: -
Enter the basic salary
Gross salary =4460
➢ WAP to show state of first character of word: -
#vi prabhu
Ashoka College of Computer Education
23
Unix & Shell Programming
Echo “enter any word from user”
Read word
Case $word in
[a e i o u]*) echo “you have enter vowels in small letters”
;;
[A E I O U]*) echo “you have enter vowels in capital letters”
;;
[a-z]*) echo “you have enter consonants in small letters”
;;
[A-Z]*) echo “you have enter consonants in capital letters”
;;
*) echo “you have enter wrong choice”
;;
Esac
Done
:wq
$sh prabhu
Output: -
Enter the word prabhat
You have entered consonant in small letter.
➢ WAP to print the contents of a table: -
#vi prabhu
For i in 2,4 ,3
Do
c=1
While($c-le10)
Do
t=’expr $i”*”$c’
Echo “$t”
c=’expr $c+1’
Done
i=’expr $i+1’
Done
Ashoka College of Computer Education
24
Unix & Shell Programming
:wq
$sh prabhu
Output: -
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
➢ WAP to enter any character from user: -
#vi prabhu
Echo “enter any character”
Read char
Case $char in
[a-z])Echo “you have entered small letters.”
;;
[A-Z]) Echo “you have entered capital letters.”
;;
[0-9] echo “you have entered digits.”
;;
*)
Echo “you have have wrong choice.”
;;
Esac
Done
:wq
$sh prabhu
Ashoka College of Computer Education
25
Unix & Shell Programming
Output: -
Enter any character T
You have entered capital letters.
Ashoka College of Computer Education