Warning! This site is under development.
This class will be recorded! Its use will be limited to members of the class. Please do not share with others.
Approximate overview
For quick tasks, it’s nice to know how to write loops and such.
Three important mechanisms
#!/bin/bash.bashrc or .bash_profile.
Declaring / initialize
NAME=EXP
E.g.,
hello="hello world"
Using
E.g.,
echo $hello
Variables generally last until the shell/program is closed.
Except … you can sometimes write export VAR to export the
variable to the enclosing shell (and beyond). (It works in your
.bashrc and .bash_profile.)
There are some variables that get used regularly by other programs. We often to these as environmental variables and name them with all caps.
PATH is a colon-separated list of directories where the shell
looks for the commands you type.When you are using the shell, it keeps a log of all the commands you’ve typed, up to some number.
You can type !number to redo that command.
You can type !letters to redo the last command that started with
those letters.
!$ refers to the last “word” on the previous line.
You can also redo a modified version of the previous command with
^pattern^replacement
if [ TEST ]
then
COMMAND
fi
if [ -f file.c ]
then
echo "Whoo, you have file.c"
else
echo "Nope, no file.c"
fi
for VAR in LIST; do
COMMANDS
done
for file in *.txt; do
echo "$file"
done
for file in *.txt; do
newfile=`echo $file | sed -e 's/txt/bar/'`
mv $file $newfile
done
for file in `grep -l Hello *.txt`; do
newfile=`echo $file | sed -e 's/txt/bar/'`
mv $file $newfile
done
while (TEST) do
COMMANDS
done
while (1) do
ls -lt FILE
sleep 10
done
function NAME() {
COMMANDS
}
The arguments are $1, $2, $3, …
function compile() {
cc $1.c -o $1
}
function rename() {
for file in *.$1; do
newfile=`echo $file | sed -e s/$1/$2/`
mv $file $newfile
done
}
The moral
Core idea: Instead of writing programs to match text, we can write “patterns” (declarative programs) to describe the text we want to match. The patterns we use are simple enough that we can efficiently make pattern-matchers.
Problem the DFA for a particular NFA can be exponentially larger; Ton of hacks to avoid building all at one.
Most of the basic things before recursion.
a matches the letter a.b matches the letter b.^ usually matches the start of a line (or a piece of text).$ usually matches the end of a line (or end of a piece of text).. usually matches any one character.\ is used as a meta character.\^ matches the caret\. matches a period\w matches a word character\B matches a word boundary\\ matches the backslash.[abcd] matches any one thing that appears in the brackets.[^abcd] matches any one thing that doesn’t in the brackets.[a-e] matches anything between a and e[:alnum:] is a regular expression that names any alphabetic
character or number character. (Sam forgets the names and has to look them up.)
[:alumnium:] does not match anything.Recursion
R1 and R2 are regular expressions then R1R2 is a regular
expression that matches sequences whose first part matches R1 and
whose second part matches R2. ab matches ab, and a[0-9] matches
a0 and a1 and a2 and … a9R is a regular expression, R* is a regular expression matching
0 or more copies of R. a* matches sequences of 0 or more copies of
a (Repetition)R1 and R2 are regular expressions, then R1|R2 is a regular
expression that matches either R1 or R2We may want to look for files that contain certain strings (e.g., to figure out where you’ve defined a function, to solve crossword puzzles).
Some useful files for practice
/usr/share/dict/words contains 200,000+ words in the English
language.a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u
We could do some variants of that for fun.
^[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*$Other fun things to do with grep
Fun arguments to grep
-l - Lists all the files without printing the matching line-n - Gives line numbers-v - Reverse matching - only lines that don’t match-i - Case insensitive-c - Only get a count of lines (which is easier than | wc -l-q - Quiet mode. Returns true or false. Sam apparantly sucks at
using it in the shell on the fly.-w - Match the pattern to words, rather than the line, and require
that they match the whole word.Stream Editor: Intended for editing “streams” (sequences of text), mostly good for batch editing. But it does some fancy batch editing.
Challenge: We have a file in the form “Last,First” and want to translate to “First Last”. How?
(RE) gives you a numbered RE \1 and \2 in the replacement give you the contents of the numbered RE