sem 3: unix programming file

30
 U  NEIL IX ATHE PR - A23 G 24710  A  02 - 3C MI S4 – Y3 G 305  AB

Upload: metalwihen

Post on 06-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 1/30

 

U

 

NEIL

IX

ATHE

PR

- A23

G

24710

 A

 

02 - 3C

MI

S4 – Y3

G

305

 AB

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 2/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33051 

INDEXUNIX 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 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 3/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33052 

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 allthe details of the computer's internals is called the operating system or

ke rn e l.

Users communicate with the kernel through a program known as the shel l .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 employeesat 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; henceUNIX is called a multiuser system.

•  A user can also run multiple programs at the same time; hence UNIX

is called multitasking.

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 4/30

 

The ma

 

Kerne l :most of 

Shel l: T

your tershell us

famous

C o m m a

your da

utilities.

3rd part

Files an

directori

 

in concep

The kernelhe tasks li

he shell is t

minal, thes standard

hells which

nds and Ut

to day a

There are

software.

d D i r ec t o r

es. These d

NEI L M

U

that uni

is the heae memory

he utility th

hell interpsyntax for

are availa

i l i t i es: Thtivities. cp,

over 250 s

All the com

i es: All dairectories a

 

ATHEW – A2

IX

es all ve

rt of the omanageme

at process

ets the coall comman

le with mo

re are varimv, cat a

tandard co

mands com

a in UNIXe organize

3 2 4 7 1 0 0 0 2

TRU

rsions of 

perating snt, task sch

s your req

mand andds. C Shell,

t of the Un

us commad grep etc.

mmands pl

e along wit

is organizeinto a tre

3CS4 – Y33

TUR

UNIX is t

stem. It ieduling an

ests. Whe

calls the pBourne Sh

ix variants.

nd and utiliare few e

us numero

h various o

into files.-like struct

0 5

he followi

 

teracts witfile manag

you type i

rogram thaell and Kor

 

ties whichxamples of 

s others p

ptional opti

All files arure called t

g four b

h hardwarement.

n a comma

t you wantn Shell are

ou would ucommand

rovided th

ns.

e organizedhe file syst

sics:

and

nd at

. Themost

se inand

ough

intom.

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 5/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33054 

DIFFERENCE BETWEEN

UNIX & WINDOWS

NO: UNIX WINDOWS

1 It is a Free Source OS. It is a licensed OS.

2Less administration and maintenance isneeded in maintaining a UNIX system

More administration and maintenance isneeded in maintaining a Windows system

3 UNIX uses daemons. Windows has service processes.

4 UNIX is more secure. Windows is more vulnerable.

5Unix is much better at handling multiple

tasks for a single user or for multipleusers than windows.

Windows is inferior in this regard.

6UNIX preferred by programmers for its

more flexible nature.Windows preferred by less sophisticated

users.

7When a new process is created by a UNIX

application, it becomes a child of theprocess that created it.

Windows processes on the other hand do notshare a hierarchical relationship

8It uses the Unix file system (UFS) also

known as Fast File System (FFS) 

Windows uses theFAT32 and NTFS File system.

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 6/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33055 

BASIC 

UNIX COMMANDS

The various commands are:

w h owho : Shows other nodes connected to same server.

who am I : Shows your node.

$ who am iaib ttyp1 Jul 20 02:19

$ whoaib ttyp0 Jul 20 00:58aib ttyp1 Jul 20 02:19

aib ttyp2 Jul 20 02:19aib ttyp3 Jul 20 02:22aib ttyp4 Jul 20 02:22aib ttyp5 Jul 20 02:14aib ttyp6 Jul 20 02:05aib ttyp7 Jul 20 02:06aib ttyp8 Jul 20 02:18aib ttyp9 Jul 20 02:06aib ttyp10 Jul 20 02:15

p w dpwd : Present Working Directory 

$ pwd/usr/aib 

Bannerbanner <name> : display <name> in a banner-like format

$ banner SCIENCE##### ##### ### ####### # # ##### #######

# # # # # # ## # # # ## # # # # # # # ###### # # ##### # # # # #####

# # # # # # # # #

# # # # # # # ## # # ###### ##### ### ####### # # ##### ####### 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 7/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33056 

cal  cal : Calender 

$ cal 2011

2011

Jan Feb MarSu 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 52 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 129 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 2623 24 25 26 27 28 29 27 28 27 28 29 30 3130 31

Apr May JunSu 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 43 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 1817 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 2524 25 26 27 28 29 30 29 30 31 26 27 28 29 30

Jul Aug SepSu 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 33 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 1717 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 2424 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 3031

Oct Nov DecSu 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 109 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 2423 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 3130 31

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 8/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33057 

da t e

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 

t ouc h  

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 > FOLM2Yosh.$ cat > FOLM1DOOM DOT.$ cat < FOLM2Yosh.

$ cat < FOLM1DOOM DOT. 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 9/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33058 

m v

mv <filename1> <filename2> : Renames file from 1 to 2 

cp

cp <filename1> <filename2> : Copies from one to another 

r m

rm <filename1> : deletes file 

$ touch Olaf

$ cat > OlafBoom Box Reloaded.

$ cat < OlafBoom Box Reloaded.

$ mv Olaf Heimer <-- Renamed

$ cat < Olaf <-- Therefore Olaf no longer exists Olaf: cannot open

$ cat < HeimerBoom Box Reloaded.

$ rm Heimer <-- Deleted

$ cat < HeimerHeimer: cannot open

$ touch dwarf

$ cat > dwarfAin't I small?

$ cat <dwarfAin't I small?

$ cp dwarf Elf <-- Copy

$ cat <ElfAin't I small? 

- i

<command> -i <files> : With Permission 

$ rm -i mini miniME

remove mini ? yremove miniME ? n

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 10/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y33059 

w c

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 < PUMA2Jump & move it.Jump & move it..Jump & move it...Yo!

$ wc PUMA2

4 13 55 PUMA2$ wc -w PUMA213 PUMA2

$ wc -c PUMA255 PUMA2

$ wc -l PUMA24 PUMA2

Head  

head -n <filename> : displays the first n lines of a file

Tai l  

tail -n <filename> : displays the last n lines of a file

$ cat < PUMA2Jump & move it.Jump & move it..Jump & move it...Yo!

$ head -1 PUMA2Jump & move it.

$ tail -2 PUMA2Jump & move it...Yo! 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 11/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y330510 

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 > JLAClark KentBruce WayneDianaBarry AllenHal JordanArthur CurryJ'onn J'onzz

$ sort JLAArthur CurryBarry AllenBruce WayneClark KentDianaHal JordanJ'onn J'onzz

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 12/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y330511 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 13/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y330512 

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 controlthe system by using the keyboard rather than a combination of mouse

selections and keystrokes. The succinctness of the interface makes it highlyuseful 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 insertionor change command. [ESC] returns the editor to command mode. Mostcommands 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

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 14/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y330513 

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 combinesinto 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 hasa 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 programor file selection, quick start, and interactive debugging. A shell script can be

used to provide a sequencing and decision-making linkage around existingprograms, 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 ascript'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 aconventional compiled program that performs an equivalent task.

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 15/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y330514 

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 conddo

Statement(s) to be executed until command is true

done 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 16/30

N EI L MATH EW – A2324710 002 – 3CS4 – Y330515 

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 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 17/30

INDEXSHELL 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/20118.  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/201112.ENTER A FIVE DIGIT NUMBER AND CALCULATE

THE SUM OF ITS DIGITS P13 

5/10/201113.CHECK IF NUMBER IS A PRIME NO

P14 

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 18/30

 P2

SHELL PROGRAMMING >

Q1. FIND SUM OF 5 NATURAL NUMBERS 

metalwihen@metalwihen:~$ vi Question1

echo "Enter 5 numbers: "

read a

read bread cread d

read e

sum=` expr $a + $b + $c + $d + $e `

echo "Sum of 5 numbers = $sum"

"Question1" 12L, 109C written

metalwihen@metalwihen:~$ sh Question1Enter 5 numbers:

1012

1416

18The Sum is: 70

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 19/30

 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 Question2Enter the radius of the Circle:

2 Ans is 12

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 20/30

 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 writtenmetalwihen@metalwihen:~$ sh Question3Enter the Principal:

1000Enter the Rate per year:

2Enter the Time in years:

3The Simple Interest is 60

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 21/30

 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=$aa=$b

b=$temp

echo "First Number is now: $a"

echo "Second Number is now: $b"

"Question4" 13L, 167C writtenmetalwihen@metalwihen:~$ sh Question4

Enter the First Number:7Enter the Second Number:

3First Number is now: 3

Second Number is now: 7

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 22/30

 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 ]; thenecho "$b is the greatest!"else

echo "$c is the greatest!"fi

"Question5" 18L, 294C written

metalwihen@metalwihen:~$ sh Question5Enter the First Number:

0Enter the Second Number:

9Enter the Third Number:

5

9 is the greatest!

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 23/30

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

elseecho "$y is NOT a leap year!"

fi

"Question6" 12L, 139C written

metalwihen@metalwihen:~$ sh Question6

Enter the Year:20082008 is a leap year!

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 24/30

 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

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 25/30

 P9

SHELL PROGRAMMING >

Q8.INPUT STUDENT RESULT MARKS AND CATEGORIZEWHETHER IN FAIL, 1ST DIVISION

OR 2ND DIVISION

metalwihen@metalwihen:~$ vi Question8

echo "Enter Final Marks: "read res

if [ $res -ge 85 ]; thenecho "You are in First Division!"

elif [ $res -ge 70 ]; then

echo "You are in Second Division!"elif [ $res -lt 40 ]; thenecho "You have FAILED!"

elseecho "No Division. AVERAGE"

fi

"Question8" 14L, 247C writtenmetalwihen@metalwihen:~$ sh Question8

Enter Final Marks:84

You are in Second Division!

metalwihen@metalwihen:~$ sh Question8

Enter Final Marks:56

No Division. AVERAGE

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 26/30

 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 writtenmetalwihen@metalwihen:~$ sh Question9

Enter Number:5Factorial is: 120

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 27/30

 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 ]dotemp=$num2

num2=`expr $num2 + $num1`

num1=$temp

echo -n " + $num2"

n=`expr $n - 1`

done

echo

"Question10" 23L, 220C written

metalwihen@metalwihen:~$ sh Question10Enter limiting number:

6Fibonacci Series: 0 + 1 + 2 + 3 + 5 + 8

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 28/30

 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 writtenmetalwihen@metalwihen:~$ sh Question11

Enter the number:

345Reverse of 345 is 543

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 29/30

 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 ]

dodigit=`expr $num % 10`

sum=`expr $sum + $digit`

num=`expr $num / 10`

done

echo "Sum of digits: $sum"

"Question12" [New] 16L, 171C writtenmetalwihen@metalwihen:~$ sh Question12

Enter the Number:12345Sum of digits: 15

8/2/2019 Sem 3: Unix Programming File

http://slidepdf.com/reader/full/sem-3-unix-programming-file 30/30

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 ]thenflag=0

fi

i=`expr $i + 1`

done

if [ $flag -eq 0 ]then

echo "Number is NOT Prime."

elseecho "Number is Prime."fi

"Question13" 30L, 236C written

metalwihen@metalwihen:~$ sh Question13

Enter the number:5Number is Prime.

metalwihen@metalwihen:~$ sh Question13

Enter the number:6

Number is NOT Prime.