1
|
- Richard C. Davis
UW CSE – 10/4/2006
- Lecture 4 – Shell Scripting
|
2
|
- 48 hours left
- Office hours announced over cse303@cs
- Mailing list problems resolved (mostly)
- Check archives if you missed or joined late
- Got someone to talk to?
|
3
|
|
4
|
- C-s and C-g in shells
- Works on Linux, but not on Windows?
- Combining Commands
- Opposite of Java: 0 =3D=3D true
- cmd1 || cmd2 (OR: stop if any succeed)
- cmd1 && cmd2 (AND: stop if any fail)
- Fixed in slides
- We’ll touch on later
|
5
|
- Emacs Undo
- Undo: C-_
- Redo: <any char> and C-_ again
- Emacs shell-script-mode
- M-x shell-script-mode (space completes)
- Open a file with extension “.sh”
|
6
|
- Command-line editing
- Input/Output
- Rdirection
- Pipes
- Logic Expressions
- Variables
- Quoting and escaping
|
7
|
- Shell scripting details
- Variables and Arrays
- Arithmetic Expressions
- Logic Expressions
- Control Structures
- Java vs. Bash programming
|
8
|
- Put all commands in file “script”
- source script
- All commands executed!
- Problem
- State of shell is changed
|
9
|
- Method
- Start a new shell
- Share stdin, stdout, stderr
- exit
- Advantages
- Shell script is like any other program
- Shell commands are programming language
- But it’s a bad language for anything else!
|
10
|
- Process
- First line of script =3D #!/bin/bash
- Get “execute” permission: chmod u+x script
- Run it: script (or ./script if . not in $PATH)
- Why this works
- The shell consults the first line
- If shell program is there, launch and run script
- Else, it’s a “real” executable, so run it
|
11
|
- You don’t export and start a new shell
- printenv : show vars visible to new shells
- set : show all variables
- You declare a function AND declare local
|
12
|
- Special Variables
- $0: program name
- $1: first argument ($2 second argument …)
- $#: number of arguments
- $@: same as $1 $2 …
- What if you want to handle many args?
- shift: shifts argument numbers down
|
13
|
- One dimensional, no fixed size
- Make an array: foo=3D(x y x)
- Set element: foo[2]=3Dhi
- Get element: ${foo[2]}
- Get number of elements: ${#foo[*]}
- All elts. separated by spaces: ${foo[*]}
- Examples: arrays.sh
|
14
|
- All variable values held in strings
- If told, shell converts to numbers (or 0)
- Three ways to do arithmetic (integer)
- Method 1: `expr $i + 1`
- Method 2: ((i =3D i + 1)) or $(($i+1))
- Spaces and $ optional inside (( ))
- Method 3: let “i =3D i + 1”
- Quotes permit use of spaces
- Examples: arithmetic.sh
|
15
|
- Through return code of test i.e. [ ]
- 0 means true
- Non-0 means false
- Lots of things to test
- File tests (exists, non-empty): [ -s file ]
- String tests (=3D): [ s1 =3D s2 ]
- Numeric tests (<): [ n1 -lt n2 ]
- Combining tests (&): [ test1 -a test2 ]
|
16
|
- Two ways to group : (v1>v2 AND v1<v3)
- Separate into different tests
- [ $1 -gt $2 ] && [ $1 -lt $3 ]
- Use \( and \)
- [ \( $1 -gt $2 \) -a \( $1 -lt $3 \) ]
- Examples: logic.sh
|
17
|
- if command
- then
- body
- elif command2
- then
- body2
- else
- body3
- fi
|
18
|
- for variable in list
- do
- body
- done
- list can be
- A given set: a b c 1 2 3
- Content of an array: ${foo[*]}
- File pattern: *
- Result of a command: `cat numbers.txt`
- Examples: loops.sh
|
19
|
- case statement
- while loop
- until loop
- break and continue
- See Linux Pocket Guide p 171-175
|
20
|
|
21
|
- Typo in variable set makes new variable
- Typo in variable use gives empty string
- Non-number used as number gives zero
- Omit array superscript, get first element
- Array out-of-bounds
- Increases size or returns empty string
- Spaces, substitution order, braces?
- Start a shell and experiment (or try manual)
|
22
|
- Shell
- (+) shorter, convenient file-access, file-tests, program execution,
pipes
- (-) crazy quoting rules and ugly syntax
- Java
- (+) cleaner, array checking, type checking, real data structures
- (-) hard to do manipulate files, users, processes
|
23
|
- Never do something manually if writing a script would save you time<=
/li>
- Never write a script if you need a large, robust piece of software=
li>
- Some languages straddle the middle
|
24
|
- Shell scripting details
- Variables and Arrays
- Arithmetic Expressions
- Logic Expressions
- Control Structures
- Java vs. Bash programming
|
25
|
- Linux Pocket Guide
- P170-176 (Control Structures)
- Bash Reference Manual
- 6.5 Arithmetic
- 6.7 Arrays
|
26
|
|