UNIX - Class3 - Programming Constructs

1,171 views

Published on

It will provide brief knowledge regarding different types of constructs such as conditional statement execution and looping etc, which is crucial for shell Scripting.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,171
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
55
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

UNIX - Class3 - Programming Constructs

  1. 1. UNIX Programming Construct Presentation By Nihar R Paital
  2. 2. 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=0 echo "1. Unix (Sun Os)" echo "2. Linux (Red Hat)" echo -n "Select your os choice [1 or 2]? " read chos if [ $chos -eq 1 ] ; then echo "You Pick up Unix (Sun Os)" else if [ $chos -eq 2 ] ; then echo "You Pick up Linux (Red Hat)" else echo "What you dont like Unix/Linux OS." fi fi Nihar R Paital
  3. 3. Multilevel if-then-else #!/bin/ksh echo Enter a number : read n if [ $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 Nihar R Paital
  4. 4. Loops in Shell ScriptsBash supports: – for loop – while loopIn each and every loop,(e) 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. Nihar R Paital
  5. 5. for Loop 1) for x in 10 20 30 40 50 do echo $x done 2) Ex: Arrays with for loop #!/bin/ksh y="shell scripting Training" for x in ${y[*]} do echo $x done Nihar R Paital
  6. 6. Ex: for statement 3) for x in `ls` do echo $x done 4) for x in `ls` `cat forloop` do echo $x done Nihar R Paital
  7. 7. while loop Syntax: while [ condition ] do command1 command2 command3 .. .... done Nihar R Paital
  8. 8. Ex: while statement. x=1 while [ $x -lt 10 ] do echo $x x=`expr $x + 1` done Nihar R Paital
  9. 9. until statementSyntax: until control command do <commands> DoneEx: x=1 until [ $x -gt 10 ] do echo $x x=`expr $x + 1` done Nihar R Paital
  10. 10. 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 ;; .... ....EsacThe $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: Nihar R Paital
  11. 11. Ex: case statement echo enter value for x read x case $x in 1)ls;; 2)cal;; 3)date;; *)echo invalid esac Nihar R Paital
  12. 12. 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. Nihar R Paital
  13. 13. Nihar R Paital

×