unix - class3 - programming constructs

13
UNIX Programming Construct Presentation By Nihar R

Upload: nihar-ranjan-paital

Post on 16-Apr-2017

1.199 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: UNIX - Class3 - Programming Constructs

UNIX

Programming Construct

Presentation By

Nihar R Paital

Page 2: UNIX - Class3 - Programming Constructs

Nihar R Paital

Nested ifs

You can write the entire if-else construct within either the body of the if statement of the body of an else statement. This is called the nesting of ifs.

Ex :chos=0echo "1. Unix (Sun Os)"echo "2. Linux (Red Hat)"

echo -n "Select your os choice [1 or 2]? "read chos

if [ $chos -eq 1 ] ; thenecho "You Pick up Unix (Sun Os)"else if [ $chos -eq 2 ] ; then echo "You Pick up Linux (Red Hat)" else echo "What you don't like Unix/Linux OS." fifi

Page 3: UNIX - Class3 - Programming Constructs

Nihar R Paital

Multilevel if-then-else

#!/bin/kshecho Enter a number :read nif [ $n -gt 0 ]; then echo "$n is positive"elif [ $n -lt 0 ]then echo "$n is negative"elif [ $n -eq 0 ]then echo "$n is zero"else echo "Opps! $n is not number, give number"fi

Page 4: UNIX - Class3 - Programming Constructs

Nihar R Paital

Loops in Shell Scripts

Bash supports:

– for loop– while loop

In each and every loop,

(a) First, the variable used in loop condition must be initialized, then execution of the loop begins.

(b) A test (condition) is made at the beginning of each iteration.

(c) The body of loop ends with a statement that modifies the value of the test (condition) variable.

Page 5: UNIX - Class3 - Programming Constructs

Nihar R Paital

for Loop

1) for x in 10 20 30 40 50 do

echo $x done 2) Ex: Arrays with for loop

#!/bin/kshy="shell scripting Training"for x in ${y[*]}do echo $xdone

Page 6: UNIX - Class3 - Programming Constructs

Nihar R Paital

Ex: for statement

3) for x in `ls` do

echo $x done

4) for x in `ls` `cat forloop` do echo $x done

Page 7: UNIX - Class3 - Programming Constructs

Nihar R Paital

while loop

Syntax:

while [ condition ] do command1 command2 command3 .. .... done

Page 8: UNIX - Class3 - Programming Constructs

Nihar R Paital

Ex: while statement.

x=1while [ $x -lt 10 ]doecho $xx=`expr $x + 1`done

Page 9: UNIX - Class3 - Programming Constructs

Nihar R Paital

until statement

Syntax:until control command

do <commands>Done

Ex:x=1until [ $x -gt 10 ]doecho $xx=`expr $x + 1`done

Page 10: UNIX - Class3 - Programming Constructs

Nihar R Paital

case statement.

The case statement is good alternative to Multilevel if-then-else-fi statement. It enable you to match several values against one variable. Its easier to read and write.

Syntax:

case $variable-name in choice1) commands ;; choice2) commands ;; .... ....Esac

The $variable-name is compared against the cases until a match is found. The shell then executes all the statements up to the two semicolons that are next to each other. The default is *) and its executed if no match is found. For e.g. write script as follows:

Page 11: UNIX - Class3 - Programming Constructs

Nihar R Paital

Ex: case statement

echo enter value for x read xcase $x in1)ls;;2)cal;;3)date;;*)echo invalidesac

Page 12: UNIX - Class3 - Programming Constructs

Nihar R Paital

Useful Shell Scripting commands.

break– To come out of a loop.

continue– To jump to the start of loop.

exit– To prematurely terminate a program.

# – To interpret the rest of line as comments.

Page 13: UNIX - Class3 - Programming Constructs

Nihar R Paital