Title: Korn Shell Script Writing
1Korn Shell Script Writing HPC
- Science Technology Support Group
- High Performance Computing
- OSC (Ohio Supercomputer Center)
- 1224 Kinnear Road
- Columbus, OH 43212
2Table of Contents
- Introduction
- What is a shell script?
- Script Programming Language
- Why use shell scripts?
- Korn shell scripts
- References
- The Evolution of a Script
- Sampling of Script Features
- A simple script
- A more versatile Script
- Creating Files within a Script
- A documented Script
- Script Basics
- Creating a shell script
- Executing a shell script
- Echoing script commands
- Script Variables
- User-defined Variables
- Storing a Value into a Variable
- Assignment Operator
- Read Command
- Command Substitution
- Null Variables
- Special Shell Variables
- Positional Parameters
- Referencing Variables
- Parameter Substitution
- Exercises
- Decision-Making statements
- Flow Control
- Exit Status
- If-Then-Else command
- Conditional Tests
- Logical Operators
- Illustrative Examples
3Table of Contents
- Looping Statements
- for Statement
- select Statement
- while Statement
- Until Statement
- Exercises
- Working with Integers
- Arithmetic Operations
- Operator Precedence
- Numerical Bases
- String vs Integer Variables
- Exercises
- Script I/O
- Shell Redirection Operators
- "Here-is" Files
- print
- read
- Quoting
- Exercises
- Communicating with Running Scripts
- Sending Signals
- How Scripts Receive Signals
- Script Functions
- Why Use Functions?
- Syntax Use
- Auto-Loading
- Function Definition Location
- Command Precedence
- HPC Designed Scripts
- Timing Loops
- Debugging Options
- Batch Jobs
- Performance Tool Preprocessing
- Multiple Tools runs
- Multiple Tool Reports
- File Renaming
4Introduction
- What is a shell script?
- Script Programming Language
- Why use shell scripts?
- Korn Shell scripts
- References
5What is a shell script?
- A shell script is a collection of Unix commands
entered into a file. When the file (script) is
executed, the commands are executed in order. - Running a shell script is EXACTLY equivalent to
interactively entering each command - For example, say a user always does the same four
steps before printing out a file making the file
owner- writable, using vi to check/change the
file, printing it out, and then making the file
unwritable (for safety reasons) - An interactive session of a user following this
procedure for the file comp.txt is shown on the
next page
6Interactive editing and printing
- chmod uw comp.txt
- vi comp.txt
- Here is a table summarizing OSC computers as of
today. - Supercomputer Processors Proc Speed Ideal
Calc. - ------------ ----------- ---------
------------- - Itanium Cluster 124 900 MHz 223.2 GFLOPS
- Cray SV-1 16 300 MHz 19.2 GFLOPS
- Origin 2000 64 300 MHz 38.4 GFLOPS
- IA-32 Intel 128 550 MHz 70.4 GFLOPS
- Sun Fire 6800 24 900 MHz
43.2 GLOPS - "comp.txt" 11 lines, 535 characters
- lpr -Pps1 comp.txt
- chmod u-w comp.txt
7Making the viprint script
- To make the previous editing and printing
procedure into a script, all the user has to do
is type the same commands they use interactively
into a file - Say the file is called viprint, it looks like
this - cat viprint
- chmod uw comp.txt
- vi comp.txt
- lpr -Pps1 comp.txt
- chmod u-w comp.txt
- After viprint is made executable, whenever the
user wants to execute these four commands they
simply type viprint as if it was a normal Unix
command - viprint
8Script Programming Language
- The real power is writing shell script comes from
using special shell commands and variables that
essentially make your script act like a program - In this course, you will learn about the
programming statements which can be used in
scripts, including - Initializing and using your own script variables.
Both single valued and arrays - Performing operations with variables
- Decision-making statements (if-then-else, etc)
- Looping statements (for,etc)
- Defining and using functions in scripts
- Script I/O commands and procedures
- Above bullet reads like a table of contents
for a text on a traditional programming
languages such as Fortran or C, although - Script programming has less capabilities and
commands
9Why use shell scripts?
- Convenience Run a number of Unix commands by
typing a single script name - Command log Script can permanently contain a
detailed set of commands with numerous options.
(If interactive, complex command is gone) - Usefulness Write a script with programming
commands to give the script a number of
capabilities/options - Take full use of what the shell has built-in to
offer you - Considering a script as a program, no compiler,
loader, or libraries needed to make it executable - Portability If a script works in the shell on
your machine it will work on the same shell on a
different machine - Easy to debug Small number of programming
statements which are relatively simple - Easy to modify, expand, and customize Just type
in the new commands - Run-type interaction Can send signals to running
scripts
10Korn shell scripts
- Several types of Unix shells exist, each with
their own script programming languages - Bourne (sh), Born Again shell (bash), Korn shell
- C shell (csh), TENEX C shell (tcsh)
- Features you would expect from a well-written
shell - Convenient interactive features command history,
command-line editing, and filename completion - Powerful scripting capabilities including
reliability and efficiency - This course will discuss Korn shell scripting
- It has all the convenient interactive features
- It is backward compatible with the Bourne shell
(the first shell) - Most scripts are sh
- Start-up sequence for ksh same as sh only
enhanced - Unix utilities and administration commands depend
on sh - It does not have the scripting bugs that the C
shell has
11Korn shell scripts
- What if I already use a different shell on my
machine? - Could switch to Korn shell (use the chsh command
if you can) - Keep your existing shell as your interactive
shell and write your scripts as Korn shell
scripts (Easily done one line needed in the
script)
12References
- Internet URLs
- nacphy.physics.orst.edu/rubin/melanie/node144.html
(Tutorial) - gonzo.tamu.edu/csh.whynot.html (Bugs in C shell
scripting) - Usenet Newsgroups
- comp.unix.shell
- comp.unix.questions
- Anonymous ftp
- athos.rutgers.edu/patra/vi.html
(shell-101.BetaA.Z) - rtfm.mit.edu/pub/usenet/news.answers/unix-faq/faq
(Unix FAQ) - OSC Unix-Series workshops (oscinfo.osc.edu)
- Basic Unix
- Intermediate Unix
- Introduction to Perl
13The Evolution of a Script
- Sampling of Scripting Features
- A Simple Script
- A More Versatile Script
- Creating Files within a Script
- A Commented Script
14Sampling of Scripting Features
- Next few slides will act as a course overview as
we take a simple script and keep extending its
capabilities - The more advanced scripts will demonstrate some
of the capabilities of Korn Shell scripting - Dont worry about understanding all the details
at this point. A complete explanation of the
scripting commands used is given in later chapters
15A Simple Script
- In the script called triangle, a C program is
compiled and then executed. The program
calculates the area of the triangle and reads in
the lengths of the sides from a file and outputs
the result to another file. - Here is an example of a user running the script
- cat triangle
- cc -n32 -cckr -O3 -LNO -Ofast -TARGmaddon -o
area area.c - area lt sides.in gt area.dat
- cat area.dat
- rm area
- ls -l triangle
- -rwxr--r-- 1 dje appl 108 May 3
1209 triangle - cat sides.in
- 8 3 7
- triangle
- Area of Triangle is 10.392305
16A Simple Script
- Limitation of the simple triangle script
- Script only works for the one area program. What
if you want to run other triangle-related
programs with this same script ?
17A More Versatile Script
- Scripts can take arguments on the command line
when they are run. The first argument is passed
to a variable with a special name 1. We can use
this feature to make the triangle script work for
any program. - Here is the resulting new script in action
- cat triangle
- cc -n32 -cckr -O3 -LNO -Ofast -TARGmaddon -o 1
1.c - 1 lt sides.in gt 1.dat
- cat 1.dat
- rm 1
- triangle area
- Area of Triangle is 10.392305
- triangle perim
- Perimeter of Triangle is 18.000000
18A More Versatile Script
- Limitation of versatile script
- Scripts should be as independent of possible of
outside existing files (for portability and
ease of use). Since the sides.in file is so
short, we should create the file within the script
19Creating Files Within a Script
- The easiest method for creating a small
internal file is to use Here-is Unix
redirection. After it is used, this temporary
file is removed - cat triangle
- if 2 debug then
- cc -g -o 1 1.c
- print "1 meant to be debugged"
- else
- cc -n32 -cckr -O3 -LNO -Ofast -TARGmaddon -o
1 1.c - cat ltlt EOF gt sides.in
- 8 3 7
- EOF
- 1 lt sides.in gt 1.dat
- cat 1.dat
- rm 1 sides.in
- fi
- triangle perim
- Perimeter of Triangle is 18.000000
20 A Commented Script
- A bad script writing style has been shown in all
incarnations of the triangle script the lack of
comments. Comments are critical to another user
(or you) in understanding what the script does. - The special comment on the first line of script
actually specifies the shell to be used for the
script commands. This is required for users whose
interactive and script shells differ. - On the next page is the timing script just
discussed but now with proper commenting
21 A Commented Script
- cat triangle
- !/bin/ksh
- Compiling C code found in first parameter
- cc -n32 -cckr -O3 -LNO -Ofast -TARGmaddon -o 1
1.c - Using redirection to make input file
- cat ltlt EOF gt sides.in
- 8 3 7
- EOF
- Using while loop to run the executable 17 times
- let i0
- while (( i lt 17 )) do
- 1 lt sides.in gtgt 1.dat
- let ii1
- done
- Checking output
- cat 1.dat
- Clean up files no longer needed
- rm 1 sides.in
22Script Basics
- Creating a Shell Script
- Executing a Shell Script
- Echoing Script Commands
23Creating a Shell Script
- A shell script is an executable file which is
executed by the shell line-by-line. It can
contain the following - UNIX commands
- shell programming statements
- comments
- Create using editor of choice
- Can include a ! construct in first line of
script to override login shell - !/bin/ksh uses Korn shell to execute script
- !/bin/csh uses C shell to execute script
- Recommend always put this in every Korn shell
script. Makes it clear what the file is. "file"
Unix command will even identify it as a Korn
Shell script - Name should be short and descriptive
24Executing a Shell Script
- There are 3 ways to execute a shell script
- 1."dot" method
- . scriptname
- 2."just the name" method
- scriptname
- 3.in the background
- scriptname
-
25Executing a Shell Script
- Method 1 runs the command as if you typed them
in on the command line - No ksh subprocess created
- Note that methods 2 and 3 require
- execute permission for scriptname
- chmod x scriptname
- current directory (.) must be in PATH or else
must use ./scriptname
26Executing a Shell Script
- Demonstration of execution methods
- ps
- PID TTY TIME CMD
- 31612 ttyq5 000 sh
- 31631 ttyq5 000 ps
- ls -l showsh
- -rw-r--r-- 1 dje appl 43 Nov 25
1997 showsh - cat showsh
- print Executing the shell script showsh
- ps
- . showsh
- Executing the shell script showsh
- PID TTY TIME CMD
- 31612 ttyq5 000 sh
- 31628 ttyq5 000 ps
27Executing a Shell Script
- chmod ux showsh
- showsh
- Executing the shell script showsh
- PID TTY TIME CMD
- 31578 ttyq5 000 ps
- 31612 ttyq5 000 sh
- 31635 ttyq5 000 sh
28Echoing Script commands
- For understanding or debugging scripts it is
often convenient to see the script commands shown
on the monitor along with the output they
produce. - Accomplished using the set -x command at the
beginning of the file - To turn off the echoing, make the command set x
(counter-intuitive?) - Also useful in batch job files and - on occasion
- interactively - On the next page a demonstration of the power of
set -x is shown
29Echoing Script commands
- see
- /homea/dje/ksh_script
- boba.osc.edu0.0
- a_opt.txt cba_opt.txt item.set.3.txt
pid.txt - b_opt.txt fant4.txt item.set.4.txt
showsh.txt - c_opt.txt item.intmod.txt no_opt.txt
trap.txt - cat see
- !/bin/ksh
- pwd
- echo DISPLAY
- ls .txt
- cat see2
- !/bin/ksh
- set -x
- pwd
- echo DISPLAY
- ls .txt
30Echoing Script commands
- see2
- pwd
- /homea/dje/ksh_script
- echo boba.osc.edu0.0
- boba.osc.edu0.0
- ls a_opt.txt b_opt.txt c_opt.txt cba_opt.txt
fant4.txt item.intmod.txt item.set.3.txt
item.set.4.txt no_opt.txt pid.txt showsh.txt
trap.txt - a_opt.txt cba_opt.txt item.set.3.txt
pid.txt - b_opt.txt fant4.txt item.set.4.txt
showsh.txt - c_opt.txt item.intmod.txt no_opt.txt
trap.txt
31Script Variables
- User-defined Variables
- Storing a Value into a Variable
- The assignment operator
- The read command
- Command Substitution
- Null Variables
- Special Shell Variables
- Positional Parameters
- Referencing Variables
- Parameter Substitution
- Exercises
32User-defined Variables
- The Korn shell includes the following types of
variables - User-defined variables
- Special shell variables
- User-defined variables can be initialized, used
and changed from the command line or from within
a shell script. - Not "declared" like in a programming language.
Just type the name of the variable - Korn shell scripts use string and integer
variables - A variable name can consist of the following
- letters, digits, and the underscore character
- first character of a variable name must be a
letter or an underscore character - A variable can be made read-only once it is
assigned a value, the value cannot be changed - "Archaic" method readonly variable_name
- Alternate method typeset -r variable_name
33Storing a Value into a Variable
- There are several methods for variables to obtain
values. - The assignment operator
- Enter the name that you have chosen for the
variable followed by an equal sign and then the
value that you want to store in the variable. - To use the a variable's value, put the operator
in front of it's name. (Read the as "contents
of") - The typeset command can also be used for variable
assignment. More up-to-date procedure, typeset
has many options. - To assign a value to an integer variable, the
assignment command must be proceeded by the word
let - On the next page, a script is shown that
demonstrates all these approaches -
34Storing a Value into a Variable
- cat assign
- streetElm
- print street lt-- Most common error
when using variables - print street
- my_name"John Smith"
- echo my_name
- nephChris Sellgren
- print neph
- typeset cardace print card
- let num57 echo num
- let numnum3 print num
- assign
- street
- Elm
- John Smith
- assign2.ksh9 Sellgren not found
- ace
- 57
35Storing a Value into a Variable
- Second method read in a value for a variable
- So far have used print and echo (archaic)
commands for printing out all the strings
following them and then a carriage return - There is a complimentary command read which input
a value into variables. The user type in as many
strings as there are variables on the read line,
and then hits carriage return - On the next page is a demo script indicating how
the read command works.
36Storing a Value into a Variable
- cat input
- print Please enter a ship name
- read ship
- print Please enter your full name
- read fname lname
- print Captain lname, welcome aboard the ship
- echo May I call you fname?
- print Enter an integer read num
- let num2num
- echo Twice your number is num
- input
- Please enter a ship name
- Redoubt
- Please enter your full name
- Horatio Hornblower
- Captain Hornblower, welcome aboard the Redoubt
- May I call you Horatio?
- Enter an integer
- 27
37Storing a Value into a Variable
- Use command substitution. The string(s) that a
Unix command returns may be put into a script
variable - The following syntax is as follows
- var_name(UNIX command)
- As with all the script commands, command
substitution can be done interactively as well - On the next page is a demo script illustrating
this technique
38Storing a Value into a Variable
- 136oscb cat command
- dir(pwd)
- print I am in the directory dir
- weekday(date A)
- print It is weekday
- files(ls -lt wc -l)
- print There a files files in this directory
- me(whoami)
- print I am me
- computer(hostname)
- print My computer is called computer
- logged_on(who cut -f1 -d' ')
- print These people are also on computer
logged_on - 137oscb command
- I am in the directory /home/dje
- It is Tuesday
- There a 277 files in this directory
- I am dje
- My computer is called oscb
39Null Variables
- A variable can be set to a null value, even
if previously assigned, using any of the
following methods. There should be no spaces
preceding or following the equal sign. If the
contents of null character variables are printed
out, a blank appears. If the contents of a
nullled integer variable is outputted, a 0 is
displayed. - name
- name''
- name"
-
- let num
- unset varname
-
40Null Variables
- All variables that don't exist are assumed null
unless set -o nounset is used. Then the shell
will indicate an error when an undefined variable
is encountered - unset name
- set -o nounset
- print name
- ksh name parameter not set
- set o nounset
41Special Shell Variables
- In addition to your own user-defined variables,
other variables defined by the shell for the user
are called special shell variables and are most
useful - These variables are set and updated automatically
by the shell. Their values cannot be changed, but
they may be referenced. - The special shell variables of most use are
identified by the following symbols -
- -
- ?
-
- !
- 0
-
-
42Special Shell Variables
- The variable contains the number of arguments
typed on the command line. - cat numargs
- print The number of arguments is
- numargs
- The number of arguments is 0
- numargs 1 2 3 4 5
- The number of arguments is 5
- numargs "Hello World"
- The number of arguments is 1
-
43Special Shell Variables
- The variable - contains the shell flags
(options) of the current shell. - print -
- isum
- set u
- echo -
- ism
44Special Shell Variables
- The variable ? contains the exit status of the
last command. If the exit status is 0, the
command ran successfully. If it is non-zero,
there was a problem - ls
- file1 data account.txt
- rm file1
- print ?
- 0
- rm dataa
- dataa No such file or directory
- print ?
- 2
- We will see exit status again. It will be used to
define true and false. Also, the user can set the
exit status in their script by using the exit
command -
45Special Shell Variables
- The variable contains the Process ID (PID) of
the current shell process. - cat pid
- !/bin/ksh
- ps
- print
- pid
- PID TTY TIME CMD
- 34738 ttyq5 000 ps
- 34744 ttyq5 000 sh
- 34765 ttyq5 000 pid
- 34765
- . pid
- PID TTY TIME CMD
- 34743 ttyq5 000 ps
- 34744 ttyq5 000 sh
- 34744
46Special Shell Variables
- The variable ! contains the process ID number of
the last command sent to the background. - sleeper
- 1 29144
- ps
- PID TTY TIME CMD
- 29144 ttyq5 000 sleeper
- 29150 ttyq5 000 sh
- 29160 ttyq5 000 ps
- 29167 ttyq5 000 sleep
- compress moby_dick
- 2 29136
- print !
- 29136
47Special Shell Variables
- The variable 0 contains the name of the script
currently being execute - cat old_script
- print The name of this script is 0
- old_script
- The name of this script is old_script
- mv old_script new_script
- new_script
- The name of this script is new_script
- The variable contains all the arguments on the
command line. - cat args
- print The arguments are
- args bob dave
- The arguments are bob dave
48Positional Parameters
- For a script, the command line arguments are also
called positional parameters. Each positional
parameters refers to one individual argument on
the command line. The ten positional parameters
available to the script writer are referenced as
follows - 1 2 3 4 5 6 7 8 9
- The parameter 1 contains the first argument, 2
the second argument, and so on. Consider the
following script - cat parms
- print Arg 1 is 1
- print Arg 2 is 2
- print Arg 3 is 3
- print Arg 4 is 4
- print Arg 5 is 5
-
49Positional Parameters
- parms 2001 A Space Odyssey
- Arg 1 is 2001
- Arg 2 is A
- Arg 3 is Space
- Arg 4 is Odyssey
- Arg 5 is
- parms "Space, the final frontier"
- Arg 1 is Space, the final frontier
- Arg 2 is
- Arg 3 is
- Arg 4 is
- Arg 5 is
50Positional Parameters
- Use the set command to change positional
parameters. It replaces existing positional
parameters with new values - cat newpos
- print starting args are
- print number of args is
- print arg 1 is 1
- print arg 2 is 2
- set NCC 1701 Enterprise
- print new args are
- print number of args is
- print arg 1 is 1
- print arg 2 is 2
- print arg 3 is 3
-
51Positional Parameters
- Here is an example of the use of the newpos
script - newpos 76 trombones
- starting args are 76 trombones
- number of args is 2
- arg 1 is 76
- arg 2 is trombones
- new args are NCC 1701 Enterprise
- number of args is 3
- arg 1 is NCC
- arg 2 is 1701
- arg 3 is Enterprise
52Positional Parameters
- What if there are more than 10 positional
parameters? - cat ten_args
- print arg 10 is 10
- ten_args a b c d e f g h i j
- arg 10 is a0
- Use the shift command to perform a shift of the
positional parameters n positions to the left. - (Default n1)
- The shell variables and also change when
the shift command is used (along with the values
of the positional parameters. - Once a default shift is performed, the first
parameter is discarded unless it is saved in a
user-defined variable -
53Positional Parameters
- Examples of shift usage
- cat shift_it
- print 0
- shift
- print 0
- shift
- print 0
- shift 3
- print 0
- shift_it It was the best of times, it was
the worst of times. - 12 shift_it It was the best of times, it
was the worst of times - 11 shift_it was the best of times, it was
the worst of times - 10 shift_it the best of times, it was the
worst of times - 7 shift_it times, it was the worst of
times - cat ten_args
- arg11
- shift
- print arg1
54Referencing Variables
- So far we have seen that the syntax for
referencing a variable (obtaining its contents)
is var_name - An alternate syntax which has more capabilities
is var_name This syntax -
- Allows a script to easily use more than 10
positional parameters - Allows for more general use of a variable
contents - Allows for the number of characters in the
variables string contents to be determined - Allows for parameter substitution replacement of
the value of a variable with an different value
based on specific conditions and events.
55Referencing Variables
- Greater than 10 positional parameters
- cat moreargs
- !/bin/ksh
- print arg 10 is 10
- print arg 10 is 10
- print arg 11 is 11
- print arg 11 is 11
- moreargs what rough beast, its time come at
last, slouches toward Bethlehem - arg 10 is what0
- arg 10 is toward
- arg 11 is what1
- arg 11 is Bethlehem
56Referencing Variables
- More general use
- cat copy
- set -x
- filelone
- cp file filewolf
- cp file filewolf
- ls lone
- copy
- filelone
- cp lone
- Insufficient arguments
- Usage cp -aDfirRp -b size -tP -e size f1
f2 - cp -aDfirRp -b size -tP -e size f1
... f2 dir - cp lone lonewolf
- ls lone lonewolf
- lone lonewolf
- Length of a Variables contents
- cat length
- book"Pillars of the Earth"
57Parameter Substitution
- Misnamed should be variable substitution
- Four syntaxes for parameter substitution
- var_name - value
- var_name value
- var_name value
- var_name ? value
- The meanings and typical usages of these four
methods are shown on the following pages
58Parameter Substitution
- parameter-value
- If parameter exists and isn't null, return its
present contents otherwise return value. - Purpose returning a default value if the
variable is undefined. - If parameter is null, the value returned is
temporary. The null value of the variable is not
permanently altered - Interactive Examples
- count""
- print You have count-5 cards
- You have 5 cards
- print count
-
- FOO/usr/local/bin/xx
- print edit file FOO-HOME/xx
- edit file /usr/local/bin/xx
- print FOO
- /usr/local/bin/xx
59Parameter Substitution
- As the name implies, this particular syntax of
parameter substitution can be used positional
parameters. - Consider the following script that searches for a
file - cat fnddemo
- !/bin/ksh
- set -x
- find 2-HOME -name 1 -print
- fnddemo bk.txt.Z .
- find . -name bk.txt.Z -print
- ./bk.txt.Z
- fnddemo dale11.ps
- find /homea/dje -name dale11.ps -print
- /homea/dje/dale11.ps
60Parameter Substitution
- parametervalue
- If parameter exists and isn't null, return its
present contents otherwise set it to value and
then return value - Purpose Setting a variable to a default value if
it is undefined. - This form of parameter substitution cannot be
used with positional parameters because their
values can only be changed with the set command - Interactive Example
- count""
- print You have count5 cards
- You have 5 cards
- print count
- 5
-
61Parameter Substitution
- parametervalue
- This substitution format acts in an opposite
manner to the previous two. - If parameter exists and is not null, return
value otherwise return null. - Purpose Testing for the existence of a variable
- Interactive Example
- cat trace
- set -x
- trace1 print trace"Trace mode on"
- trace y
- tracey
- print Trace mode on
- Trace mode on
- trace2
- trace
- print
-
62Parameter Substitution
- parameter?message
- If parameter exists and isn't null, return its
contents otherwise print - scriptname parameter message
- and abort the current script. Omitting message
produces the default system message. - Purpose Catching errors that result from
variables being undefined which need to be. - Examples
- cat fnd2
- find 2-HOME -name 1?"No file specified"
-print - fnd2
- fnd2 1 No file specified
- cat fnd3
- find 2-HOME -name 1? -print
- fnd3
- fnd3 1 parameter null or not set
-
63Parameter Substitution
- NOTE In all forms, the colon () is optional. If
omitted, the operator tests for existence only,
i.e., change "exists and isn't null" to "exists"
in each definition.
64Advanced Pattern Matching
- Korn shell enhancements to the set of
pattern-matching operators - Operator Meaning
- variablepattern If the pattern matches the
beginning of the variable's value, delete the
shortest part that matches and return the
rest. - variablepattern If the pattern matches
the beginning of the variable's value, delete
the longest part that matches and return the
rest. - variablepattern If the pattern matches
the end of the variable's value, delete the
shortest part that matches and return the
rest. - variablepattern If the pattern matches the
end of the variable's value, delete the
longest part that matches and return the
rest. - Mnemonic matches the front because number
signs precede numbers matches the rear because
percent signs follow numbers.
65Advanced Pattern Matching
- Typical use of the above pattern-matching
operators is stripping off components of
pathnames such as directory prefixes and filename
suffixes. - Example Assume the variable TEST has the value
/home/job/src/long.file.name then - Expression Result
- TEST// long.file.name
- TEST// job/src/long.file.name
- TEST /home/job/src/long.file.name
- TEST. /home/job/src/long.file
- TEST. /home/job/src/long
66Exercises
- 1. Write a script called lsc which executes the
command ls -C. To execute this command you must
give the full path name to your lsc script. Make
the lsc shell script executable and run it. - 2.Write a script called b which changes your
current directory to /bin, displays the current
directory, and then executes the lsc script
created above. Make the b script executable and
run it. What is your current directory when the b
script is finished executing? Why? -
- 3.Write a script called ex_on, which turn on
execute permission on the first argument on the
command line. - 4.Modify ex_on to turn on execute permission on
all arguments on the command line. - 5.Write a script called 12 that prints the
twelfth argument on the command line.
67Exercises
- 6. Suppose you keep a file of your home coin
collection that keeps track of how many coins you
have in a given category. Lines in this file look
like - 62 U.S. proofs
- 11 U.S. pennies (1850-1908)
- 36 U.S. pennies (1909-1950)
- 9 U.S. nickels (1861-1938)
- Write a script that prints the N types of coins
of which you have the most. The default for N is
10. The program should take one argument for the
name of the input file and an optional argument
for how many lines to print.
68Decision Making Commands
- Flow Control
- Exit Status II
- if/then/else Command
- Condition Tests
- Logical Operators
- Negation Operator
- Illustrative Examples
- Case Statement
- Exercises
69Flow Control
- Flow control gives a programmer the power to
specify that only certain blocks of shell
commands are executed - Korn shell supports the following decision making
constructs - if/else Execute a list of statements if a
certain condition is/is not true - case Execute one of several lists of statements
depending on the value of a variable -
70Exit Status II
- For decision making, we will look at the exit
status in a different way then we did before - The constructs if, while, until and the logical
AND () and OR () operators use exit status to
make logical decisions - A 0 value is a logical "true" (success)
- A nonzero is a logical "false" (failure)
- There are built-in true and false commands which
you can use. The true command returns an exit
status of 0, the false command returns an exit
status of 1.
71Exit Status of a Script
- A script, like any other process, sets an exit
status when it finishes executing. Shell scripts
will finish in one of the following ways - Abort - If the script aborts due to an internal
error, the exit status is that of the last
command (the one that aborted the script). - End - If the script runs to completion, the exit
status is that of the last command in the script - Exit - If the script encounters and exit command,
the exit status is that set by that command - Syntax for the exit command is exit num. When
the exit command is encountered the script ends
right there and the exit status is set to num
72if / else
- Simplest type of decision making construct is the
conditional embodied in Korn shell's if
statement. - Syntax
- if condition
- then
- statements
- elif condition lt-- can use multiple
elif clauses - then statements . . .
- else
- statements
- fi lt-- must be by itself on the final
line of the construct -
73if / else
- The if statement uses an exit status to determine
whether or not to execute the commands. - Statements are executed only if the given
condition is true. - If one or more elifs are used, the last else
clause is an "if all else fails" part.
74Condition Tests
- The if construct can only test exit status but
that doesn't limit you to checking only whether
commands ran properly or not. - Using the operator, many different
attributes can be tested - string comparisons
- file attributes
- arithmetic conditions
- condition just returns an exit status that
tells whether condition is true or not - surround expressions that include various
types of relational operators. -
75Condition Tests Strings
- String comparison operators
- Relational Operator True if ...
- str pat str matches pat.
- str ! pat str does not match pat.
- str1 lt str2 str1 is less than str2.
- str1 gt str2 str1 is greater than str2.
- -n str str is not null (has length greater
than 0). - -z str str is null (has length 0).
- based
on the ASCII value of their characters - str refers to an expression with a string
value, and pat refers to a pattern that can
contain wildcards.
76String Comparison Examples
- cat compstr
- xcastle ycast
- if x y
- then
- print x matches y
- else
- print x does not match y
- fi
- compstr
- castle matches cast
- cat compstr1
- xhello yhelper
- if x gt y
- then
- print "x is gt y"
- else
- print "x is not gt y"
- fi
- compstr1
77String Comparison Examples
- cat compstr2
- if -n 1
- then
- print First argument is 1
- fi
- if -z 1
- then
- print Where is the first argument?
- fi
- compstr2 Logan
- First argument is Logan
- compstr2
- Where is the first argument?
78Condition Tests Files
- File attribute checking
- Relational Operator True if
... - -a file file exists
- -d file file is a directory
- -f file file is a regular file
- -r file there is read permission on file
- -s file file is not empty
- -w file you have write permission on file
- -x file you have execute permission on
file, or directory search permission if it
is a directory - -O file you own file
- -G file your group id is same as file's
- file1 -nt file2 file1 is newer than file2
- file1 -ot file2 file1 is older than file2
- There are 21 such operators in total.
-
79File Checking Demo Directory
- Consider a directory that contains the following
files and subdirectories - ls -lrt
- -rwxr--r-- 1 dje appl 5 Jun 27
2000 file - -rw-r--r-- 1 dje appl 5 Jun 27
2000 my_prog - -rw-r--r-- 1 dje appl 5 Jun 27
2000 my_scr.1 - drwxrwsr-x 2 dje appl 512 Jul 3
2000 scripts - -rw-rw-r-- 1 dje appl 5 Sep 4
2000 slide1.ps - drwxr-sr-x 2 dje appl 512 May 18
1154 probs - drwxr-sr-x 2 dje appl 512 May 18
1154 solutions - -rw-r--r-- 1 dje appl 0 May 18
1154 court.msg - -rw-r--r-- 1 dje appl 0 May 18
1155 heel - -rw-r--r-- 1 dje appl 0 May 18
1157 greed - -rwxr-xr-x 1 dje appl 80 May 18
1201 file.c - -rwxr-xr-x 1 dje appl 80 May 18
1201 file.f90 - -rw-r--r-- 1 dje appl 0 May 18
1204 typescript
80File Checking Examples
- cat fexist
- if -a 1 then
- print File 1 does exist
- else
- print File 1 does not exist
- fi
- fexist sort.c
- File sort.c does not exist
- cat ffull
- if -s 1 then
- print 1 has something in it
- else
- print 1 is empty
- fi
- ffull heel
- heel is empty
- cat fx
- if -x 1 then
- print I can execute 1
81Condition Tests Integers
- Need to use a new test operator for integer
comparisons - (( )) instead of
- Relational Operator Meaning
- lt less than
- gt greater than
- lt less than or equal
- gt greater than or equal
- equal
- ! not equal
-
82Integer Comparison Examples
- cat intcomp
- if (( 1 lt 2 )) then
- print 1 is less than 2
- fi
- if (( 1 lt 2 )) then
- print 1 is less than or equal to 2
- fi
- if (( 1 2 )) then
- print 1 is equal to 2
- fi
- if (( 1 gt 2 )) then
- print 1 is greater than 2
- fi
- if (( 1 gt 2 )) then
- print 1 is greater than or equal to 2
- fi
- if (( 1 ! 2 )) then
- print 1 not equal to 2
- fi
83Integer Comparison Examples
- intcomp 3 89
- 3 is less than 89
- 3 is less than or equal to 89
- 3 not equal to 89
- intcomp 13 13
- 13 is less than or equal to 13
- 13 is equal to 13
- 13 is greater than or equal to 13
84Logical Operators
- Two special symbols allow the user to combine
relational tests into more complex conditions - is a logical AND
- is a logical OR
- How they work
- statement1 statement2
- statement2 will execute only if statement1 was
successful (returned an exit status of 0) - statement1 statement2
- statement2 will execute only if statement1 was
not successful (returned a nonzero exit status).
85Logical Operators
- The logical operators and are commonly used
with if/else commands - Examples
- ls -l
- -r-xr-xr-x 1 dje appl 365 May 18
1513 intcomp - -rw-r--r-- 1 dje appl 209 May 18
1512 length.txt - cat setw
- !/bin/ksh
- if -f 1 -x 1 then
- print You are getting write permission for 1
- chmod uw 1
- fi
- setw length.txt
- setw intcomp
- You are getting write permission for intcomp
- ls -l
- -rwxr-xr-x 1 dje appl 365 May 18
1513 intcomp - -rw-r--r-- 1 dje appl 209 May 18
1512 length.txt
86Logical Operators
- cat gone
- !/bin/ksh
- if 1 quit 1 bye then
- print You have closed correctly
- else
- print Improper closing word
- exit 11
- fi
- gone bye
- You have closed correctly
- echo ?
- 0
- gone quit
- You have closed correctly
- echo ?
- 0
- gone end
- Improper closing word
- echo ?
87The Negation Operator
- If an exclamation point ! is put before any of
the conditions discussed, it reverses the truth
or falseness of the condition. - ! -d probs returns true (exit status 0) if
probs is not a directory - ! Bob ?o? returns false (exit status 1)
even though Bob matches the pattern
88Combining Different Types of Conditions
- If you want to combine an integer comparison with
file or string comparison within the same
condition, you must do two things - Use for the test operator
- Use the integer relational operators shown below
- Relational Operator Comparison
- -lt less than
- -le less than or equal
- -eq equal
- -ge greater than or equal
- -gt greater than
- -ne not equal
89Combining Conditionals Example
- cat fileint
- !/bin/ksh
- let num2
- if 1 lt tact num -ge 3 then
- print Pippin
- else
- print Merry
- fi
- fileint ace 45
- Pippin
- fileint vice 2
- Merry
90Illustrative Examples
- Test operator run interactively
- -z ""
- print ?
- 0
- -z foo
- print ?
- 1
- Treating integers strings as strings compressed
format - if 6 gt 57 then print huh?fi
- huh?
-
91- Illustrative Examples
- The following script sets user execute
permission on an ordinary, file that is not a
directory - if -f 1 ! -d 1 then
- chmod ux 1
- fi
92Illustrative Examples
- The following script removes the first file if
it's older than the second file and the variable
KEY is non-null - if 1 -ot 2 -n KEY
- then
- /bin/rm 1
- fi
-
93Illustrative Examples
- The following script compares two files and if no
differences are found, removes the second file.
Also helpful USAGE programming style - USAGE"Usage\t 0 file1 file2"
- if -ne 2 then
- print USAGE
- exit 1
- fi
- diff 1 2 gt /dev/null
- if ? -eq 0 then
- /bin/rm 2
- print 2 removed, 1 kept
- else
- print 1 and 2 differ
- fi
94Case command
- Provides a multiple choice decision structure.
When executed one of several statement blocks
will be run. - Replacement for the elif-ladder
- Lets you test strings against patterns that can
contain wildcard characters. - Syntax
- case expression in
- pattern1)
- statements
- pattern2)
- statements
- ...
- esac
-
95Case Operation
- If expression matches one of the patterns, its
corresponding statements are executed - If there are several patterns separated by pipes,
the expression can match any of them in order for
the associated statements to be run ( acts as
logical or ) - Patterns are checked in order for a match if
none is found, nothing happens - When the double colons () are encountered at
the end of the one of the statement lists, the
case terminates -
96Case Examples
- Here's a simple script which moves C and Fortran
source files to one directory and object code
files to another - case file in
- .c.f)
- /bin/mv file HOME/src
- .o )
- /bin/mv file HOME/obj
- )
- print file not moved (Default
value or USAGE) - esac
-
- (Could have also used .cf wildcard
construct above)
97Case Examples
- The case statement is often used to specify
options for a shell script. - Here is a shell script called dt_fmat that allows
the user to enter options that affect the way the
date is displayed - case 1 in
- -d) print -n Date
- date "a h d"
- -t) print -n Time
- date "T"
- -w) print -n Weekday
- date "a"
- -y) print -n Year 20
- date "y"
- -m) print -n Month
- date "h"
- ) print -n Date
- date "a h d"
- print -n Time
- date "T"
- esac
98Case Usage
- date
- Sat May 19 120157 EDT 2001
- dt_fmt -d
- DateSat May 19
- dt_fmt -t
- Time120205
- dt_fmt -w
- WeekdaySat
- dt_fmt -y
- Year 2001
- dt_fmt -m
- MonthMay
- dt_fmt
- DateSat May 19
- Time120254
99Elif-Ladder vs Case
- The following script shows and elif-ladder and
a case statement that do the exact same thing.
Note how the case statement has a simpler syntax - if 1 cat 1 dog then
- print You have a normal pet
- elif -z 1 then
- print You have no pets
- else
- print You have a wierd pet
- fi
- case 1 in
- catdog) print You have a normal pet
- '') print You have no pets
- ) print You have a wierd pet
- esac
- Advantage of elif-ladder not limited to string
comparisons
100Exercises
- 1.Write a script called char that checks a single
character on the command line, c. If the
character is a digit, digit is displayed. If the
character is an upper or lowercase alphabetic
character, letter is displayed. Otherwise, other
is displayed. Have the script print an error
message if the argument c is more than one
character in length. - 2.Write a script called mycopy that copies a
source file to a directory. Add a check to see if
the source file exists. If the source file does
not exist, print an error message. - 3.Write a script called mydir that prints the
message File is a directory if the file is a
directory. -
101Exercises
-
- 4.Write a script called ver that accepts one
argument on the command line indicating the name
of a file. The file is copied to another file
with the same name with the addition of the
extension .v2. Also, the line Version 2 is added
to the top of the file. - 5.Execute the ver script on itself, creating a
new version of the file called ver.v2. - 6.Rewrite ver.v2 to accept a possible second
argument. If two arguments are entered, the file
specified by the first argument is copied to a
file with the name of the second argument. If no
second argument is entered, the file is copied to
another file with the - same name, adding the extension .v2. In
either case, the line Version2 is added to the
top of the file.
102Looping Commands
- for
- select
- while
- until
- Exercises
103for Loop Command
- The for loop allows a section of commands to be
run a fixed number of times - During each time through an iteration, the loop
variable is set to a different value. - Syntax
- for name in list
- do
- statements that can use name
- done
- where
- name is the loop variable which can be called
anything - list is a list of strings (defaults to )
separated by spaces - Operation name is set to each string in list, in
order, for each iteration the number of
iterations equals the number of names in list. -
104for Loop Examples
- Outputting a Phrase
- cat simple
- for word in This is a test
- do
- print word
- done
- simple
- This
- is
- a
- test lt-- Omitting the in This is
a test phrase -
and instead running the script as
-
simple This is a test -
would yield the same output. -
105for Loop Examples
- The list can be the contents of a local variable
-
- Check to see who is logged on the machines listed
in the variable SYSTEMS"myrtle gull sandy
newport daytona" - for sys in SYSTEMS
- do
- finger _at_sys
- print
- done
-
-
-
106for Loop Examples
- list can contain shell wildcards and command
substitution as well - cat file1
- for i in
- do
- print i
- done
- file1
- file1
- file2
- cat file2
- for i in (ls)
- do
- print i
- done
- file2
- file1
107for Loop Examples
- Can explicitly make the list all the command line
arguments (also notice the alternate syntax) - cat upper
- !/bin/ksh
- for bug in do
- typeset -u daybug
- print day
- done
- upper Robson Bond Green
- ROBSON
- BOND
- GREEN
-
108select Command
- select allows you to generate simple interactive
ASCII menus easily. - Syntax
- select name in list
- do
- statements that can use name
- done
- This is the same syntax as the for loop except
for the keyword select. As with for, in list
defaults to if omitted. -
-
109select Operation
- Each item in list is displayed as the choice in
the menu with a number preceding it. At the
bottom of the menu the PS3 prompt is displayed
and script is waiting for the user to select a
choice by entering its number. (A default value
for the PS3 prompt is provided by the system but
the user can change it) - The selected choice is stored in name and the
selected number in a system-predefined variable
called REPLY - The statements in the body of the do loop are run
- Repeats the process forever exit loop with break
statement (or user can issue ctrl-d)
110select Example
- The following script termselect allows you to
select a terminal setting - PS3'terminal? '
- select term in vt100 vt220 xterm
- do
- if -n term then
- TERMterm
- print TERM is term
- else
- print invalid choice
- fi
- done
-
111select Example
- termselect
- 1) vt100
- 2) vt220
- 3) xterm
- terminal? 4
- invalid choice
- terminal? 3
- TERM is xterm
112select Example
- cat travel
- select i in Past Present Future do
- print You have picked choice REPLY
- print "You will go to the i"
- break
- done
- travel
- 1) Past
- 2) Present
- 3) Future
- ? 1
- You have picked choice 1
- You will go to the Past
- travel
- 1) Past
- 2) Present
- 3) Future