SysProg-Tutor 03 Unix Shell Script Programming
-
Author
wongyos-keardsri -
Category
Technology
-
view
1.883 -
download
3
Embed Size (px)
description
Transcript of SysProg-Tutor 03 Unix Shell Script Programming

Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
UNIX Shell Script Programming
Tutor Session III:
Wongyos Keardsri (P’Bank)Department of Computer EngineeringFaculty of Engineering, Chulalongkorn UniversityBangkok, Thailand
Mobile Phone: 089-5993490E-mail: [email protected], MSN: [email protected]: @wongyos

2
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Tutor Outline
Introduction to ShellShellShell Script
VariablesCreating/AssigningAccessingSetting
Getting Start Shell ScriptInclude shellCreate shellRun shell
Data OperationsDecision Statements
If-elseSwitch-case
Iteration StatementForWhile

3
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Introduction to Shell
What is the shell or Unix shell?A command-line interpreter and script host that provides a traditional user interface for the Unixoperating system and for Unix-like systemsThere are many shells; sh, bash, ksh, csh, zsh, …
Bourne Shell (sh)Written by Stephen BourneWas the 1st popular Unix shell

4
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Bourne Shell (sh) (Cont)Available on all Unix systemsSupports a fairly versatile programming languageA subset of more powerful Korn shell (ksh)Implement with regular C programmingExecutable file is stored as /bin/sh
(Cont)
Introduction to Shell

5
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Variables
Creating and assigning a variable
Printing/Showing a variable value
Setting environment variable
Read only variable
name=valuename=value
echo $name
export NAME
No spacesNo spaces
With spacesWith spaces
readonly name

6
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Variables
Example$ age=15$ nickname=Bank$ echo I'm $nickname, $age years old
More an examples by yourself

7
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Variables
Accessing a variable
(Cont)
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.

8
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Variables
Example
(Cont)
$ verb=sing$ echo I like $verbingI like$ echo I like ${verb}ingI like singing
More an examples by yourself

9
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Getting Start Shell Script
What is the shell script?Similar to DOS batch filesQuick and simple programmingText file interpreted by shell, effectively new commandList of shell commands to be run sequentiallyTypical operations for file manipulation, program execution, and printing text

10
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Getting Start Shell Script
Include full path to interpreter (shell)
Example
(Cont)
#!/path/shell
#!/bin/sh#!/usr/bin/sh#!/bin/csh -f

11
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Getting Start Shell Script
Using vi command to create shell script fileRunning shell script by using the command below
Example
(Cont)
sh [file]
$ vi test.sh...$ sh test.sh

12
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Getting Start Shell Script
Interaction with userOutput value
Input value
Comment line
(Cont)
echo [texts/variables]
read [variables]
# your comments

13
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Getting Start Shell Script(Cont)
Getting Start Shell Script(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

14
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Example$ cat example1.shecho there are $# command line arguments: $@$ sh example1.shthere are 0 command line arguments:$ sh example1.sh x y zthere are 3 command line arguments: x y z
More an examples by yourself
Getting Start Shell Script(Cont)

15
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Data Operation
Operators
Using expr to excute operators
Operators Meaning* / % multiplication, division, remainder+ - addition, subtraction= != > < >= <= comparison operators& logical and| logical or
expr $va1 op $var2

16
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Data Operation
Conditional Expressions
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
(Cont)
test [expression]

17
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Data Operation
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 charactersstr1 = str2 True if str1 is equal to str2str1 != str2 True if str1 is not equal to str2string True if string is not nullint1 -eq int2 True if int1 is equal to int2int1 -ne int2 True if int1 is not equal to int2int1 -gt int2 True if int1 is greater than int2
(Cont)

18
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Data Operation
test forms (Cont)int1 -ge int2 True if int1 is greater than or equal to int2int1 -lt int2 True if int1 is less than int2int1 -le int2 True if int1 is less than or equalt to int2!expr True if expr is falseexpr1 -a expr2 True if ezpr1 and expr2 are trueexpr1 -o expr2 True if ezpr1 or expr2 are true
(Cont)

19
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Decision Statement
If-else statement
if [condition]then [result]
elif [condition]then [result]
else[result]
fi

20
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Decision Statement
Exampleif test -r file1
then echo "file1"elif [ -r file2 ]
then cp file2 file3echo "file2 copy to file3"
elseecho "no file"
fiMore the examples by yourself
test –r file2
(Cont)
test –r file2

21
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Decision Statement
Switch-case statement
case $var invalue1) [result] ;;value2) [result] ;;...*) [result] ;;
esac Default case
(Cont)
Default case

22
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Decision Statement
Examplecase $day in
Monday ) echo "A new week" ;;Saturday | Sunday ) echo "Free" ;;Friday ) echo "Hooray !!" ;;* ) echo "It is $DAY" ;;
esac
More the examples by yourself
(Cont)

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

24
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Examplefor color in red yellow green blue
do echo one color is $colordone
More the examples by yourself
(Cont)
Iteration Statement
for xdo echo x = $x
done

25
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
Iteration Statement
While statement
while [condition]do
[result]done
test $var1 –opt $var2
(Cont)
test $var1 –opt $var2

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

27
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
See More
[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

28
Chulalongkorn University
2110313 Operating Systems and System Programs (1/2010)
Tutor Session - 3
End
Question ?
… Answer