Topic 3: Shell Scripting

mewstennisΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

88 εμφανίσεις

1
CISC 220 fall 2012, set 3: Shell Scripting
Topic 3: Shell Scripting
Before we start: an administrative note about C.
We'll start C in Week 6 & continue using C during the rest of
the term.
Just 3-4 lectures on chapters 1-4 of C text.
Assuming knowledge of Java (C-like) syntax from CISC 124
Think about reading ahead & practicing early if:

You have never used C

Your Java is shaky

You are not confident about programming
2
CISC 220 fall 2012, set 3: Shell Scripting
What Is A Scripting Language?
1. A programming language designed to control the
operation of other software applications.
2. Usually interpreted (not compiled)
bash:

controls operation of Linux/Unix kernel, use of
many other programs

can be used interactively or by putting a series of
commands into a file (script)
3
CISC 220 fall 2012, set 3: Shell Scripting
Basic Commands & Control Structures
Basic commands in a typical language like Java/C:
● assignment statement
● call a function
Basic commands in bash:
● set value of shell variable
● built-in shell commands ("cd", "echo", etc.)
● run a program or other shell script
All languages have control structures as well:

selection (if, switch, etc.)

repetition (for, while, etc.)
4
CISC 220 fall 2012, set 3: Shell Scripting
Topics To Cover
Techniques:

conditionals (if)

shell variable tricks

loops (while & for)

command substitution

user input (FYI, won't be tested)
Useful tools: basename & dirname
One more miscellaneous topic in these slides: links
5
CISC 220 fall 2012, set 3: Shell Scripting
Every bash command has a numerical exit status.
0 means success, anything else means failure
$? means exit status of last command
Exit Status
6
CISC 220 fall 2012, set 3: Shell Scripting
Default: status of script = status of last command executed
To change that: exit command:
exit n: exit from script now, exit status is n
Specifying Exit Status of a Script
7
CISC 220 fall 2012, set 3: Shell Scripting
Formal syntax from Bash Reference Manual:if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else
alternate-consequents;]
fi
The if Command
8
CISC 220 fall 2012, set 3: Shell Scripting
[[ expression ]]: Evaluate expression as boolean condition
Spaces around brackets are important!
Simple string comparisons with ==, <, > (no <= or >=)
Combine comparisons with && and ||, and parenthesis.
String Tests in Conditions
if [[ $HOME == $PWD ]]
then
echo I am home
else
echo I am somewhere else
fi
Example: Script to decide if a string starts with b.
Options: print a message, return a status result, or both
9
CISC 220 fall 2012, set 3: Shell Scripting
Remember: to use the value of shell variable X:
$X or ${X}
Shell Variables: Advanced Use (1)
There are variations on this syntax to add checks or
string manipulation:
${var:-alt}
expands to alt if var is unset or an empty string
otherwise, expands to value of var
${var:offset:length}
expands to a substring of var
indexes start at 0
if length is omitted, goes to end of string
negative offset starts from end of string
must have a space between the : and the negative sign!
10
CISC 220 fall 2012, set 3: Shell Scripting
Shell Variables: Advanced Use (2)
${#var}
expands to the length of $var
${var/pattern/string}
expands to the value of $var with the longest match of
pattern replaced by string. The pattern may contain
wildcards as with file names.
${var//pattern/string}
expands to the value of $var with all matches of
pattern replaced by string.
11
CISC 220 fall 2012, set 3: Shell ScriptingThe following may be used inside [[ .. ]]
-e file: file exists
-d file: file exists and is a directory
-h file: file exists and is a symbolic link
-f file: file exists and is a regular file
-r file: file exists and is readable
-w file: file exists and is writeable
-x file: file exists and is executable
file1 nt file2: file1 is newer than file2
(or file2 doesn't exist)
More Conditional Operators
&& = and
|| = or
! = not
12
CISC 220 fall 2012, set 3: Shell Scripting((expression)): evaluates expression as arithmetic expression.
Exit status is 0 (success) if value of expression is non-zero.
Has all the usual comparisons, including <= and >=.
Arithmetic in Conditions
Example:if (($X<=7))
then
....
13
CISC 220 fall 2012, set 3: Shell Scripting
Can assign values inside ((...)).
Difference between:

X=5+2

((X=5+2))
Arithmetic Assignments
14
CISC 220 fall 2012, set 3: Shell ScriptingSyntax from Bash Reference Manual:while test-commands; do consequent-commands; done
while Command
((X=1))
while (($X<=4))
do
echo $X
((X++))
done
Example: script to print factorial of argument
15
CISC 220 fall 2012, set 3: Shell Scriptingfor name [in words ...]; do commands; done
for Command
Examples:
for X in *; do echo $X; done
for PARAM in $*;.....
for VAL in 3 5 9; ....
for FILE in a* *.txt
Note: default list is $* -- all the command-line arguments.
So the following are equivalent:
for P in $*; do ....
and
for P; do ...
16
CISC 220 fall 2012, set 3: Shell Scripting
shift -- no arguments
shift Command
Effect: shift positional parameters ($1, $2, $3, etc.) left by 1
Another way to loop through all positional parameters:
while (($# > 0))
do
echo $1
shift
done
17
CISC 220 fall 2012, set 3: Shell Scripting
dirname takes one argument: a file name
prints the directory part of the file name
~:266: dirname /cas/course/cisc220/pride.txt
/cas/course/cisc220
Useful Command: dirname
With a simple name, the directory part is "." :
~:271: dirname myfile
.
File name doesn't have to name an existing file:
~:273: dirname nosuchdir/nosuchfile
nosuchdir
18
CISC 220 fall 2012, set 3: Shell Scripting
With one argument: does the opposite of dirname  strips off
directory(s) and leaves base file name.
~:275: basename /cas/course/cisc220/pride.txt
pride.txt
Another Useful Command: basename
With two arguments: second argument is a suffix.
If suffix matches end of file name, it is stripped off
Otherwise, no effect (not an error).
~:277: basename /cas/course/cisc220/pride.txt .txt
pride
~:278: basename /cas/course/cisc220/pride.txt .cpp
pride.txt
19
CISC 220 fall 2012, set 3: Shell ScriptingMotivation: Sometimes you want to use the result of a command
in another command.
Command Substitution
$(cmd) = output of cmd
Alternate syntax: `cmd`
Examples:

script to list everything in the same directory as argument.

take a file listing names of files and loop through all those files
20
CISC 220 fall 2012, set 3: Shell Scripting
User Input
read var
Waits for user to enter input, then assigns input line to var.
read var1 var2
Assigns first word of input to var1, rest of line to var2
May extend to more words.
Unused words are set to empty string
read -p prompt variables
Prints a prompt first
Example: simple guessing game
21
CISC 220 fall 2012, set 3: Shell Scripting
Order of Events For Command Line
1. bash reads command from script or command line
ll a* "my file" $(dirname $F) $HOME/x.txt 2>errs | less
2. breaks down into tokens:
ll a* "my file" $(dirname $F) $HOME/x.txt 2>errs | less
3. resolves aliases (only in interactive mode)
ls -l a* "my file" $(dirname $F) $HOME/x.txt 2>errs | less
4. breaks into simple commands joined by pipe
ls -l a* "my file" $(dirname $F) $HOME/x.txt 2>errs
less
pipe
22
CISC 220 fall 2012, set 3: Shell Scripting
Order of Events, continued
pipe
ls -l a* my file $(dirname $F) $HOME/x.txt 2>errs
less
5. shell variable substitution
ls -l a* my file $(dirname subdir/file2)
/cas/staff/malamb/x.txt 2>errs
less
pipe
6. command substitution
ls -l a* my file subdir /cas/staff/malamb/x.txt 2>errs
less
pipe
7. wild card expansion
ls -l a1 a2 my file subdir /cas/staff/malamb/x.txt 2>errs
less
pipe
23
CISC 220 fall 2012, set 3: Shell Scripting
Order of Events, continued

ls -l a1 a2 my file file2 /cas/staff/malamb/x.txt 2>errs
less
pipe
8. I/O Redirection
ls -l a1 a2 my file subdir /cas/staff/malamb/x.txt
(memo: send standard error to file called errs)
less
pipe
9. Execute commands:

look up ls command on PATH

call ls program with parameters:

-l

a1

a2

my file

subdir

/cas/staff/malamb/x.txt

send error stream from ls to errs

pipe combined output to less command
24
CISC 220 fall 2012, set 3: Shell Scripting
Unix/Linux File Systems
Linux maintains a central table of i-nodes.
Each i-node contains information about one file:

size of file

device & location on device

owner, group and permissions

timestamps

link count
To see i-node number of a file: ls -i
To see contents of i-node: stat
25
CISC 220 fall 2012, set 3: Shell Scripting
Directories
A directory is a special kind of file.
Contains a mapping from file names to i-nodes.
Interesting consequence:
Two (or more) file names can refer to the same i-node
Called a hard link
26
CISC 220 fall 2012, set 3: Shell Scripting
Two file names can refer to the same i-node.
Restrictions:
1.no hard links across physical devices
2.no hard links between directories, just regular files
Hard Links
Creating a hard link:
ln file1 file2
file1 should already exist. file2 should not exist.
Creates a new directory entry called file2, referring to the same
i-node
27
CISC 220 fall 2012, set 3: Shell Scripting
Symbolic link is special file; contains "pointer" to another file
(points to original file's name, not its i-node)
No restrictions: can use across devices, can use with directories.
Symbolic Links
Example:
ln s file1 file2
file1 should already exist. file2 should not exist.
file2 is now a symbolic link to file1
28
CISC 220 fall 2012, set 3: Shell Scripting
Some operations/programs change file directly (same inode)
example: >> , vim, nano
Some change file by creating a new version
examples: emacs, cp
Very different behavior with respect to links!
Behavior of Hard & Symbolic Links
29
CISC 220 fall 2012, set 3: Shell Scripting
From a 2008 quiz: fill in output where applicable$ echo "one" > file1
$ ln file1 file2
$ ln -s file1 file3
$ echo "two" >| file1
$ cat file1
two
$ cat file2
two
$ cat file3
two
$ mv file1 file4
$ echo "four" >| file4
$ cat file2
output??
$ cat file3
output??
Example: Links (Part 1)
30
CISC 220 fall 2012, set 3: Shell Scripting
From a 2008 quiz: fill in output where applicable$ echo "aaa" > fileA
$ ln fileA fileB
$ ln -s fileA fileC
$ rm fileA
$ echo "bbb" > fileA
$ cat fileB
output??
$ cat fileC
output??
Example: Links (Part 2)
31
CISC 220 fall 2012, set 3: Shell Scripting
From a 2008 quiz: fill in output where applicable$ cat messages
echo "apple"
echo "banana" >&2
echo "cherry" > fruit
echo "lemon" > 2
echo "grape"
$ ./messages >good 2>bad
output??
$ cat good
output??
$ cat bad
output??
$ cat fruit
output??
Example: I/O Redirection
32
CISC 220 fall 2012, set 3: Shell Scripting
From a 2008 lab:
Write a script called maxArg which takes any number of
numeric arguments and prints the maximum of the absolute
values of those arguments. The script may assume that all
of its arguments will be non-negative integers. The
maximum of zero arguments is zero.
Example: Maximum Argument