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)