Title: Shell ScriptingProgramming
1Shell Scripting/Programming
- CSCI 2467 Spring 2007
- Instructor Michael Ruth
- Computer Science Department
- University of New Orleans
- mruth_at_cs.uno.edu
2Any Questions Before We Move On?
- Corrections
- The default shell used by the dept is the Korn
shell - The file that gets run all the time is not the
.cshrc but the .kshrc file - I can never seem to remember all of your
questions - please send them to mevia email
- Any other questions before we move on?
3Topics
- Motivation
- Or why learn shell programming?
- What is it?
- How to switch Shells (csh ? ksh, etc)
- Invoking a Shell Script
- Sha-bang!
- How do we know where that is?
- Our first script
- The Korn Shell Overview
- Comments
- Variables
- Control-flow (conditionals, conditional
execution, looping, etc) - Command Line Arguments
- Functions (built-in)
- Arrays
4Why Learn Shell Programming
- Automate tasks, such as
- Software install procedures
- Backups
- Administration tasks
- Periodic operations on a database via cron
- Any repetitive operations on files
- A shell script is a "quick and dirty" method of
prototyping a complex application - No compilation
- execution
5What is Shell Scripting?
- A shell script is a script written for the shell,
or command line interpreter, of an operating
system. - Scripts are text files interpreted by a shell
program - Shell Scripting is by default very shell
dependent (like duh) so not everything works from
shell to shining shell - We will focus on the department shell
- The default shell used by the department is the
Korn shell (which is the bourne shell )
6First how do we switch shells?
- Remember me saying that shells are programs?
Well, to switch, simply execute the command - Shell commands
- csh C Shell, but not the kind we sell at the C
shore - ksh Korn Shell (department standard)
- bash Bourne Again Shell
- sh Bourne Shell
- Try switching between them and executing simple
commands - Specifically, try the commands which involve the
environment (set, stty) - To find out what shell you are in
- echo SHELL
- For the purposes of this course, focus on the
Korn Shell (ksh)
7Invoking a Shell Script
- There are two basic mechanisms for invoking a
shell script - Use the shell command to execute script
-
- Ex ksh myscript.ksh
- Make the script itself an executable
- Sha-bang mechanism
- The script must be set to be executable (chmod
755)
8Sha-bang or (!)
- a specific pair of characters that, when used as
the first two characters on the first line of a
script, causes Unix to execute that script using
the interpreter specified by the rest of that
line - More precisely, a shebang line consists of a
number sign and an exclamation point character
("!"), followed by the (full) path to the
interpreter program that will provide the
interpretation - The shebang is actually a human-readable instance
of a magic number in the executable file, the
magic byte string being 0x23 0x21, the two
characters' encoding in ASCII.
9More about Sha-Bangs
- !/bin/bash Execute using bash program in the
/bin/ directory - !/bin/csh Execute using csh, the C shell
instead - !/bin/ksh Execute using the Korn shell
- !/usr/bin/perl Execute using Perl
- Shebang lines can also include specific options
that will be passed to the interpreter. - However, implementations differ widely on how
options are parsed - !/usr/bin/perl w Execute using Perl with
warnings enabled
10How do we find the full path?
- which
- Command takes a list of names and determines
(using path information) what would be executed
had these names been given as commands - Ex
- which perl ? /usr/bin/perl
- which ksh ? /usr/bin/ksh
- Also, as a side note the options can be
discovered via man as usual
11Our First Script
! /usr/bin/ksh echo Hello World!
12Comments
- To comment entire lines of text use the
operator -
- This is all a comment
-
-
- This is a comment too
13Setting and Exporting Variables
- varvalue
- Variables are treated as text strings, unless the
context implies a numeric interpretation. - Note there must be no spaces around the ""
- set
- Display all the variables currently set in the
shell - unset var
- Remove the variable "srcfile"
- var
- Give the variable a null value, (not the same as
removing it). - export var
- Added srcfile to the list of variables which will
be made available to external program through the
environment. - If you don't do this, the variable is local to
this shell instance. - export
- List all the variables currently being exported -
this is the environment which will be passed to
external programs
14Using those Variables
- var
- Prefacing the variable name with causes the
value of the variable to be substituted in place
of the name. - var
- If the variable is not surrounded by whitespace
(or other characters that can't be in a name),
the name must be surrounded by "" braces so
that the shell knows what characters you intend
to be part of the name
15Some Preset Variables
- USER, LOGNAME
- Preset to the currently logged-in username.
- PATH
- The list of directories that will be searched for
external commands. You can change this in a
script to make sure you get the programs you
intend, and don't accidentally get other versions
which might have been installed. - EDITOR
- If set, this contains the name of the program
which the user prefers to use for text file
editing. A program which needs to have the user
manually edit a file might choose to start up
this program instead of some built-in default
(e.g. "crontab -e". This also determines the
default command-line-editing behaviour in
interactive shells. - PWD
- Always set the current working directory
(readonly) - OLDPWD
- The previous directory (before the most recent cd
command)
16Conditionals (Strings)
- -n "string"
- true if string has non-zero length
- -z "string"
- true if string has zero length
- variable text
- True if variable matches text.
- variable
- True if variable comes before (lexically)
textSimilarly, comes after
17Conditionals (Math)
- variable -eq number
- True if variable, interpreted as a number, is
equal to number. - variable -ne number
- True if variable, interpreted as a number, is
not equal to number.Similarly, -lt less than,
-le less than or equal, -gt greater than, -ge
greater than or equal
18Conjunction junction Whats your function!
(conditionals)
- AND
- conditional conditional
- conditional a conditional
- OR
- conditional conditional
- conditional o conditional
- NEGATION
- ! conditional
19Conditionals File Tests
- -e file
- True if file exists (can be of any type).
- -f file
- True if file exists and is an ordinary file.
- -d file
- True if file exists and is a directory.
- -r file
- True if file exists and is readableSimilarly, -w
writable, -x executable, -L is a symlink. - -s file
- True if file exists and has size greater than
zero
20Conditional Execution (if)
- Standard if/else constructs
- In the following discussion list implies a group
of statements - if (condition)
- then
- list
- elif (conditional)
- then
- list
- else
- list
- fi
21Conditional Execution (case)
- Case statements are exactly what you think they
are - The pattern follows the same rules as the
filename metacharacters - case word in
- pattern)
- list
-
- esac
- This needs an example
22Case Example
case fn in .dat) echo Processing a .dat
file .sas) echo Processing a
.sas file ) catch all
pattern echo Unsure how to deal with fn"
esac
23Looping Constructs (while/until)
- While operates precisely as expected
- while (condition)
- do
- list
- done
- Until is a bit like its opposite
- until (condition)
- do
- list
- done
24Last Loopy Construct
- For is more like a foreach statement
- for identifier in word list
- do
- list
- done
- Example
- for file in .dat
- do
- echo Processing file
- done
25Command Line Arguments
- Officially termed positional parameters
- The number of parameters is in
- The actual values are stored in 1 through n
- All values are stored in , _at_
- Use these in the for loop construct to loop over
the command line arguments - Lastly, 0 stores the command line along with the
path
26Command line example
!/usr/bin/ksh for clop in _at_ do echo
clop done
27Shell Functions (intro)
- You can write functions in shell scripts
- A function ( procedure) must be defined before
it is called, because it is interpreted at run
time - It knows all the variables from the calling shell
except the commandline arguments. - But has it's own command line arguments so that
one can call it with different values from
different places in the script. It has an exit
status but cannot return a value like a c
function can
28Creating a function
- There are two ways to create functions
- function foo
- commands...
-
- foo()
- commands...
-
29Calling the function
- To call it just put it's name in the script
- foo
- To give it arguments do
- foo arg1 arg2
- The arguments are there in the form of 1...n
and for all at once like in the main code - And the main 1 is not influenced bye the 1 of a
particular function - The return statement exits the function
immediately with the specified return value as an
exit status
30Arrays
- Set and use an array like
- arrname14
- To fill in
- print arraname1
- To print out
- arrname
- Get all elements
- arrname
- Get the number of elements
31Others
- Read lines of text interactively
- Read command
- echo "What is your name?"
- read myname
- echo myname
- The math evaluator is very useful.
- Everything inside the double-parens gets
evaluated with basic math functions. - For example
- four((2 2))
- eight((four 4))
- print ((four eight))
32Resources
- Unix Shell Programming Patterns
- http//c2.com/cgi/wiki?UnixShellPatterns
- SHELLdorado
- http//www.shelldorado.com
- Microsoft Services for UNIX
- http//www.microsoft.com/windows/sfu/
- Dartmouth has a great tutorial
- http//www.dartmouth.edu/rc/classes/ksh/
- Useful Korn Shell Tutorial
- http//b62.tripod.com/doc/docksh.htmregex
- Incredible BASH Shell Tutorial
- http//tldp.org/LDP/abs/html/index.html
33The future
- Perl Regular Expressions
- Homework 1 will be delivered THURSDAY!!!
- An amazing announcement will be made as well!
- Introduction to C Programming
34Questions?