UN PRO RAM NG L B NIX OGR MMIN LAB

NEIL MATHEW - A23 M W 3247100 - 3C – Y3305 002 CS4

INDEX
UNIX LAB FILE

1. WHAT IS UNIX?

2 3 4 5 12 13 14 15

2. UNIX STRUCTURE
3. DIFFERENCE BETWEEN UNIX AND WINDOWS 4. BASIC UNIX COMMANDS

5. VI EDITOR
6. SHELL SCRIPTING

7. LOOPING SYNTAX
8. DESCISION MAKING SYNTAX

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

1

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.

• •

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

2

UN NIX STRUC S CTURE E
The ma ain concept that unit t tes all ver rsions of UNIX is the followin ng four ba asics:

Kernel: The kernel is the hea of the operating sy art o ystem. It in nteracts with hardware and e most of the tasks lik memory managemen task sch t ke nt, heduling and file manag d gement. Shell: The shell is the utility th processe your requ t hat es uests. When you type in a command at n your term minal, the shell interpr s rets the com mmand and calls the p program tha you want. The at shell use standard syntax for all commands. C Shell, Bourne Sh es a , hell and Korn Shell are most famous shells which are availab with mos of the Un ix variants. s h ble st Commands and Ut tilities: The are vario ere ous comma nd and utilities which y you would u in use your day to day ac y ctivities. cp, mv, cat an grep etc. are few ex nd xamples of commands and s utilities. There are over 250 standard com s mmands plu numerou others p us us provided thr rough 3rd party software. All the commands com along wit h various op y me ptional optio ons. Files an Director nd ries: All dat in UNIX is organized into files. All files are organized into ta d e d directorie These directories ar organized into a tree es. re d e-like structure called the file syste em.

NEIL MATHEW – A2 M 2324710002 – 3CS4 – Y33 305

3

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.

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

4

BASIC

UNIX COMMANDS
The various commands are:

who : Shows other nodes connected to same server. who am I : Shows your node.
$ who am i aib ttyp1 $ who aib aib aib aib aib aib aib aib aib aib aib

who

Jul 20 02:19

ttyp0 ttyp1 ttyp2 ttyp3 ttyp4 ttyp5 ttyp6 ttyp7 ttyp8 ttyp9 ttyp10

Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul

20 20 20 20 20 20 20 20 20 20 20

00:58 02:19 02:19 02:22 02:22 02:14 02:05 02:06 02:18 02:06 02:15

pwd : Present Working Directory
$ pwd /usr/aib

pwd

banner <name> : display <name> in a banner-like format $ banner SCIENCE
##### # # # # ##### # # # # # # # ##### ##### # # ##### ### # # # # # ### ####### # # ##### # # ####### # # ## # # # # # # # # # # # ## # # ##### # # # # # ####### # # # ##### # # # ##### #######

Banner

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

5

cal : Calender
$ cal 2011

cal

2011 Jan 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 Apr 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 Jul Su Mo Tu We Th Fr 1 3 4 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 24 25 26 27 28 29 31 Oct Su Mo Tu We Th Fr 2 9 16 23 30 3 4 5 6 7 10 11 12 13 14 17 18 19 20 21 24 25 26 27 28 31 Feb 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 May We Th 4 5 11 12 18 19 25 26 Mar We Th 2 3 9 10 16 17 23 24 30 31

Su Mo Tu 1 6 7 8 13 14 15 20 21 22 27 28 29

Fr 4 11 18 25

Sa 5 12 19 26

Su 1 8 15 22 29

Mo 2 9 16 23 30

Tu 3 10 17 24 31

Fr 6 13 20 27

Sa 7 14 21 28

Jun Su Mo Tu We Th 1 2 5 6 7 8 9 12 13 14 15 16 19 20 21 22 23 26 27 28 29 30 Sep Su Mo Tu We Th 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29 Dec Su Mo Tu We Th 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29

Fr 3 10 17 24

Sa 4 11 18 25

Sa 2 9 16 23 30

Su Mo 1 7 8 14 15 21 22 28 29

Tu 2 9 16 23 30

Aug We Th 3 4 10 11 17 18 24 25 31 Nov We Th 2 3 9 10 16 17 23 24 30

Fr 5 12 19 26

Sa 6 13 20 27

Fr 2 9 16 23 30

Sa 3 10 17 24

Sa 1 8 15 22 29

Su Mo Tu 1 6 7 8 13 14 15 20 21 22 27 28 29

Fr 4 11 18 25

Sa 5 12 19 26

Fr 2 9 16 23 30

Sa 3 10 17 24 31

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

6

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 20 $ date 07 $ date 11 $ date
2011 $date +%T <-02:27:14 Time

+%d <-- Day (Date LowerCase) +%m <-- Month +%y <-- Year (two digits) +%Y <-- Year (all four digits)

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.

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

7

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

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

8

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 $ wc -w PUMA2 13 PUMA2 $ wc -c PUMA2 55 PUMA2 $ wc -l PUMA2 4 PUMA2

55 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!

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

9

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

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

10

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

11

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 i R A I o O

append text, after the cursor; insert text, before the cursor; enter Overtype Mode; append text, after end of line; insert text, before first non-whitespace character; open new line below cursor in Insert Mode; open new line above cursor in Insert Mode;

EXITING THE INSERT MODE ESC Exit insertion mode and return to command mode

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

12

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 subdirectories 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.

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

13

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

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

14

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

NEIL MATHEW – A2324710002 – 3CS4 – Y3305

15

INDEX
SHELL PROGRAMMING

3/8/2011

1. FIND SUM OF 5 NATURAL NUMBERS

P2 P3 P4 P5 P6 P7 P8

10/8/2011

2. FIND THE AREA OF CIRCLE
3. CALCULATE THE SIMPLE INTEREST 4. SWAP TWO NUMBERS

10/8/2011

17/8/2011

24/8/2011

5. FIND THE LARGEST NUMBER
6. CHECK IF THE YEAR IS LEAP YEAR OR NOT

24/8/2011

31/8/2011

7. MAKE A CALCULATOR USING SWITCH CASE
8. INPUT STUDENT RESULT MARKS AND CATEGORIZE WHETHER IN FAIL, 1ST OR 2ND DIVISION 9. FIND FACTORIAL OF A NUMBER 10.FIND FIBONACCI SERIES 11.FIND REVERSE OF A NUMBER 12.ENTER A FIVE DIGIT NUMBER AND CALCULATE THE SUM OF ITS DIGITS 13.CHECK IF NUMBER IS A PRIME NO

7/9/2011

P9

14/9/2011

P10 P11 P12

21/9/2011

21/9/2011

28/9/2011

P13

5/10/2011

P14

SHELL PROGRAMMING >

Q1. FIND SUM OF 5 NATURAL NUMBERS

metalwihen@metalwihen:~$ vi Question1 echo read read read read read "Enter 5 numbers: " a b c d 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

P2

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

P3

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

P4

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

P5

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 [ then echo elif echo else echo fi $a -gt $b ] && [ $a -gt $c ] "$a is the greatest!" [ $b -gt $a ] && [ $b -gt $c ]; then "$b is the greatest!" "$c is the greatest!"

"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!

P6

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!

P7

SHELL PROGRAMMING >

Q7. MAKE A CALCULATOR USING SWITCH CASE

metalwihen@metalwihen:~$ vi Question7 echo read echo read echo echo echo echo echo echo echo read "Enter the First Number:" a "Enter the Second Number:" b

"Options:" "To Add, Enter +" "To Subtract, Enter -" "To Multiply, Enter *" "To Divide, Enter /" -n "Your Choice: " 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

P8

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 [ echo elif echo elif echo else echo fi $res -ge 85 ]; then "You are in First Division!" [ $res -ge 70 ]; then "You are in Second Division!" [ $res -lt 40 ]; then "You have FAILED!" "No Division. AVERAGE"

"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

P9

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

P10

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

P11

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

P12

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

P13

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.

P14

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.