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

More Related Content

PDF
SysProg-Tutor 02 Introduction to Unix Operating System
PDF
SysProg-Tutor 01 Introduction to C Programming Language
PPS
C programming session 14
PPS
09 iec t1_s1_oo_ps_session_13
PDF
L kernel-logging-apis-pdf
PDF
PPT
Big Java Chapter 1
PDF
Java Programming Assignment
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 01 Introduction to C Programming Language
C programming session 14
09 iec t1_s1_oo_ps_session_13
L kernel-logging-apis-pdf
Big Java Chapter 1
Java Programming Assignment

Viewers also liked (20)

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

Similar to SysProg-Tutor 03 Unix Shell Script Programming (20)

PPT
1 4 sp
ODP
OpenGurukul : Language : Shell Scripting
PDF
8807290 shell-scripting
DOCX
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
DOCX
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
PDF
Lab4 scripts
PDF
Shell Script Linux
ODP
Shellscripting
PDF
Bash shell programming in linux
PPT
Unit vii wp ppt
PPTX
Shell Programming Language in Operating System .pptx
PDF
(Ebook) linux shell scripting tutorial
PDF
21bUc8YeDzZpE
PDF
21bUc8YeDzZpE
PDF
21bUc8YeDzZpE
PPT
Shell Scripting
PDF
Unix Shell Scripting
PDF
Linux shell script-1
PPT
Shell programming
PPT
390aLecture05_12sp.ppt
1 4 sp
OpenGurukul : Language : Shell Scripting
8807290 shell-scripting
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
Lab4 scripts
Shell Script Linux
Shellscripting
Bash shell programming in linux
Unit vii wp ppt
Shell Programming Language in Operating System .pptx
(Ebook) linux shell scripting tutorial
21bUc8YeDzZpE
21bUc8YeDzZpE
21bUc8YeDzZpE
Shell Scripting
Unix Shell Scripting
Linux shell script-1
Shell programming
390aLecture05_12sp.ppt
Ad

More from Wongyos Keardsri (20)

PDF
Discrete-Chapter 11 Graphs Part III
PDF
Discrete-Chapter 11 Graphs Part II
PDF
Discrete-Chapter 11 Graphs Part I
PDF
Discrete-Chapter 10 Trees
PDF
Discrete-Chapter 09 Algorithms
PDF
Discrete-Chapter 08 Relations
PDF
Discrete-Chapter 07 Probability
PDF
Discrete-Chapter 06 Counting
PDF
Discrete-Chapter 05 Inference and Proofs
PDF
Discrete-Chapter 04 Logic Part II
PDF
Discrete-Chapter 04 Logic Part I
PDF
Discrete-Chapter 03 Matrices
PDF
Discrete-Chapter 02 Functions and Sequences
PDF
Discrete-Chapter 01 Sets
PDF
Discrete-Chapter 12 Modeling Computation
PDF
Java-Chapter 14 Creating Graphics with DWindow
PDF
Java-Chapter 11 Recursions
PDF
Java-Chapter 10 Two Dimensional Arrays
PDF
Java-Chapter 09 Advanced Statements and Applications
PDF
Java-Chapter 08 Methods
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 10 Trees
Discrete-Chapter 09 Algorithms
Discrete-Chapter 08 Relations
Discrete-Chapter 07 Probability
Discrete-Chapter 06 Counting
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 03 Matrices
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 01 Sets
Discrete-Chapter 12 Modeling Computation
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 11 Recursions
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 08 Methods

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
DevOps & Developer Experience Summer BBQ
PPTX
MYSQL Presentation for SQL database connectivity
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
cuic standard and advanced reporting.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Electronic commerce courselecture one. Pdf
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Omni-Path Integration Expertise Offered by Nor-Tech
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Big Data Technologies - Introduction.pptx
DevOps & Developer Experience Summer BBQ
MYSQL Presentation for SQL database connectivity
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
NewMind AI Weekly Chronicles - August'25 Week I
20250228 LYD VKU AI Blended-Learning.pptx
Event Presentation Google Cloud Next Extended 2025
cuic standard and advanced reporting.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Electronic commerce courselecture one. Pdf
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
CIFDAQ's Market Insight: SEC Turns Pro Crypto
GamePlan Trading System Review: Professional Trader's Honest Take
madgavkar20181017ppt McKinsey Presentation.pdf
Advanced Soft Computing BINUS July 2025.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Omni-Path Integration Expertise Offered by Nor-Tech
Dropbox Q2 2025 Financial Results & Investor Presentation

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: [email protected], MSN: [email protected] 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)