Shell Programming
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.
Teacher Incharge
Principal
Index
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.
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
Output:-
akhil
Rohit
pankaj
sunil
raka
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
Output: - Linux
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
Syntax: - #pwd
Output: -
Root prabhu
tput clear command ::--- This command is used to clear the screen.
man command ::--- This command is used when we need help then
we use man command.
Syntax: - #man ls
Syntax: - #ls-axf
Output: - prabhat Kishore
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.
Is *.c command: - This command is used to display all the files with
extension .c .
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
Syntax: - tty
Output: -
/device/pts/0
Syntax: - #stty
Output: - Speed=54630 baud;
Line=0;
Eol=m_^?
cat > command: - This command is used to create the hidden files.
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
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: - # 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
Shell Scripting
#vi prabhu
Echo “enter first number”
Read $a
Echo “enter second number”
Read $b
Echo ‘expr $a + $b’
:wq
#sh prabhu
Output
# 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
Output: -
# 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:-
#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
#vi prabhu
Echo “enter the limit”
Read limit
a=0
b=1
c=0
while($c – lt $limit)
do
c=’expr $a+$b’
echo “$c”
a=”$b”
b=”$c”
done
:wq
#sh prabhu
Output: -
# 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]
then
echo “grade C”
else
echo fail
fi
:wq
#sh prabhu
Output: -
Enter the marks 60
grade A
#vi prabhu
i=1
While($i-le10)
Do
Echo $i
i=’expr $i+1’
Done
:wq
#sh prabhu
Output: -
1 1 2 3 4 5 6 8 9
#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
#vi prabhu
Echo “enter the number from 1 to 3”
Read a
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
#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
Output: -
#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: -
3*7=21
3*8=24
3*9=27
3*10=30
#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: -
#vi prabhu
Output: -
#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
: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
#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
Output: -