SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Downloaden Sie, um offline zu lesen
Tutor Session - 3




Chulalongkorn
                                                                           Tutor Session III:


                               UNIX Shell Script
 University




                                  Programming

                Wongyos Keardsri (P’Bank)
                Department of Computer Engineering
                Faculty of Engineering, Chulalongkorn University
                Bangkok, Thailand
                Mobile Phone: 089-5993490
                E-mail: wongyos@gmail.com, MSN: bankberrer@hotmail.com
                Twitter: @wongyos
                                                 2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                     Tutor Outline
Chulalongkorn
 University



                Introduction to Shell               Data Operations
                   Shell                            Decision Statements
                   Shell Script                          If-else
                Variables                                Switch-case
                   Creating/Assigning               Iteration Statement
                   Accessing                             For
                   Setting                               While
                Getting Start Shell Script
                   Include shell
                   Create shell
                   Run shell

         2                                   2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                    Introduction to Shell
Chulalongkorn
 University




                What is the shell or Unix shell?
                  A command-line interpreter and script host that
                  provides a traditional user interface for the Unix
                  operating system and for Unix-like systems
                  There are many shells; sh, bash, ksh, csh, zsh, …
                Bourne Shell (sh)
                  Written by Stephen Bourne
                  Was the 1st popular Unix shell



         3                                 2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                   Introduction to Shell
Chulalongkorn
 University                                                                     (Cont)
                Bourne Shell (sh) (Cont)
                  Available on all Unix systems
                  Supports a fairly versatile programming language
                  A subset of more powerful Korn shell (ksh)
                  Implement with regular C programming
                  Executable file is stored as /bin/sh




         4                                2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                               Variables
Chulalongkorn
 University


                Creating and assigning a variable
                        name=value
                                                    No spaces
                Printing/Showing a variable value
                        echo $name
                                                    With spaces
                Setting environment variable
                        export NAME

                Read only variable
                       readonly name
         5                              2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                                  Variables
Chulalongkorn
 University




                Example
                $ age=15
                $ nickname=Bank
                $ echo I'm $nickname, $age years old



                     More an examples by yourself




         6                                 2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                                      Variables
Chulalongkorn
 University                                                                          (Cont)
                Accessing a variable
                   Syntax                                Action
                $name          Replaced by the value of name.
                ${name}        Replaced by the value of name.
                ${name-word}   Replaced by the value of name if set, and word otherwise.
                ${name+word}   Replaced by word if name is set, and nothing otherwise.
                ${name=word}   Assign word to the variable name if name is not already
                               set and then replaced by the value of name.
                ${name?word}   Replaced by name if name is set. If name is not set, word
                               is displayed to the standard error and the shell is exited.



         7                                     2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                                    Variables
Chulalongkorn
 University                                                                        (Cont)
                Example
                $   verb=sing
                $   echo I like $verbing
                I   like
                $   echo I like ${verb}ing
                I   like singing



                       More an examples by yourself




         8                                   2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                            Getting Start Shell Script
Chulalongkorn
 University




                What is the shell script?
                  Similar to DOS batch files
                  Quick and simple programming
                  Text file interpreted by shell, effectively new command
                  List of shell commands to be run sequentially
                  Typical operations for file manipulation, program
                  execution, and printing text




         9                                2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                            Getting Start Shell Script
Chulalongkorn
 University                                                                    (Cont)
                Include full path to interpreter (shell)
                       #!/path/shell

                Example
                 #!/bin/sh
                 #!/usr/bin/sh
                 #!/bin/csh -f




         10                              2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                           Getting Start Shell Script
Chulalongkorn
 University                                                                 (Cont)
                Using vi command to create shell script file
                Running shell script by using the command below
                 sh [file]

                Example
                 $ vi test.sh
                 ...
                 $ sh test.sh




         11                           2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                            Getting Start Shell Script
Chulalongkorn
 University                                                                   (Cont)
                Interaction with user
                  Output value
                   echo [texts/variables]
                  Input value
                   read [variables]

                Comment line
                 # your comments


         12                             2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                            Getting Start Shell Script
Chulalongkorn
 University                                                                   (Cont)
                Special Variables
                  $#      Number of arguments on command line
                  $0      Name that script was called as
                  $1-$9   Command line arguments
                  $*      All arguments
                  $@      All arguments (separately quoted)
                  $?      Numeric result code of previous command
                  $$      Process ID of this running script



         13                             2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                            Getting Start Shell Script
Chulalongkorn
 University                                                                      (Cont)
                Example
                $ cat example1.sh
                echo there are $# command line arguments: $@
                $ sh example1.sh
                there are 0 command line arguments:
                $ sh example1.sh x y z
                there are 3 command line arguments: x y z


                     More an examples by yourself



         14                                2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                              Data Operation
Chulalongkorn
 University




                Operators
                    Operators                        Meaning
            * / %               multiplication, division, remainder
            + -                 addition, subtraction
            = != > < >= <=      comparison operators
            &                   logical and
            |                   logical or

                Using expr to excute operators
                 expr $va1 op $var2

         15                             2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                  Data Operation
Chulalongkorn
 University                                                                          (Cont)
                Conditional Expressions
                test [expression]
                  test returns a zero exit code if expression evaluates to true;
                  otherwise, nonzero exit status

                test forms
                  -d   filename         True if filname exists as a directory file
                  -f   filename         True if filname exists as a nondirectory file
                  -l   string           True if length of string is nonzero
                  -n   string           True if string contains at least one character


         16                                    2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                            Data Operation
Chulalongkorn
 University                                                                   (Cont)
                test forms (Cont)
                  -r filename     True if filname exists as a readable file
                  -w filename     True if filname exists as a writable file
                  -x filename     True if filname exists as an executable file
                  -z string       True if string contains no characters
                  str1 = str2     True if str1 is equal to str2
                  str1 != str2    True if str1 is not equal to str2
                  string          True if string is not null
                  int1 -eq int2   True if int1 is equal to int2
                  int1 -ne int2   True if int1 is not equal to int2
                  int1 -gt int2   True if int1 is greater than int2

         17                             2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                               Data Operation
Chulalongkorn
 University                                                                       (Cont)
                test forms (Cont)
                  int1 -ge   int2  True if int1 is greater than or equal to int2
                  int1 -lt   int2  True if int1 is less than int2
                  int1 -le   int2  True if int1 is less than or equalt to int2
                  !expr            True if expr is false
                  expr1 -a   expr2 True if ezpr1 and expr2 are true
                  expr1 -o   expr2 True if ezpr1 or expr2 are true




         18                                 2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                    Decision Statement
Chulalongkorn
 University




                If-else statement
                if [condition]
                   then [result]
                elif [condition]
                   then [result]
                else
                   [result]
                fi




         19                           2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                     Decision Statement
Chulalongkorn
 University                                                                    (Cont)
                Example
                if test -r file1
                   then echo "file1"
                elif [ -r file2 ]         test –r file2
                   then cp file2 file3
                   echo "file2 copy to file3"
                else
                   echo "no file"
                fi

                          More the examples by yourself


         20                              2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                  Decision Statement
Chulalongkorn
 University                                                                   (Cont)
                Switch-case statement
                case $var in
                  value1) [result] ;;
                  value2) [result] ;;
                  ...
                  *) [result] ;;
                                                   Default case
                esac




         21                             2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                     Decision Statement
Chulalongkorn
 University                                                                     (Cont)
                Example
                case $day in
                  Monday ) echo "A new week" ;;
                  Saturday | Sunday ) echo "Free" ;;
                  Friday ) echo "Hooray !!" ;;
                  * ) echo "It is $DAY" ;;
                esac


                      More the examples by yourself




         22                               2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                         Iteration Statement
Chulalongkorn
 University




                For statement
                for var {in [word]+}
                do
                   [result]
                done
                  Iterate the value of the variable var through each word in the word
                  list
                  Evaluate the command in list after each iteration
                  If no word is supplied, $@ ($1 ..) is used instead
                  A break command causes the loop to terminate
                  A continue command causes the loop to jump to the next iteration
         23                                  2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                      Iteration Statement
Chulalongkorn
 University                                                                     (Cont)
                Example
                for color in red yellow green blue
                  do echo one color is $color
                done

                for x
                  do echo x = $x
                done


                      More the examples by yourself


         24                               2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                  Iteration Statement
Chulalongkorn
 University                                                               (Cont)
                While statement
                while [condition]
                do                     test $var1 –opt $var2
                   [result]
                done




         25                         2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                   Iteration Statement
Chulalongkorn
 University                                                                  (Cont)
                Example
                while true
                do
                   who | grep u51xxx
                   sleep 30
                done
                                         More the examples by yourself
                x=1
                while test $x -le 10
                do
                   echo x is $x
                   x=`expr $x + 1`
                done
         26                            2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                          See More
Chulalongkorn
 University



         [1] http://www.grymoire.com/Unix/Sh.html
         [2] http://www.ooblick.com/text/sh/
         [3] http://www.injunea.demon.co.uk/pages/page204.htm




         27                          2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3



                                                               End
Chulalongkorn
 University




                Question ?


                           … Answer
         28              2110313 Operating Systems and System Programs (1/2010)

Weitere ähnliche Inhalte

Andere mochten auch

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramWongyos Keardsri
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Wongyos Keardsri
 
Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsWongyos Keardsri
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsWongyos Keardsri
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsWongyos Keardsri
 
Final morris esri_nwgis_lidar
Final morris esri_nwgis_lidarFinal morris esri_nwgis_lidar
Final morris esri_nwgis_lidarEric Morris
 
Ch2(working with forms)
Ch2(working with forms)Ch2(working with forms)
Ch2(working with forms)Chhom Karath
 
Appendex a
Appendex aAppendex a
Appendex aswavicky
 
Appendex b
Appendex bAppendex b
Appendex bswavicky
 
Chapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power SuppliesChapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power SuppliesPatty Ramsey
 
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceGuidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceeWater
 
CIS 110 Chapter 1 Intro to Computers
CIS 110 Chapter 1 Intro to ComputersCIS 110 Chapter 1 Intro to Computers
CIS 110 Chapter 1 Intro to ComputersPatty Ramsey
 
Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Chhom Karath
 
Appendex e
Appendex eAppendex e
Appendex eswavicky
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPprabhatjon
 

Andere mochten auch (20)

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master Program
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
k10947 Ppt ic
k10947 Ppt ick10947 Ppt ic
k10947 Ppt ic
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)
 
Java-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and ObjectsJava-Chapter 13 Advanced Classes and Objects
Java-Chapter 13 Advanced Classes and Objects
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and Objects
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applications
 
Final morris esri_nwgis_lidar
Final morris esri_nwgis_lidarFinal morris esri_nwgis_lidar
Final morris esri_nwgis_lidar
 
Ch2(working with forms)
Ch2(working with forms)Ch2(working with forms)
Ch2(working with forms)
 
Appendex a
Appendex aAppendex a
Appendex a
 
Appendex b
Appendex bAppendex b
Appendex b
 
Chapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power SuppliesChapter 4 Form Factors & Power Supplies
Chapter 4 Form Factors & Power Supplies
 
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater SourceGuidelines for Modelling Groundwater Surface Water Interaction in eWater Source
Guidelines for Modelling Groundwater Surface Water Interaction in eWater Source
 
CIS 110 Chapter 1 Intro to Computers
CIS 110 Chapter 1 Intro to ComputersCIS 110 Chapter 1 Intro to Computers
CIS 110 Chapter 1 Intro to Computers
 
C# programs
C# programsC# programs
C# programs
 
Php i-slides
Php i-slidesPhp i-slides
Php i-slides
 
Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)Ch7(publishing my sql data on the web)
Ch7(publishing my sql data on the web)
 
Appendex e
Appendex eAppendex e
Appendex e
 
Unix Master
Unix MasterUnix Master
Unix Master
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Ähnlich wie SysProg-Tutor 03 Unix Shell Script Programming

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 ProgrammingUtkarsh Sengar
 
CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...
CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...
CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...ijdms
 
Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...
Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...
Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...ijdms
 
unix training | unix training videos | unix course unix online training
unix training |  unix training videos |  unix course  unix online training unix training |  unix training videos |  unix course  unix online training
unix training | unix training videos | unix course unix online training Nancy Thomas
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Scriptstudent
 
SELFLESS INHERITANCE
SELFLESS INHERITANCESELFLESS INHERITANCE
SELFLESS INHERITANCEijpla
 
Cross-lingual event-mining using wordnet as a shared knowledge interface
Cross-lingual event-mining using wordnet as a shared knowledge interfaceCross-lingual event-mining using wordnet as a shared knowledge interface
Cross-lingual event-mining using wordnet as a shared knowledge interfacepathsproject
 
Previewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML DatabasePreviewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML DatabaseChristoph Lange
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented CollaborationAlena Holligan
 

Ähnlich wie SysProg-Tutor 03 Unix Shell Script Programming (10)

UML01
UML01UML01
UML01
 
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
 
CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...
CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...
CONSIDERING STRUCTURAL AND VOCABULARY HETEROGENEITY IN XML QUERY: FPTPQ AND H...
 
Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...
Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...
Considering Structural and Vocabulary Heterogeneity in XML Query: FPTPQ and H...
 
unix training | unix training videos | unix course unix online training
unix training |  unix training videos |  unix course  unix online training unix training |  unix training videos |  unix course  unix online training
unix training | unix training videos | unix course unix online training
 
Unix Shell Script
Unix Shell ScriptUnix Shell Script
Unix Shell Script
 
SELFLESS INHERITANCE
SELFLESS INHERITANCESELFLESS INHERITANCE
SELFLESS INHERITANCE
 
Cross-lingual event-mining using wordnet as a shared knowledge interface
Cross-lingual event-mining using wordnet as a shared knowledge interfaceCross-lingual event-mining using wordnet as a shared knowledge interface
Cross-lingual event-mining using wordnet as a shared knowledge interface
 
Previewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML DatabasePreviewing OWL Changes and Refactorings Using a Flexible XML Database
Previewing OWL Changes and Refactorings Using a Flexible XML Database
 
Obect-Oriented Collaboration
Obect-Oriented CollaborationObect-Oriented Collaboration
Obect-Oriented Collaboration
 

Mehr von Wongyos Keardsri

Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IWongyos Keardsri
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsWongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsWongyos Keardsri
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityWongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 CountingWongyos Keardsri
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsWongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIWongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IWongyos Keardsri
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesWongyos Keardsri
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesWongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationWongyos Keardsri
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowWongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos Keardsri
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysWongyos Keardsri
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsWongyos Keardsri
 

Mehr von Wongyos Keardsri (20)

Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
Discrete-Chapter 09 Algorithms
Discrete-Chapter 09 AlgorithmsDiscrete-Chapter 09 Algorithms
Discrete-Chapter 09 Algorithms
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
 
Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
 
Discrete-Chapter 03 Matrices
Discrete-Chapter 03 MatricesDiscrete-Chapter 03 Matrices
Discrete-Chapter 03 Matrices
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and Sequences
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindow
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional Arrays
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and Applications
 
Java-Chapter 08 Methods
Java-Chapter 08 MethodsJava-Chapter 08 Methods
Java-Chapter 08 Methods
 

Kürzlich hochgeladen

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
[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.pdfhans926745
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
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 RobisonAnna Loughnan Colquhoun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
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.pdfsudhanshuwaghmare1
 
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 textsMaria Levchenko
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Kürzlich hochgeladen (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
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
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

SysProg-Tutor 03 Unix Shell Script Programming

  • 1. Tutor Session - 3 Chulalongkorn Tutor Session III: UNIX Shell Script University Programming Wongyos Keardsri (P’Bank) Department of Computer Engineering Faculty of Engineering, Chulalongkorn University Bangkok, Thailand Mobile Phone: 089-5993490 E-mail: wongyos@gmail.com, MSN: bankberrer@hotmail.com Twitter: @wongyos 2110313 Operating Systems and System Programs (1/2010)
  • 2. Tutor Session - 3 Tutor Outline Chulalongkorn University Introduction to Shell Data Operations Shell Decision Statements Shell Script If-else Variables Switch-case Creating/Assigning Iteration Statement Accessing For Setting While Getting Start Shell Script Include shell Create shell Run shell 2 2110313 Operating Systems and System Programs (1/2010)
  • 3. Tutor Session - 3 Introduction to Shell Chulalongkorn University What is the shell or Unix shell? A command-line interpreter and script host that provides a traditional user interface for the Unix operating system and for Unix-like systems There are many shells; sh, bash, ksh, csh, zsh, … Bourne Shell (sh) Written by Stephen Bourne Was the 1st popular Unix shell 3 2110313 Operating Systems and System Programs (1/2010)
  • 4. Tutor Session - 3 Introduction to Shell Chulalongkorn University (Cont) Bourne Shell (sh) (Cont) Available on all Unix systems Supports a fairly versatile programming language A subset of more powerful Korn shell (ksh) Implement with regular C programming Executable file is stored as /bin/sh 4 2110313 Operating Systems and System Programs (1/2010)
  • 5. Tutor Session - 3 Variables Chulalongkorn University Creating and assigning a variable name=value No spaces Printing/Showing a variable value echo $name With spaces Setting environment variable export NAME Read only variable readonly name 5 2110313 Operating Systems and System Programs (1/2010)
  • 6. Tutor Session - 3 Variables Chulalongkorn University Example $ age=15 $ nickname=Bank $ echo I'm $nickname, $age years old More an examples by yourself 6 2110313 Operating Systems and System Programs (1/2010)
  • 7. Tutor Session - 3 Variables Chulalongkorn University (Cont) Accessing a variable Syntax Action $name Replaced by the value of name. ${name} Replaced by the value of name. ${name-word} Replaced by the value of name if set, and word otherwise. ${name+word} Replaced by word if name is set, and nothing otherwise. ${name=word} Assign word to the variable name if name is not already set and then replaced by the value of name. ${name?word} Replaced by name if name is set. If name is not set, word is displayed to the standard error and the shell is exited. 7 2110313 Operating Systems and System Programs (1/2010)
  • 8. Tutor Session - 3 Variables Chulalongkorn University (Cont) Example $ verb=sing $ echo I like $verbing I like $ echo I like ${verb}ing I like singing More an examples by yourself 8 2110313 Operating Systems and System Programs (1/2010)
  • 9. Tutor Session - 3 Getting Start Shell Script Chulalongkorn University What is the shell script? Similar to DOS batch files Quick and simple programming Text file interpreted by shell, effectively new command List of shell commands to be run sequentially Typical operations for file manipulation, program execution, and printing text 9 2110313 Operating Systems and System Programs (1/2010)
  • 10. Tutor Session - 3 Getting Start Shell Script Chulalongkorn University (Cont) Include full path to interpreter (shell) #!/path/shell Example #!/bin/sh #!/usr/bin/sh #!/bin/csh -f 10 2110313 Operating Systems and System Programs (1/2010)
  • 11. Tutor Session - 3 Getting Start Shell Script Chulalongkorn University (Cont) Using vi command to create shell script file Running shell script by using the command below sh [file] Example $ vi test.sh ... $ sh test.sh 11 2110313 Operating Systems and System Programs (1/2010)
  • 12. Tutor Session - 3 Getting Start Shell Script Chulalongkorn University (Cont) Interaction with user Output value echo [texts/variables] Input value read [variables] Comment line # your comments 12 2110313 Operating Systems and System Programs (1/2010)
  • 13. Tutor Session - 3 Getting Start Shell Script Chulalongkorn University (Cont) Special Variables $# Number of arguments on command line $0 Name that script was called as $1-$9 Command line arguments $* All arguments $@ All arguments (separately quoted) $? Numeric result code of previous command $$ Process ID of this running script 13 2110313 Operating Systems and System Programs (1/2010)
  • 14. Tutor Session - 3 Getting Start Shell Script Chulalongkorn University (Cont) Example $ cat example1.sh echo there are $# command line arguments: $@ $ sh example1.sh there are 0 command line arguments: $ sh example1.sh x y z there are 3 command line arguments: x y z More an examples by yourself 14 2110313 Operating Systems and System Programs (1/2010)
  • 15. Tutor Session - 3 Data Operation Chulalongkorn University Operators Operators Meaning * / % multiplication, division, remainder + - addition, subtraction = != > < >= <= comparison operators & logical and | logical or Using expr to excute operators expr $va1 op $var2 15 2110313 Operating Systems and System Programs (1/2010)
  • 16. Tutor Session - 3 Data Operation Chulalongkorn University (Cont) Conditional Expressions test [expression] test returns a zero exit code if expression evaluates to true; otherwise, nonzero exit status test forms -d filename True if filname exists as a directory file -f filename True if filname exists as a nondirectory file -l string True if length of string is nonzero -n string True if string contains at least one character 16 2110313 Operating Systems and System Programs (1/2010)
  • 17. Tutor Session - 3 Data Operation Chulalongkorn University (Cont) test forms (Cont) -r filename True if filname exists as a readable file -w filename True if filname exists as a writable file -x filename True if filname exists as an executable file -z string True if string contains no characters str1 = str2 True if str1 is equal to str2 str1 != str2 True if str1 is not equal to str2 string True if string is not null int1 -eq int2 True if int1 is equal to int2 int1 -ne int2 True if int1 is not equal to int2 int1 -gt int2 True if int1 is greater than int2 17 2110313 Operating Systems and System Programs (1/2010)
  • 18. Tutor Session - 3 Data Operation Chulalongkorn University (Cont) test forms (Cont) int1 -ge int2 True if int1 is greater than or equal to int2 int1 -lt int2 True if int1 is less than int2 int1 -le int2 True if int1 is less than or equalt to int2 !expr True if expr is false expr1 -a expr2 True if ezpr1 and expr2 are true expr1 -o expr2 True if ezpr1 or expr2 are true 18 2110313 Operating Systems and System Programs (1/2010)
  • 19. Tutor Session - 3 Decision Statement Chulalongkorn University If-else statement if [condition] then [result] elif [condition] then [result] else [result] fi 19 2110313 Operating Systems and System Programs (1/2010)
  • 20. Tutor Session - 3 Decision Statement Chulalongkorn University (Cont) Example if test -r file1 then echo "file1" elif [ -r file2 ] test –r file2 then cp file2 file3 echo "file2 copy to file3" else echo "no file" fi More the examples by yourself 20 2110313 Operating Systems and System Programs (1/2010)
  • 21. Tutor Session - 3 Decision Statement Chulalongkorn University (Cont) Switch-case statement case $var in value1) [result] ;; value2) [result] ;; ... *) [result] ;; Default case esac 21 2110313 Operating Systems and System Programs (1/2010)
  • 22. Tutor Session - 3 Decision Statement Chulalongkorn University (Cont) Example case $day in Monday ) echo "A new week" ;; Saturday | Sunday ) echo "Free" ;; Friday ) echo "Hooray !!" ;; * ) echo "It is $DAY" ;; esac More the examples by yourself 22 2110313 Operating Systems and System Programs (1/2010)
  • 23. Tutor Session - 3 Iteration Statement Chulalongkorn University For statement for var {in [word]+} do [result] done Iterate the value of the variable var through each word in the word list Evaluate the command in list after each iteration If no word is supplied, $@ ($1 ..) is used instead A break command causes the loop to terminate A continue command causes the loop to jump to the next iteration 23 2110313 Operating Systems and System Programs (1/2010)
  • 24. Tutor Session - 3 Iteration Statement Chulalongkorn University (Cont) Example for color in red yellow green blue do echo one color is $color done for x do echo x = $x done More the examples by yourself 24 2110313 Operating Systems and System Programs (1/2010)
  • 25. Tutor Session - 3 Iteration Statement Chulalongkorn University (Cont) While statement while [condition] do test $var1 –opt $var2 [result] done 25 2110313 Operating Systems and System Programs (1/2010)
  • 26. Tutor Session - 3 Iteration Statement Chulalongkorn University (Cont) Example while true do who | grep u51xxx sleep 30 done More the examples by yourself x=1 while test $x -le 10 do echo x is $x x=`expr $x + 1` done 26 2110313 Operating Systems and System Programs (1/2010)
  • 27. Tutor Session - 3 See More Chulalongkorn University [1] http://www.grymoire.com/Unix/Sh.html [2] http://www.ooblick.com/text/sh/ [3] http://www.injunea.demon.co.uk/pages/page204.htm 27 2110313 Operating Systems and System Programs (1/2010)
  • 28. Tutor Session - 3 End Chulalongkorn University Question ? … Answer 28 2110313 Operating Systems and System Programs (1/2010)