SlideShare une entreprise Scribd logo
1  sur  13
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

Contenu connexe

Tendances

Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
Utkarsh Sengar
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 
String variable in php
String variable in phpString variable in php
String variable in php
chantholnet
 

Tendances (20)

Subroutines
SubroutinesSubroutines
Subroutines
 
Subroutines in perl
Subroutines in perlSubroutines in perl
Subroutines in perl
 
Awk programming
Awk programming Awk programming
Awk programming
 
Syntax
SyntaxSyntax
Syntax
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
 
Learning sed and awk
Learning sed and awkLearning sed and awk
Learning sed and awk
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
 
Petitparser at the Deep into Smalltalk School 2011
Petitparser at the Deep into Smalltalk School 2011Petitparser at the Deep into Smalltalk School 2011
Petitparser at the Deep into Smalltalk School 2011
 
Mastering Grammars with PetitParser
Mastering Grammars with PetitParserMastering Grammars with PetitParser
Mastering Grammars with PetitParser
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
 
Licão 13 functions
Licão 13 functionsLicão 13 functions
Licão 13 functions
 
Perl 101 - The Basics of Perl Programming
Perl  101 - The Basics of Perl ProgrammingPerl  101 - The Basics of Perl Programming
Perl 101 - The Basics of Perl Programming
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
String variable in php
String variable in phpString variable in php
String variable in php
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
Improving Dev Assistant
Improving Dev AssistantImproving Dev Assistant
Improving Dev Assistant
 

Similaire à UNIX - Class3 - Programming Constructs

Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
MARRY7
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
adkinspaige22
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
vibrantuser
 

Similaire à UNIX - Class3 - Programming Constructs (20)

Scripting ppt
Scripting pptScripting ppt
Scripting ppt
 
OS.pdf
OS.pdfOS.pdf
OS.pdf
 
Shell programming
Shell programmingShell programming
Shell programming
 
Unix
UnixUnix
Unix
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
003 scripting
003 scripting003 scripting
003 scripting
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Osp2.pdf
Osp2.pdfOsp2.pdf
Osp2.pdf
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 
Scripting 101
Scripting 101Scripting 101
Scripting 101
 
Best training-in-mumbai-shell scripting
Best training-in-mumbai-shell scriptingBest training-in-mumbai-shell scripting
Best training-in-mumbai-shell scripting
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
390aLecture05_12sp.ppt
390aLecture05_12sp.ppt390aLecture05_12sp.ppt
390aLecture05_12sp.ppt
 
Scripting ppt
Scripting pptScripting ppt
Scripting ppt
 
Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
 

Plus de Nihar Ranjan Paital (8)

Oracle Select Query
Oracle Select QueryOracle Select Query
Oracle Select Query
 
Useful macros and functions for excel
Useful macros and functions for excelUseful macros and functions for excel
Useful macros and functions for excel
 
Unix - Class7 - awk
Unix - Class7 - awkUnix - Class7 - awk
Unix - Class7 - awk
 
UNIX - Class2 - vi Editor
UNIX - Class2 - vi EditorUNIX - Class2 - vi Editor
UNIX - Class2 - vi Editor
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
Test funda
Test fundaTest funda
Test funda
 
Csql for telecom
Csql for telecomCsql for telecom
Csql for telecom
 
Select Operations in CSQL
Select Operations in CSQLSelect Operations in CSQL
Select Operations in CSQL
 

Dernier

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

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