2. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
1
INDEX
UNIX LAB FILE
1. WHAT IS UNIX? 2
2. UNIX STRUCTURE 3
3. DIFFERENCE BETWEEN UNIX AND WINDOWS 4
4. BASIC UNIX COMMANDS 5
5. VI EDITOR 12
6. SHELL SCRIPTING 13
7. LOOPING SYNTAX 14
8. DESCISION MAKING SYNTAX 15
3. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
2
WHAT IS UNIX?
The UNIX operating system is a set of programs that act as a link between
the computer and the user.
The computer programs that allocate the system resources and coordinate all
the details of the computer's internals is called the operating system or
kernel.
Users communicate with the kernel through a program known as the shell.
The shell is a command line interpreter; it translates commands entered by
the user and converts them into a language that is understood by the kernel.
• Unix was originally developed in 1969 by a group of AT&T employees
at Bell Labs, including Ken Thompson, Dennis Ritchie, Douglas McIlroy,
and Joe Ossanna.
• There are various Unix variants available in the market. Solaris Unix,
AIX, UP Unix and BSD are few examples. Linux is also a flavour of Unix
which is freely available.
• Several people can use a UNIX computer at the same time; hence
UNIX is called a multiuser system.
• A user can also run multiple programs at the same time; hence UNIX
is called multitasking.
4. The ma
Kernel:
most of t
Shell: T
your term
shell use
famous s
Comma
your day
utilities.
3rd party
Files an
directorie
ain concept
The kernel
the tasks lik
he shell is t
minal, the s
es standard
shells which
nds and Ut
y to day ac
There are
y software.
nd Director
es. These d
NEIL M
UN
t that unit
is the hea
ke memory
the utility th
shell interpr
syntax for a
h are availab
tilities: The
ctivities. cp,
over 250 s
All the com
ries: All dat
irectories ar
MATHEW – A2
NIX S
tes all ver
art of the o
managemen
hat processe
rets the com
all comman
ble with mos
ere are vario
mv, cat an
standard com
mands com
ta in UNIX
re organized
2324710002 –
STRUC
rsions of
operating sy
nt, task sch
es your requ
mmand and
ds. C Shell,
st of the Un
ous comma
nd grep etc.
mmands plu
me along wit
is organized
d into a tree
– 3CS4 – Y33
CTURE
UNIX is t
ystem. It in
heduling and
uests. When
calls the p
, Bourne Sh
ix variants.
nd and utili
are few ex
us numerou
h various op
d into files.
e-like struct
305
E
he followin
nteracts wit
d file manag
n you type i
program tha
hell and Kor
ties which y
xamples of
us others p
ptional optio
All files are
ure called t
ng four ba
h hardware
gement.
n a comma
at you want
n Shell are
you would u
commands
provided thr
ons.
e organized
he file syste
3
asics:
e and
nd at
. The
most
use in
s and
rough
d into
em.
5. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
4
DIFFERENCE BETWEEN
UNIX & WINDOWS
NO: UNIX WINDOWS
1 It is a Free Source OS. It is a licensed OS.
2
Less administration and maintenance is
needed in maintaining a UNIX system
More administration and maintenance is
needed in maintaining a Windows system
3 UNIX uses daemons. Windows has service processes.
4 UNIX is more secure. Windows is more vulnerable.
5
Unix is much better at handling multiple
tasks for a single user or for multiple
users than windows.
Windows is inferior in this regard.
6
UNIX preferred by programmers for its
more flexible nature.
Windows preferred by less sophisticated
users.
7
When a new process is created by a UNIX
application, it becomes a child of the
process that created it.
Windows processes on the other hand do not
share a hierarchical relationship
8
It uses the Unix file system (UFS) also
known as Fast File System (FFS)
Windows uses the
FAT32 and NTFS File system.
6. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
5
BASIC
UNIX COMMANDS
The various commands are:
who
who : Shows other nodes connected to same server.
who am I : Shows your node.
$ who am i
aib ttyp1 Jul 20 02:19
$ who
aib ttyp0 Jul 20 00:58
aib ttyp1 Jul 20 02:19
aib ttyp2 Jul 20 02:19
aib ttyp3 Jul 20 02:22
aib ttyp4 Jul 20 02:22
aib ttyp5 Jul 20 02:14
aib ttyp6 Jul 20 02:05
aib ttyp7 Jul 20 02:06
aib ttyp8 Jul 20 02:18
aib ttyp9 Jul 20 02:06
aib ttyp10 Jul 20 02:15
pwd
pwd : Present Working Directory
$ pwd
/usr/aib
Banner
banner <name> : display <name> in a banner-like format
$ banner SCIENCE
##### ##### ### ####### # # ##### #######
# # # # # # ## # # # #
# # # # # # # # #
##### # # ##### # # # # #####
# # # # # # # # #
# # # # # # # ## # # #
##### ##### ### ####### # # ##### #######
7. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
6
cal
cal : Calender
$ cal 2011
2011
Jan Feb Mar
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
Apr May Jun
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
Jul Aug Sep
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
31
Oct Nov Dec
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31
30 31
8. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
7
date
date +% _ : Shows date, with specified formats
$date
Wed Jul 20 02:26:01 IST 2011
$ date +%H <-- Hours (Time Capital)
02
$ date +%M <-- Minutes
26
$ date +%S <-- Seconds
19
$ date
Wed Jul 20 02:26:31 IST 2011
$ date +%d <-- Day (Date LowerCase)
20
$ date +%m <-- Month
07
$ date +%y <-- Year (two digits)
11
$ date +%Y <-- Year (all four digits)
2011
$date +%T <-- Time
02:27:14
touch
touch <filename> : Creates files without data
cat
cat > (existing/new filename) : Write data to file (also creates)
cat < (existing filename) : Read data from file
$ touch FOLM1 FOLM2 FOLM3
$ cat > FOLM2
Yosh.
$ cat > FOLM1
DOOM DOT.
$ cat < FOLM2
Yosh.
$ cat < FOLM1
DOOM DOT.
9. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
8
mv
mv <filename1> <filename2> : Renames file from 1 to 2
cp
cp <filename1> <filename2> : Copies from one to another
rm
rm <filename1> : deletes file
$ touch Olaf
$ cat > Olaf
Boom Box Reloaded.
$ cat < Olaf
Boom Box Reloaded.
$ mv Olaf Heimer <-- Renamed
$ cat < Olaf <-- Therefore Olaf no longer exists
Olaf: cannot open
$ cat < Heimer
Boom Box Reloaded.
$ rm Heimer <-- Deleted
$ cat < Heimer
Heimer: cannot open
$ touch dwarf
$ cat > dwarf
Ain't I small?
$ cat <dwarf
Ain't I small?
$ cp dwarf Elf <-- Copy
$ cat <Elf
Ain't I small?
-i
<command> -i <files> : With Permission
$ rm -i mini miniME
remove mini ? y
remove miniME ? n
10. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
9
wc
wc <filename> : Word Count ( lines, words, characters)
-l : only lines
-w : only words
-c : only characters
-wl : words & lines
-wc : words & characters
-lc : lines & characters
$ cat < PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!
$ wc PUMA2
4 13 55 PUMA2
$ wc -w PUMA2
13 PUMA2
$ wc -c PUMA2
55 PUMA2
$ wc -l PUMA2
4 PUMA2
Head
head -n <filename> : displays the first n lines of a file
Tail
tail -n <filename> : displays the last n lines of a file
$ cat < PUMA2
Jump & move it.
Jump & move it..
Jump & move it...
Yo!
$ head -1 PUMA2
Jump & move it.
$ tail -2 PUMA2
Jump & move it...
Yo!
11. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
10
Sort
sort <filename> : Sorts files taking each line as separate entity
sort -r <filename> : reverse order (descending)
sort -o<to_filename> <from_filename> : save sorted file
sort <filename> <filename2> : Combines both and sorts it all.
sort -o<TO_file> <FROM_file1> <FROM_file2> : Saves the combined and sorted.
-m : treats each file as an entity instead of the lines within it
-u : avoids repetition when common entities from 2 files repeat
$ cat > JLA
Clark Kent
Bruce Wayne
Diana
Barry Allen
Hal Jordan
Arthur Curry
J'onn J'onzz
$ sort JLA
Arthur Curry
Barry Allen
Bruce Wayne
Clark Kent
Diana
Hal Jordan
J'onn J'onzz
13. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
12
VI EDITOR
Vi editor, is a widely-used and popular UNIX-based text editor.
Like most UNIX system interfaces and other text editors, it lets you control
the system by using the keyboard rather than a combination of mouse
selections and keystrokes. The succinctness of the interface makes it highly
useful for people who work at a computer all day, especially programmers
entering or manipulating language statements.
There are two modes in the Vi Editor.
1. Command Mode
2. Insertion Mode
The editor begins in command mode, where the cursor movement and text
deletion and pasting occur. Insertion mode begins upon entering an insertion
or change command. [ESC] returns the editor to command mode. Most
commands execute as soon as you type them except for "colon" commands
which execute when you press the return key.
ENTERING THE INSERT MODE
a append text, after the cursor;
i insert text, before the cursor;
R enter Overtype Mode;
A append text, after end of line;
I insert text, before first non-whitespace character;
o open new line below cursor in Insert Mode;
O open new line above cursor in Insert Mode;
EXITING THE INSERT MODE
ESC Exit insertion mode and return to command mode
14. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
13
SHELL SCRIPTING
A shell script is a text file that contains a sequence of commands for a
UNIX-based operating system. It's called a shell script because it combines
into a single file (script) a sequence of commands that would otherwise have
to be presented to the system from a keyboard one at a time.
A shell script is usually created for command sequences for which a user has
a repeated need. You initiate the sequence of commands in the shell script by
simply entering the name of the shell script on a command line.
Often, writing a shell script is much quicker than writing the equivalent code
in other programming languages. The many advantages include easy program
or file selection, quick start, and interactive debugging. A shell script can be
used to provide a sequencing and decision-making linkage around existing
programs, and for moderately-sized scripts the absence of a compilation step
is an advantage.
On the other hand, shell scripting is prone to costly errors. Inadvertent typing
errors such as rm -rf * / are folklore in the Unix community; a single extra
space converts the command from one that deletes everything in the sub-
directories to one which deletes everything - and also tries to delete
everything in the root directory. Similar problems can transform cp and mv
into dangerous weapons, and misuse of the > redirect can delete the
contents of a file. This is made more problematic by the fact that many UNIX
commands differ in name by only one letter: cp, cd, dd, df, etc.
Another significant disadvantage is the slow execution speed and the need to
launch a new process for almost every shell command executed. When a
script's job can be accomplished by setting up a pipeline in which efficient
filter commands perform most of the work, the slowdown is mitigated, but a
complex script is typically several orders of magnitude slower than a
conventional compiled program that performs an equivalent task.
15. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
14
LOOPING SYNTAX
FOR LOOP
Syntax:
for { <variable_name> } in { <list of variable> }
do
<commands>
done
WHILE LOOP
Syntax:
while [ <condition> ]
do
<command1>
<command2>
done
UNTIL LOOP
Syntax:
until cond
do
Statement(s) to be executed until command is true
done
16. NEIL MATHEW – A2324710002 – 3CS4 – Y3305
15
DECISION MAKING SYNTAX
SWITCH CASE
It is a control statement used for decision making.
Syntax:
case $<variable> in
<option1>)
<statement>
;;
<option2>)
<statement>
;;
esac
IF CONDITION
It is a control statement used for decision making.
Syntax:
if [ $<variable> <logical operator> $<variable>
then
<statement>
fi
( IF - ELSE IF )
if [ $<variable> <logical operator> $<variable> ]
then
<statement>
elif [$<variable> <logical operator> $<variable> ]
then
<statement>
else
<statement>
fi
17. INDEX
SHELL PROGRAMMING
3/8/2011 1. FIND SUM OF 5 NATURAL NUMBERS P2
10/8/2011 2. FIND THE AREA OF CIRCLE P3
10/8/2011 3. CALCULATE THE SIMPLE INTEREST P4
17/8/2011 4. SWAP TWO NUMBERS P5
24/8/2011 5. FIND THE LARGEST NUMBER P6
24/8/2011 6. CHECK IF THE YEAR IS LEAP YEAR OR NOT P7
31/8/2011 7. MAKE A CALCULATOR USING SWITCH CASE P8
7/9/2011
8. INPUT STUDENT RESULT MARKS AND CATEGORIZE
WHETHER IN FAIL, 1ST OR 2ND DIVISION P9
14/9/2011 9. FIND FACTORIAL OF A NUMBER P10
21/9/2011 10.FIND FIBONACCI SERIES P11
21/9/2011 11.FIND REVERSE OF A NUMBER P12
28/9/2011
12.ENTER A FIVE DIGIT NUMBER AND CALCULATE
THE SUM OF ITS DIGITS P13
5/10/2011 13.CHECK IF NUMBER IS A PRIME NO P14
18. P2
SHELL PROGRAMMING >
Q1. FIND SUM OF 5 NATURAL NUMBERS
metalwihen@metalwihen:~$ vi Question1
echo "Enter 5 numbers: "
read a
read b
read c
read d
read e
sum=` expr $a + $b + $c + $d + $e `
echo "Sum of 5 numbers = $sum"
"Question1" 12L, 109C written
metalwihen@metalwihen:~$ sh Question1
Enter 5 numbers:
10
12
14
16
18
The Sum is: 70
19. P3
SHELL PROGRAMMING >
Q2. FIND THE AREA OF CIRCLE
metalwihen@metalwihen:~$ vi Question2
echo "Enter the radius of the Circle: "
read r
area=` expr 22 / 7 * $r * $r`
echo "Ans is $area"
"Question2" 7L, 104C written
metalwihen@metalwihen:~$ sh Question2
Enter the radius of the Circle:
2
Ans is 12
20. P4
SHELL PROGRAMMING >
Q3. CALCULATE THE SIMPLE INTEREST
metalwihen@metalwihen:~$ vi Question3
echo "Enter the Principal: "
read P
echo "Enter the Rate per year: "
read R
echo "Enter the Time in years: "
read T
SI=`expr $P * $R * $T / 100`
echo "The Simple Interest is " $SI
"Question3" 14L, 179C written
metalwihen@metalwihen:~$ sh Question3
Enter the Principal:
1000
Enter the Rate per year:
2
Enter the Time in years:
3
The Simple Interest is 60
21. P5
SHELL PROGRAMMING >
Q4. SWAP TWO NUMBERS
metalwihen@metalwihen:~$ vi Question4
echo "Enter the First Number: "
read a
echo "Enter the Second Number: "
read b
temp=$a
a=$b
b=$temp
echo "First Number is now: $a"
echo "Second Number is now: $b"
"Question4" 13L, 167C written
metalwihen@metalwihen:~$ sh Question4
Enter the First Number:
7
Enter the Second Number:
3
First Number is now: 3
Second Number is now: 7
22. P6
SHELL PROGRAMMING >
Q5. FIND THE LARGEST NUMBER
metalwihen@metalwihen:~$ vi Question5
echo "Enter the First Number: "
read a
echo "Enter the Second Number: "
read b
echo "Enter the Third Number: "
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is the greatest!"
elif [ $b -gt $a ] && [ $b -gt $c ]; then
echo "$b is the greatest!"
else
echo "$c is the greatest!"
fi
"Question5" 18L, 294C written
metalwihen@metalwihen:~$ sh Question5
Enter the First Number:
0
Enter the Second Number:
9
Enter the Third Number:
5
9 is the greatest!
23. P7
SHELL PROGRAMMING >
Q6. CHECK IF THE YEAR IS A LEAP YEAR OR NOT
metalwihen@metalwihen:~$ vi Question6
echo "Enter the Year: "
read y
rem=`expr $y % 4`
if [ $rem -eq 0 ]
then
echo "$y is a leap year!"
else
echo "$y is NOT a leap year!"
fi
"Question6" 12L, 139C written
metalwihen@metalwihen:~$ sh Question6
Enter the Year:
2008
2008 is a leap year!
24. P8
SHELL PROGRAMMING >
Q7. MAKE A CALCULATOR USING SWITCH CASE
metalwihen@metalwihen:~$ vi Question7
echo "Enter the First Number:"
read a
echo "Enter the Second Number:"
read b
echo
echo "Options:"
echo "To Add, Enter +"
echo "To Subtract, Enter -"
echo "To Multiply, Enter *"
echo "To Divide, Enter /"
echo -n "Your Choice: "
read ch
case $ch in
+)
res=`expr $a + $b`
echo "Sum is: $res"
;;
-)
res=`expr $a - $b`
echo "Remainder is: $res"
;;
*)
res=`expr $a * $b`
echo "Product is: $res"
;;
/)
res=`expr $a / $b`
echo "Quotient is: $res"
;;
esac
"Question7" 40L, 458C written
metalwihen@metalwihen:~$ sh Question7
Enter the First Number:
4
Enter the Second Number:
6
Options:
To Add, Enter +
To Subtract, Enter -
To Multiply, Enter *
To Divide, Enter /
Your Choice: *
Product is: 24
25. P9
SHELL PROGRAMMING >
Q8.INPUT STUDENT RESULT MARKS AND CATEGORIZE
WHETHER IN FAIL, 1ST DIVISION
OR 2ND DIVISION
metalwihen@metalwihen:~$ vi Question8
echo "Enter Final Marks: "
read res
if [ $res -ge 85 ]; then
echo "You are in First Division!"
elif [ $res -ge 70 ]; then
echo "You are in Second Division!"
elif [ $res -lt 40 ]; then
echo "You have FAILED!"
else
echo "No Division. AVERAGE"
fi
"Question8" 14L, 247C written
metalwihen@metalwihen:~$ sh Question8
Enter Final Marks:
84
You are in Second Division!
metalwihen@metalwihen:~$ sh Question8
Enter Final Marks:
56
No Division. AVERAGE
26. P10
SHELL PROGRAMMING >
Q9. FIND FACTORIAL OF A NUMBER
metalwihen@metalwihen:~$ vi Question9
echo "Enter Number:"
read n
f=1
while [ $n -gt 0 ]
do
f=`expr $f * $n`
n=`expr $n - 1`
done
echo "Factorial is: $f"
"Question9" 16L, 124C written
metalwihen@metalwihen:~$ sh Question9
Enter Number:
5
Factorial is: 120
27. P11
SHELL PROGRAMMING >
Q10. FIND FIBONACCI SERIES
metalwihen@metalwihen:~$ vi Question10
echo "Enter limiting number:"
read n
num1=0
num2=1
echo -n "Fibonacci Series:"
echo -n " $num1"
while [ $n -gt 1 ]
do
temp=$num2
num2=`expr $num2 + $num1`
num1=$temp
echo -n " + $num2"
n=`expr $n - 1`
done
echo
"Question10" 23L, 220C written
metalwihen@metalwihen:~$ sh Question10
Enter limiting number:
6
Fibonacci Series: 0 + 1 + 2 + 3 + 5 + 8
28. P12
SHELL PROGRAMMING >
Q11. FIND REVERSE OF A NUMBER
metalwihen@metalwihen:~$ vi Question11
echo "Enter the number:"
read n
num=$n
rev=0
while [ $num -gt 0 ]
do
digit=`expr $num % 10`
rev=`expr $rev * 10 + $digit`
num=`expr $num / 10`
done
echo "Reverse of $n is $rev"
"Question11" 17L, 184C written
metalwihen@metalwihen:~$ sh Question11
Enter the number:
345
Reverse of 345 is 543
29. P13
SHELL PROGRAMMING >
Q12. ENTER A FIVE DIGIT NUMBER
AND CALCULATE THE SUM OF ITS DIGITS
metalwihen@metalwihen:~$ vi Question12
echo "Enter the Number:"
read num
sum=0
while [ $num -gt 0 ]
do
digit=`expr $num % 10`
sum=`expr $sum + $digit`
num=`expr $num / 10`
done
echo "Sum of digits: $sum"
"Question12" [New] 16L, 171C written
metalwihen@metalwihen:~$ sh Question12
Enter the Number:
12345
Sum of digits: 15
30. P14
SHELL PROGRAMMING >
Q13. CHECK IF NUMBER IS A PRIME NO
metalwihen@metalwihen:~$ vi Question12
echo "Enter the number:"
read n
flag=1
i=2
while [ $i -lt $n ]
do
rem=`expr $n % $i`
if [ $rem -eq 0 ]
then
flag=0
fi
i=`expr $i + 1`
done
if [ $flag -eq 0 ]
then
echo "Number is NOT Prime."
else
echo "Number is Prime."
fi
"Question13" 30L, 236C written
metalwihen@metalwihen:~$ sh Question13
Enter the number:
5
Number is Prime.
metalwihen@metalwihen:~$ sh Question13
Enter the number:
6
Number is NOT Prime.