SlideShare a Scribd company logo
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
Ad

Recommended

UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
Nihar Ranjan Paital
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
Nihar Ranjan Paital
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
Nihar Ranjan Paital
 
Subroutines
Subroutines
Krasimir Berov (Красимир Беров)
 
Shell scripting
Shell scripting
Mufaddal Haidermota
 
Scalar data types
Scalar data types
Krasimir Berov (Красимир Беров)
 
Lists and arrays
Lists and arrays
Krasimir Berov (Красимир Беров)
 
Lecture 22
Lecture 22
rhshriva
 
Subroutines
Subroutines
primeteacher32
 
Subroutines in perl
Subroutines in perl
sana mateen
 
Awk programming
Awk programming
Dr.M.Karthika parthasarathy
 
Syntax
Syntax
Krasimir Berov (Красимир Беров)
 
Awk essentials
Awk essentials
Logan Palanisamy
 
Learning sed and awk
Learning sed and awk
Yogesh Sawant
 
PHP7. Game Changer.
PHP7. Game Changer.
Haim Michael
 
Petitparser at the Deep into Smalltalk School 2011
Petitparser at the Deep into Smalltalk School 2011
Tudor Girba
 
Mastering Grammars with PetitParser
Mastering Grammars with PetitParser
Lukas Renggli
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Licão 13 functions
Licão 13 functions
Acácio Oliveira
 
Perl 101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
Utkarsh Sengar
 
Practical approach to perl day2
Practical approach to perl day2
Rakesh Mukundan
 
Intro to Perl and Bioperl
Intro to Perl and Bioperl
Bioinformatics and Computational Biosciences Branch
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Perl programming language
Perl programming language
Elie Obeid
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
String variable in php
String variable in php
chantholnet
 
Perl Scripting
Perl Scripting
Varadharajan Mukundan
 
Improving Dev Assistant
Improving Dev Assistant
Dave Cross
 
OS.pdf
OS.pdf
ErPawanKumar3
 
Scripting ppt
Scripting ppt
anamichintu
 

More Related Content

What's hot (20)

Subroutines
Subroutines
primeteacher32
 
Subroutines in perl
Subroutines in perl
sana mateen
 
Awk programming
Awk programming
Dr.M.Karthika parthasarathy
 
Syntax
Syntax
Krasimir Berov (Красимир Беров)
 
Awk essentials
Awk essentials
Logan Palanisamy
 
Learning sed and awk
Learning sed and awk
Yogesh Sawant
 
PHP7. Game Changer.
PHP7. Game Changer.
Haim Michael
 
Petitparser at the Deep into Smalltalk School 2011
Petitparser at the Deep into Smalltalk School 2011
Tudor Girba
 
Mastering Grammars with PetitParser
Mastering Grammars with PetitParser
Lukas Renggli
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Licão 13 functions
Licão 13 functions
Acácio Oliveira
 
Perl 101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
Utkarsh Sengar
 
Practical approach to perl day2
Practical approach to perl day2
Rakesh Mukundan
 
Intro to Perl and Bioperl
Intro to Perl and Bioperl
Bioinformatics and Computational Biosciences Branch
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
 
Perl programming language
Perl programming language
Elie Obeid
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
String variable in php
String variable in php
chantholnet
 
Perl Scripting
Perl Scripting
Varadharajan Mukundan
 
Improving Dev Assistant
Improving Dev Assistant
Dave Cross
 

Similar to UNIX - Class3 - Programming Constructs (20)

OS.pdf
OS.pdf
ErPawanKumar3
 
Scripting ppt
Scripting ppt
anamichintu
 
Unix shell scripts
Unix shell scripts
Prakash Lambha
 
003 scripting
003 scripting
Sherif Mousa
 
Osp2.pdf
Osp2.pdf
ErPawanKumar3
 
Unix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
Scripting ppt
Scripting ppt
anamichintu
 
Scripting 101
Scripting 101
ohardebol
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
What is a shell script
What is a shell script
Dr.M.Karthika parthasarathy
 
Shell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptx
SherinRappai
 
Licão 12 decision loops - statement iteration
Licão 12 decision loops - statement iteration
Acácio Oliveira
 
Shell programming
Shell programming
Moayad Moawiah
 
ShellProgramming and Script in operating system
ShellProgramming and Script in operating system
vinitasharma749430
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
Vpmv
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
Eliezer Moraes
 
Licão 11 decision making - statement
Licão 11 decision making - statement
Acácio Oliveira
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
Bash Programming
Bash Programming
Kiplangat Chelule
 
First steps in C-Shell
First steps in C-Shell
Brahma Killampalli
 
Unix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
Scripting 101
Scripting 101
ohardebol
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
Shell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptx
SherinRappai
 
Licão 12 decision loops - statement iteration
Licão 12 decision loops - statement iteration
Acácio Oliveira
 
ShellProgramming and Script in operating system
ShellProgramming and Script in operating system
vinitasharma749430
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
Vpmv
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
Eliezer Moraes
 
Licão 11 decision making - statement
Licão 11 decision making - statement
Acácio Oliveira
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
Ad

More from Nihar Ranjan Paital (8)

Oracle Select Query
Oracle Select Query
Nihar Ranjan Paital
 
Useful macros and functions for excel
Useful macros and functions for excel
Nihar Ranjan Paital
 
Unix - Class7 - awk
Unix - Class7 - awk
Nihar Ranjan Paital
 
UNIX - Class2 - vi Editor
UNIX - Class2 - vi Editor
Nihar Ranjan Paital
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
Nihar Ranjan Paital
 
Test funda
Test funda
Nihar Ranjan Paital
 
Csql for telecom
Csql for telecom
Nihar Ranjan Paital
 
Select Operations in CSQL
Select Operations in CSQL
Nihar Ranjan Paital
 
Ad

Recently uploaded (20)

cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
PyCon SG 25 - Firecracker Made Easy with Python.pdf
PyCon SG 25 - Firecracker Made Easy with Python.pdf
Muhammad Yuga Nugraha
 
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
 
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Seminar: Targeting Trust: The Future of Identity in the Workforce.pptx
FIDO Alliance
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
9-1-1 Addressing: End-to-End Automation Using FME
9-1-1 Addressing: End-to-End Automation Using FME
Safe Software
 
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
Information Security Response Team Nepal_npCERT_Vice_President_Sudan_Jha.pdf
ICT Frame Magazine Pvt. Ltd.
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
"How to survive Black Friday: preparing e-commerce for a peak season", Yurii ...
Fwdays
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
From Manual to Auto Searching- FME in the Driver's Seat
From Manual to Auto Searching- FME in the Driver's Seat
Safe Software
 
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 

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