Title: Shell Scripting
1Shell Scripting
2Intro to Shell Scripting
- What is a shell?
- Process environment accessed by CLI
- Has the ability to start other programs as well
as a programming language - What kinds of shells are there?
- BASH Bourne Again Shell
- CSH C Shell
- KSH Korn Shell
- Differences are minor, mostly grammar.
3Components of a Shell Script
- ASCII text file
- Can be used as a script, or in an active shell
- Can call any executable, or other shells
- Language support for things like
- Conditional testing
- Looping
- Variables
- Environment
- Functions
- Arrays
4Running a Shell Script
- First time write of a shell script requires
- change the mode to make executable (preferred
way)? - chmod x my.sh
- run from shell executable at command line
- bash my.sh
- based on PATH variable, may need to start
relative path - ./my.sh instead of my.sh(may say no such
file)? - Executing shell in debug mode
- bash -x my.sh
5Meta Characters in the Shell
- Pattern Matches
- Every file in the current directory
- ? Files consisting of one character
- ?? Files consisting of two characters
- ?? Files consisting of two or more
characters - abcdefg Files consisting of a single letter
from a to g. - gfedcba Same as above
- a-g Same as above
- a-cd-g Same as above
- a-zA-Z0-9 Files that consist of a single
letter or number - !a-zA-Z0-9 Files that consist of a single
character not a letter or number - a-zA-Z Files that start with a letter
- ?a-zA-Z Files whose second character matches
a letter. - 0-9 Files that end with a number
- ?0-9 Two character filename that end with a
number - .0-9 Files that end with a dot and a
number
6Special Characters in Shell Scripting
- Character Meaning
- pipe for I/O
- end of cmd - background
- terminate statement
- lt gt less than greater than
- ( ) cond testing
- variable
- " ' quotes
- \ remove meaning for next char
- ltspacegt lttabgt ltnewlinegt space tab newline
- wildcard (be careful!) example
- ? decimal exit status
- cond testing
- comment
- used for home directory
7Reserved words in Shell Scripting
- Word Meaning
- ! logical NOT
- braces
- case esac case open/close
- do done do loop syntax
- if then else elif fi conditional testing syntax
- for in for loop syntax
- until until loop syntax
- while while loop syntax
8Command Names in Shell Scripting (some
examples)?
- Name Meaning
- alias command aliases
- true / false logical true/false values
- jobs foreground/background
- wait wait until cmd executes
- bg / fg force a job in background or
foreground - cd change directories
- kill send process signal
- pwd print working directory
- fc fix command
9Quoting in Shell Scripts
- 3 types of quotation, very different meanings
- double quote
- Meaning
- everything in quotes is a single string
- evaluate and plug in any variables
- single quote '
- Meaning
- everything in quotes is a single string
- do NOT evaluate variables, literal on everything
- back quotes
- Meaning
- execute string in quotes
10Quoting example in Shell Scripting
- VARABC
- Double Quotes
- echo Hello World - VAR
- Hello World - ABC
- Single Quotes
- echo Hello World - VAR
- Hello World - VAR
- Back Quotes
- echo VAR
- ERROR can not execute command ABC
11Variables in Shell Scripting
- namevalue pairs
- A2
- use as A from then on..
- Name can be the following
- starts with a value a-Z, not numeric
- any string that does not contain a special
character - Value can be anything, be careful of special
characters!! - Environmental variables already set
- export!
12I/O and shell scripting
- I/O is input, output and error
- Known to the shell as
- Input comes from the 'lt' character
- cat lt filename
- Output uses the 'gt' character
- cat filename gt other-filename
- Error uses the '2gt' characters
- 2 stands for the file descriptor 2, output is 1
and input is 0 - cat filename gt other-filename 2gt errors
- Pipe '' is the standard out of cmd1 as standard
in of cmd2 - cmd1 cmd2
13I/O and Shell Scripting con't
- Appending to an existing file
- Use the gtgt characters to append
- cat filename gtgt filename2
- note if filename2 does not exist, it will
create and add text - Copying where output goes
- Use the '' character
- cat filename gt filename2 2gt1
- Here-to document
- Ability to run commands until the word is met
- cat ltlt EOF
- do something
- EOF
14Arithmetic in Shell Scripting
- Arithmetic expansion
- ((expression))?
- echo ((4 2))?
- 6
- A 4 B3
- echo ((4 3))?
- 12
15Conditional Testing
- if then elif else fi
- based on true false values
- if test then
- do something
- fi
- if A -eq 1 then
- echo 'A' is A
- fi
- if ls -la then
- echo Can ls -la
- fi
if A -ne 1 then echo A is not 1 elif
A -gt 2 then echo A greater than
2 else echo A less than 1 fi
16Conditional Testing what you can test for?
- if test is true do something
- Can test for the following
- exit value for executable
- e.g. if ls -la then
- using the test syntax
- e.g. a ABC - string test
- !
- e.g. a -eq 12 - numeric test
- -eq, -lt, -gt, -ne, -le, -ge
17Conditional Testing - cont
- Operand Meaning
- If the right hand side matches a pattern,
(i.e., similar to filename matching, with
asterisks and question marks.) the condition is
true. - ! If the right hand side doesn't match a
pattern, the condition is true. - -d var True if the file is a directory.
- -e var True if the file exists.
- -f var True if the file is a file. (I.e., not
a directory) - -o var True if the file is owned by the user.
- -r var True if the user has read access.
- -w var True if the user has write access.
- -x var True if the user has execute access.
- -z var True if the file is zero-length.
18Conditional testing con't case
- case statements
- case "year" in
- 0-90-9)?
- year19year
- yearsexpr year - 1901
-
- 0-90-90-90-9)?
- yearsexpr year - 1901
-
- )?
- echo 1gt2 Year \"year\" out of range
... - exit 127
-
- esac
19Looping constructs
- 3 different kinds of looping
- for
- while
- until
- All accomplish the following, but 'for' is
different because it iterates on the variable/cmd
sent to the command.
20Looping constructs - for loop
- For each item in command
- for VARIABLE in LIST
- do
- something
- done
- alphabet"a b c d e" Initialise a string
- count0 Initialise a counter
- for letter in alphabet Set up a loop control
- do Begin the loop
- countexpr count 1 Increment the
counter - echo "Letter count is letter" Display
the result - done End of loop
21Looping constructs - while loop
- while test is true do something
- while TRUE
- do
- something
- done
- alphabet"a b c d e" Initialise a string
- count0 Initialise a counter
- while count -lt 5 Set up a loop
control - do Begin the loop
- countexpr count 1 Increment the
counter - positionbc count count - 1
Position of next letter - letterecho "alphabet" cut
-cposition-position Get next letter - echo "Letter count is letter"
Display the result - done End of loop
22Looping constructs - until loop
- until test is true do something
- until TRUE
- do
- something
- done
alphabet"a b c d e" Initialise a
string count0 Initialise a
counter while count -lt 5 Set up a
loop control do Begin the loop
countexpr count 1 Increment the
counter positionbc count count - 1
Position of next letter letterecho
"alphabet" cut -cposition-position Get
next letter echo "Letter count is
letter" Display the result done
End of loop
23Functions
- Shells have the ability to utilize function calls
- deal with a file, add people one at a time
- do_file()?
-
- while parse_one
- etc...
-
- etc...
- take standard input (or a specified file)
and do it. - if "1" ! "" then
- cat 1 do_file
- else
- do_file
- fi
24Questions?