UNIX




 Programming Construct



           Presentation By

                     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=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 don't like Unix/Linux OS."
                 fi
          fi
                                                                        Nihar R Paital
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
Loops in Shell Scripts
Bash supports:

      –   for loop
      –   while loop

In 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
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
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
while loop
   Syntax:

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




                             Nihar R Paital
Ex: while statement.


    x=1
    while [ $x -lt 10 ]
    do
    echo $x
    x=`expr $x + 1`
    done




                          Nihar R Paital
until statement
Syntax:
      until control command
      do
          <commands>
      Done
Ex:
      x=1
      until [ $x -gt 10 ]
      do
      echo $x
      x=`expr $x + 1`
      done



                              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:
                                                                  Nihar R Paital
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
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
Nihar R Paital

More Related Content

PPS
UNIX - Class4 - Advance Shell Scripting-P1
PPS
UNIX - Class5 - Advance Shell Scripting-P2
PPS
UNIX - Class1 - Basic Shell
PPTX
Shell scripting
PDF
Lecture 22
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class1 - Basic Shell
Shell scripting
Lecture 22

What's hot (20)

PPTX
Subroutines
PPTX
Subroutines in perl
DOCX
Awk programming
PPSX
Awk essentials
PPT
Learning sed and awk
PDF
PHP7. Game Changer.
PDF
Petitparser at the Deep into Smalltalk School 2011
PDF
Mastering Grammars with PetitParser
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
PPTX
Licão 13 functions
PPT
Perl 101 - The Basics of Perl Programming
PDF
Practical approach to perl day2
PPTX
Strings,patterns and regular expressions in perl
PDF
Perl programming language
ODP
Introduction to Perl - Day 1
PPTX
String variable in php
PDF
Perl Scripting
PDF
Improving Dev Assistant
Subroutines
Subroutines in perl
Awk programming
Awk essentials
Learning sed and awk
PHP7. Game Changer.
Petitparser at the Deep into Smalltalk School 2011
Mastering Grammars with PetitParser
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Licão 13 functions
Perl 101 - The Basics of Perl Programming
Practical approach to perl day2
Strings,patterns and regular expressions in perl
Perl programming language
Introduction to Perl - Day 1
String variable in php
Perl Scripting
Improving Dev Assistant
Ad

Similar to UNIX - Class3 - Programming Constructs (20)

PDF
PPTX
Scripting ppt
PPTX
Unix shell scripts
PDF
003 scripting
PDF
Osp2.pdf
PPTX
Unix Shell Programming subject shell scripting ppt
PPTX
Scripting ppt
PPTX
Scripting 101
PPTX
AOS_Module_3ssssssssssssssssssssssssssss.pptx
DOCX
What is a shell script
PPTX
Shell Programming Language in Operating System .pptx
PPTX
Licão 12 decision loops - statement iteration
PPT
Shell programming
PPT
ShellProgramming and Script in operating system
PPTX
Case, Loop & Command line args un Unix
PPT
Advanced linux chapter ix-shell script
PPTX
Licão 11 decision making - statement
PPT
34-shell-programming asda asda asd asd.ppt
PPT
Bash Programming
PPTX
First steps in C-Shell
Scripting ppt
Unix shell scripts
003 scripting
Osp2.pdf
Unix Shell Programming subject shell scripting ppt
Scripting ppt
Scripting 101
AOS_Module_3ssssssssssssssssssssssssssss.pptx
What is a shell script
Shell Programming Language in Operating System .pptx
Licão 12 decision loops - statement iteration
Shell programming
ShellProgramming and Script in operating system
Case, Loop & Command line args un Unix
Advanced linux chapter ix-shell script
Licão 11 decision making - statement
34-shell-programming asda asda asd asd.ppt
Bash Programming
First steps in C-Shell
Ad

More from Nihar Ranjan Paital (8)

PPS
Oracle Select Query
PDF
Useful macros and functions for excel
PPS
Unix - Class7 - awk
PPS
UNIX - Class2 - vi Editor
PPS
UNIX - Class6 - sed - Detail
PDF
PPT
Csql for telecom
PPT
Select Operations in CSQL
Oracle Select Query
Useful macros and functions for excel
Unix - Class7 - awk
UNIX - Class2 - vi Editor
UNIX - Class6 - sed - Detail
Csql for telecom
Select Operations in CSQL

Recently uploaded (20)

PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
STKI Israel Market Study 2025 version august
PDF
Comparative analysis of machine learning models for fake news detection in so...
PPTX
Build Your First AI Agent with UiPath.pptx
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Architecture types and enterprise applications.pdf
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Enhancing plagiarism detection using data pre-processing and machine learning...
PDF
CloudStack 4.21: First Look Webinar slides
PDF
Getting started with AI Agents and Multi-Agent Systems
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PDF
Five Habits of High-Impact Board Members
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
STKI Israel Market Study 2025 version august
Comparative analysis of machine learning models for fake news detection in so...
Build Your First AI Agent with UiPath.pptx
Credit Without Borders: AI and Financial Inclusion in Bangladesh
sustainability-14-14877-v2.pddhzftheheeeee
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
A review of recent deep learning applications in wood surface defect identifi...
Architecture types and enterprise applications.pdf
Final SEM Unit 1 for mit wpu at pune .pptx
Basics of Cloud Computing - Cloud Ecosystem
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Enhancing plagiarism detection using data pre-processing and machine learning...
CloudStack 4.21: First Look Webinar slides
Getting started with AI Agents and Multi-Agent Systems
Module 1 Introduction to Web Programming .pptx
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
Five Habits of High-Impact Board Members

UNIX - Class3 - Programming Constructs

  • 1. UNIX Programming Construct Presentation By Nihar R Paital
  • 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 don't like Unix/Linux OS." fi fi Nihar R Paital
  • 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. Loops in Shell Scripts Bash supports: – for loop – while loop In 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. 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. 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. while loop  Syntax: while [ condition ] do command1 command2 command3 .. .... done Nihar R Paital
  • 8. Ex: while statement. x=1 while [ $x -lt 10 ] do echo $x x=`expr $x + 1` done Nihar R Paital
  • 9. until statement Syntax: until control command do <commands> Done Ex: x=1 until [ $x -gt 10 ] do echo $x x=`expr $x + 1` done Nihar R Paital
  • 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 ;; .... .... 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: Nihar R Paital
  • 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. 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