Intro to bash scripting old

From FreekiWiki
Revision as of 11:45, 5 May 2010 by Paulmunday (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Class Description

The class: a six week course on Tues evenings covering basic and intermediate scripting in the bash shell. Each night, we will create a few scripts of about 15 lines of code, modify the code, run it, and talk about the results. We will also get to know the "Advanced Bash Scripting Guide" [1] and the Gnu "Bash Reference Manual" [2] and learn to research and solve our own programming problems. User projects are encouraged -- bring your problems and we will solve them together!

I am a fifth year Ph D student from Berkeley in Demography, writing an anthropology of a small lumber town in Oregon. I have worked as a programmer in Linux for almost 10 years, and I am currently employed part time as a software project manager at Portland State.

The first class: Feb 17, 5:00 to 6:30, 2009.

Some helpful links:

Class Outline

Day Zero: Introductions and adminitrivia. Command line refresher. How to find your way round the filesystem at a terminal. How to use the text editor nano. How to use version control with subversion. Collaborating with multiple people on version control.

Day One: What is a "script" and what is a "variable". We will write a simple script in nano, with comments, a "shebang" line, appropriate permissions, and simple output. We work on the idea of a variable, using shell expansion to assign output to variables, interpolating variables, and exporting environment variables. We will also examine the output and input streams ("stdin", "stdout", "stderr"). We will "comment-out" code. Finally we will talk about style, including indentation, variable names, trickiness, and comment-first scripting.

Day Two: "For-loops", "word splitting", and printf. We will explore the for-loop in all its glory, going over lists stored in variables. This will require a discussion of how Bash automatically splits strings into words and how we can control this through quoting syntax. We will also make interesting formatting commands with the printf command.

Day Three: Conditionals ("if/ then" statements). We will show how to write "if" and "case" statements, and incorporate pattern matching and "file tests" into our scripts.

Day Four: File input, "while-read", and useful scripting. We will investigate how to get an input line from a file, parse it into useable pieces, and do interesting things with that data (like write a simple email "spambot").

Day Five: SQL databases and the command line. We will use SQLite to store email addresses, query the table, and use the results to send spam to our friends.

Class Approach

The class is based on each student learning by doing -- writing and running code. Each class period I will have posted code on the wiki. You will "checkout" the class code repository. Inside this repository, you will type examples from this wiki page, with modifications to make your own scripts, run them from the command line, and fix bugs. At the end of the class, you will "commit" your new scripts for looking at later.

Code Listings and Class Notes

Every day instructions

Each class we will go through the same drill with checking out the code tree, working in it, and committing our changes. This allows us to save our work and collaborate.

Each new class period should be done within a new directory, and each new script is a different file within that directory.

Note that this class will only cover the tip of the iceberg for Bash scripting; to learn more -- follow the bash links above, type in the code you find while you read the explanations, and experiment. Be brave. You don't have enough permissions to break anything important.

Note also that this class is a work in progress, and I might jump into the wiki to make a change to the notes or the code for next time. Feel free to suggest changes or ask questions, especially if you buy me dinner after class.

Sometimes my instructions will be too terse -- ask questions, but try to learn what I am talking about and translate it into commands at the prompt without me specifying exactly what those commands are all the time.

Each script should be commented at the top with (1) your email address, (2) a sentence about what the script does, and (3) another sentence about HOW it does it in high-level terms.

Finally, give Charlie a big hand! He will be the TA for the class, walking around and helping, having successfully completed the first iteration of the soon to be famous FG Bash Scripting Class. All alumni are eligible to TA next time and earn valuable volunteer hours.

Zeroth day -- working on the command line, editing files, using version control

The command line, editing, etc.

Here we will practice working on the command line and editing files and such...

First note: DON'T EVER USE SPACES IN FILENAMES!!!! It just makes trouble you may not know how to get out of (yet). Just use alphanumeric characters, underscores, and dots -- nothing else for now!

Second note: WHEN LOST IN THE FILE SYSTEM, RUN `pwd` AND `ls -l`. It is a good habit anytime you change to a directory to run those two commands, read the output, and visualize where you are and what is happening.

Make a directory (`mkdir foo`), change current working directory to it (`cd foo`), list all the files in it (`ls`), print the current working directory (`pwd`), touch a file (`touch myfile.txt`), list the files again, delete the file created with touch (`rm myfile.txt`), change directories one level up (`cd ..`), delete the directory you started by making (`rmdir foo`).

Do it all again except use different names.

Make a directory and cd to it. Start editing a file (`nano myfile.txt`). Figure out how to save the file in nano. Exit nano. List files in your directory. Edit the file again with nano. Repeat until you understand what is going on.

Start a new directory, mess around like above, then look at permissions with the verbose option to ls, `ls -l`. Change permissions so you can't view the file (`chmod 0000 myfile.txt`), list the directory verbosely again, and try to edit the file. Change the permissions again to 0700. Edit it again. Then clean out the directory and get back to home.

Ask questions.

Repeat until understood. Talk with your neighbors

Subversion and google code

Here we will practice working with a code repository -- pulling files from a central location, modifying them, pushing our changes back to the central location, seeing what other people have done.

(Take a deep breath.)

Open a new browser window:

Sign in with your Gmail name (referred to as GOOGLENAME below) and its password (look in the top right corner for "Sign In").

Click the "source" tab. Under the heading Command Line Access you see a line that ends with the link " password". Click on this link, and you are taken to a new web page that displays an ugly 12 character PROJECTPASSWD. Write down this password and save it for future use. Then browser-back to the source tab page.

Just above the line with the link that you just clicked is a line that reads "svn checkout freegeekbash --username GOOGLENAME" . Open a terminal window, put this whole line (not the too similar line with "http://...") into the command line and hit Enter. You will be given a blank prompt for a password. Enter your PROJECTPASSWD. Wait a bit, read the output and notice you have a new directory freegeekbash with files and subdirectories. List the files, best with the -a or -al option. cd to freegeekbash, ls -al. cd to semester2, ls -al. Ask questions.

Here in semester2, make a directory named GOOGLENAME (remember--your Gmail name, without the ""), cd to GOOGLENAME, make a directory named class0, cd to class0, and finally create and edit a file here.

ONE STUDENT AT A TIME: cd up two levels (`cd ..; cd ..`); run `pwd` to make sure it is "~/freegeekbash/semester2". Run `svn add GOOGLENAME` to add your new directory, READ THE OUTPUT. Run
svn commit -m 'started a new dir for the bash class'
; this "commits" your changes to the central repository, along with a comment (after the "-m" switch) about them. MAKE SURE YOU PUT QUOTES AROUND YOUR COMMENT AFTER THE "-m".

ONE STUDENT AT A TIME: cd to ~/freegeekbash, run `svn update`, read the output. Then explore the new files (running `ls -al` and `pwd`) and directories underneath the directory "semester2".

Once all the students are caught up, everyone cd to ~/freegeekbash and run `svn update`, then `svn log`, and look at the output.


Rinse repeat until we can all deal with editing new and old files, making directories, committing, and updating.

MAKE SURE AT END: everybody run `svn commit -m "final commit at end of first day"` in ~/freegeekbash/

First day -- scripts and variables

What is a shell script anyway? It is ...

... a file of text ...

... full of unix commands, variables, and control structures ...

... that usually executes from top to bottom ...

... using variables to hold data ...

... and loops and conditionals to do fancy programming stuff...

... with a way to read input and write output ...

... that probably has some "side-effects".

Why learn shell? Old-school Unix style? Class?

Getting code from repository

svn checkout freegeekbash --username PROJECTPASSWD

cd freegeekbash/semester/GOOGLENAME/

mkdir class1

cd class1

# Do your programming thing here

Class 1 - Script 1

Following just prints my name. Run it as is, but then change it to print your name.

# script 1 2008-10-29
# Script just prints my name


Edit it by running the following: nano, then save it (ctrl-O in nano), then chmod 0700, and run as


in the other terminal window.

Class 1 - Script 2

This script takes a parameter from the command line and uses it as the name: "variable expansion". Note the expansion, but also how different quotes or lack thereof have different effects.

# script 2 2008-10-29
# Prints names from command line

echo "Hello, $NAME."
echo 'Hello, $NAME.'
echo Hello, $NAME.
echo Hello, "$NAME".
echo Hello, \"$NAME\".
echo "Hello, ${NAME}with text."
echo "Hello, $NAMEwith text."

try from the command line:

./ Foobar
./ "Foobar Smith"
./ Foobar Smith 

Class 1 - Script 3

This script does some basic math, and then outputs it using variable expansion. Check out the quoting. Also note what happens when we try to do math on weird input.

# Script adds two numbers from command line

RES=$(( $LEFT + $RIGHT ))
echo $(( $LEFT + $RIGHT ))
echo '$(( $LEFT + $RIGHT ))'
echo "$(( $LEFT + $RIGHT ))"
echo "$LEFT + $RIGHT = $RES."
echo '$LEFT + $RIGHT = $RES.'  # Why does this do what it does?

try from the command line:

./ 100 10        
./ 'one hundred' 'ten' 
./ 34           

Class 1 - Script 4

This script does some "shell expansion" using the unix command "date",
   which gives a formatted string of the date; use "date --help" to see more).
# Script 4.  Script calculates the year if we give it years from now.

YEAR=$( date +'%Y' )
echo "Start at year $YEAR, finish at year $(($YEAR + $YEARS_FORWARD))"

Try from the command line

./ 10
./ "ten"

Class 1 - Script 5

# Script 5 takes a filename, strips any spaces from it, and moves the original file
#    to the new name.

RES=$( echo $1 | sed 's/ /_/g' )
echo "After stripping of spaces, \"$1\" looks like \"$RES\""  # Why do we escape the quotes here?
echo mv "$1" "$RES" # try this same script without "echo" here, after touching a file with a name that you put into the parameters of the script

Try from the command line:

touch "a file with spaces in the name"
./ "a quoted filename with spaces"
./ an unquoted filename with spaces
././ a_filename_wo_spaces

Committing your changes

cd freegeekbash/GOOGLENAME/

svn update

svn add class1

svn ci -m 'Committing code for class 1'

# Make sure you see update -- call the teacher if not

svn update


Coding style -- variable names, comments, indentation, trickiness.

Comment first design.

"Commenting out" code.

Second day -- for-loops

First checkout the code following the instructions (bad as they may be) for the previous day.

What is a for-loop? It is a bash construct that repeatedly grabs one item from a sequence of data separated by whitespace, does something with that piece of the sequence, until there is no more data.

Here is the paradigm:

  # bunch of statements in here that are done repeatedly
  # referencing $VARIABLE
  # more statements

Finger exercises

Edit a few files with nano, find them with ls, and delete them with rm

Run the command `seq 1 10` from the command line

Run the command `printf "hello %s, my name is %s, I am %i years old" Tarzan Jane 32`

Run the command `factor 144` from the command line

Class 2 - Script 0

# script 0 2008-10-29
# Basic for loop with seq and printf


printf "Start = %i,  finish = %i\n" $START  $FINISH   
for X in $FULL_SEQ; do
    echo "touching file.$X"
    touch "file.$X"
    COUNT=$(( $COUNT+1 ))
printf "finished working on %i files\n" $COUNT

Save this as, then chmod 0700 Run as follows:

./ 1 10
./ 4 14

Examine your directory to see the new files and their names. Those are from this script.

Class 2 - Script 1

# script 1 2008-10-29                                             
# Do some fancy formatting with printf,                                               
#    by calculating the first 10 "orders of magnitude", and printing them             

SEQ=$( seq 0 $MAX)
echo $SEQ
for I in $SEQ; do
    OUT=$(( 10 ** $I ))
    printf "The %3i order of magnitude = %i.\n" $I $OUT

Run this as


Modify it to make "MAX" store the number from $1.

Class 2 - Script 2

The following factors a number and creates a bunch of files based on the result. Note the sed pipeline below. As Charlie correctly inferred over email, it erases any initial sequence of numbers that is followed by a colon. needed because the output of factor 5040 begins with "5040:" before the factors 2 2 2 ...

# script 2 2008-10-29                                                   
# Takes a name and a number as parameters,                                                  
#    factors the number, touches all files "$name.$number".                                 

NAME=foobar # use $1 
FACTORS=$(factor $NUMBER | sed 's/^[0-9]*://g' )
printf "Factors working on: %s\n" "$FACTORS"

for SUFFIX in $FACTORS; do
    TEST=$(($TEST * $SUFFIX))
    COUNT=$(($COUNT + 1))
    touch "$NAME.$SUFFIX"
    printf "Touched:  prefix = %s, suffix = %i\n" $NAME $SUFFIX
printf "\nFinished working on %i files\n" $COUNT

Run this script as


Then modify it to set "NAME" and "NUMBER" from the command line.

Class 2 - Script 3

Script 3 prints out times table using TWO loops, one "inside" the other.

# script 3 2008-10-29                                                                         
# Generate times tables by using a nested loop.                                                                   

INCREMENT=1 # try changing this                                                               

# Print top row of output table                                                                                                   
printf "      "
for x in $SEQ; do
    printf "%4i " $x

# Print top row separator                                                                                         
printf "\n     "
for x in $SEQ; do
    printf "_____"


# Fill in each row with left label and cell result                                                                
for x in $SEQ; do
    printf "\n%4i| " $x
    for y in $SEQ; do
        printf "%4i " $(( $x * $y ))
printf "\n"

Run this as

./ 1 12


Debugging loops with echo statements

Precalculating things like sequences

Code style -- when to add whitespace between sections and stanzas

Third day -- conditionals

Class 3 - Script 1 -- determine relative order of two numbers, and check input validity

# script 1 2008-10-29                                                                         
# Categorize numbers based on input.  
#    Also check input (finally!).                                                                                       

USAGE='./ num1 num2'
ERROR="Error.  Usage: $USAGE"

# Handles the input
if [[ $1 ]]; then                 # $1 is "true" if something is in there
    echo $ERROR
    exit 1

if [[ $1 ]]; then                                       
    echo $ERROR
    exit 1

# Compares the left and right variables and prints a message
if [[ $LEFT < $RIGHT ]]; then
    echo "$LEFT is strictly lesser than $RIGHT _lexicographically_"
elif [[ $LEFT > $RIGHT ]]; then
    echo "$LEFT is strictly greater than $RIGHT _lexicographically_"
    echo "$LEFT is exactly the same as $RIGHT _lexicographically_"

Do the chmod 0700 etc dance. Try this as

./script1 aardvark zebra
./script1 1 9
./script1 10 9
./script1 foo

Class 3 - Script 2 -- filter prime numbers

Prints out prime numbers, uses a lot of crazy conditional operations.

#  displays prime numbers less than input parameter.=
#  tests for input validity
#  breaks out of loop once go over max
#  does lots of stuff!

# write a function that tweaks factor command
function myfactor (){
    OUT=$(factor $1 | sed 's/^[0-9]*: //')
    echo "$OUT"

# Verify input
USAGE=" num"
if [[ $1 && $(echo $1 | grep '^[0-9][0-9]*$') ]]; then             ## Note boolean operator
    echo $USAGE
    exit 1;                     ## "1" signifies error.  Run a script and then try "echo $?" 

# Verify script has not already been than run for given number
if [[ -e $MAX ]]; then
    echo "Already ran script for number $MAX.  rm file to do repeat." 1>&2
    exit 1
    touch $MAX

# Get prime numbers, but not above MAXMAX
for X in $(seq $MAX); do 
    # Test whether have more factors than input number
    # when X is a prime, it has only one factor which is itself
    # this loop prints a list of primes
    FACTORS=$( myfactor $X ) 
    if [[ $X == $FACTORS  ]]; then
        echo $X
        echo "foobar" > /dev/null                       # do nothing

    # test whether above MAXMAX, "break" out of loop if so.
    if [[ $X -gt $MAXMAX ]] ; then
        echo "won't let $X go above $MAXMAX. Breaking out of loop"


# do something to highlight how the break statement works
echo bye

Run this as

./ 80
./ 800
./ blahblahlbah

Class 3 - Script 3 -- using case

# Script that does some silliness  case statements

USAGE="./$0 input" 
if [[ $1 ]]; then
    ARG=$1; shift
    echo "$USAGE"

case $ARG in 
    foo | bar | baz) echo "speaking unix baby-talk";;
    mama | dada) 
        echo "speaking real baby-talk";; ## note multiline block
    [0-9]*) echo "numbers"
        touch $ARG.file
    mama ) echo ma-ma;;         ## we won't ever do this
    *) echo other stuff;;

Run this as

./ 12
./ mama
./ dada
./ "this is a long line"



Fourth day -- while-read and functions

We are going to read a file with email addresses (and other information like "valid") and send spam to all of them.

The main things to look out for here are (1) writing a "function", (2) "sourcing" one bash script inside another script, and (3) reading and processing line-by-line input to do something useful.

Class 4 - Script 1 -- simple function

The function syntax creates a "mini" command that can be accessed wherever it is sourced.


# creates a function (just like a new unix command), that sends                                  
# semi-customized spam to the recipient                                                                

function spam() {
    if [[ $1 ]]; then
        local RECIP=$1
        echo "spam:  missing recipient parameter" 1>&2
        return 1

    local TODAY=$(date +%A)

    printf "First para:  Buy me!\n\nSecond para: type your password!\n" \
        | mail  -s "$TODAY's winning lotto ticket" $RECIP

    printf "Finished: %s\n" $RECIP 1>&2

    if [[ -e "$RECIP" ]]; then
        echo "removing $RECIP" 1>&2
        rm "$RECIP"

    return 0

Try this from the command line

$ source

Class 4 - Script 2 -- simple while-read

# use "while-read" to parse a text file

# print column headings
printf "COLUMN-1    REMAINING\n"

# print four characters of first column, and then the first little bit of
# everything else.

while read V1 VPLUS ; do
        printf '"%4.4s..." "%.5s..."\n' $V1 "$VPLUS"
exit 0

Try this (note the funky "<<END" syntax -- I will explain...)

$ cat | ./ <<END
column1 some more date
column2 a whole lot more data


Class 4 - Script 3 -- source a function, while-read a file, and send data from file to the function

Now we are going to send the contents of a file through a script that sends spam to each email address on each new line


# processes a file, sends spam to each valid email address (verified with grep
# and an "if" statement)

# By "sourcing" the function in this file, we can use all its code without
# looking at it

# set up some variables

# process the file from "standard input"
while read V1 VPLUS ; do
        if echo $V1 | grep $FULL_EMAIL_PATTERN ; then
                spam $V1
                echo "spammed (internet): $V1" 1>&2
                SUCCESS_COUNTER=$(( $SUCCESS_COUNTER + 1))
        elif echo $V1 | grep $USER_EMAIL_PATTERN; then
                spam $V1
                echo "spammed (local): $V1" 1>&2
                SUCCESS_COUNTER=$(( $SUCCESS_COUNTER + 1))
                echo "unreadable email: $V1" 1>&2
                FAILURE_COUNTER=$(( $FAILURE_COUNTER + 1))

# summarize what we did and exit
echo "Successfully spammed $SUCCESS_COUNTER emails!" 1>&2
exit 0

Try this (use your own email where it says "USEREMAIL"):

cat | ./ <<END

Fifth Day -- Using SQL and Bash Scripting

Discussion and accumulated questions?

SQL, from the SQL prompt

Resources (other than sucky online tutorials):

Lets make a table of email addresses and stuff for our famous spambot! We will also play around a little bit at the SQL prompt.

Notes: (1) single quotes to delimit strings. (2) unquoted names refer to columns in a query. (3) Up-arrow is your friend. (4) Semicolon terminates statements. (5) "Metacommands" start with a dot, don't need semicolon. (5) Multiline queries OK with final semicolon at end. (6) WAY more than we can cover today! (7) Don't type the prompt stuff "sqlite>" -- that is just to remind you of the example's context. (8) Ctrl-D to send sqlite an end of file.

Let's make a table of spam targets and play around with the database.

$ sqlite3 mydb.sqlite

sqlite> select 2 + 3, 'I am a string';
sqlite> .help
sqlite> .tables
sqlite> create table spammables (emailAddress text, demographic text, timesSpammed int);
sqlite> .tables
sqlite> insert into spammables (emailAddress, demographic, timesSpammed) values ('', 'rich', 0);
sqlite> insert into spammables (emailAddress, demographic, timesSpammed) values ('', 'poor', 0);
sqlite> select * from spammables;
sqlite> alter table spammables add column gender text;
sqlite> update spammables set gender = 'm' where emailAddress = '';
sqlite> update spammables set gender = 'f' where emailAddress <> '';
sqlite> update spammables set timesSpanned = timesSpammed + 1 where timesSpammed = 0;
sqlite> delete from spammables where gender = 'm';
sqlite> select * from spammables;
sqlite> delete from spammables;
sqlite> select * from spammables;
sqlite> drop table spammables;
sqlite> .tables

(Almost) running sql inside the shell script

Create a file "dbPopulate.sql" in nano, copy the "create table" and "insert" statements from above, add some more insert statements. Then create a database by using it thus:

$ cat dbPopulate.sql | sqlite3 spamDb.sqlite

Check it thus:

$ echo "select * from spammables" | sqlite3 spamDb.sqlite > output.txt
$ nano output.txt

Using a database to run a spambot

Now we will use a while loop to read emails to spam and update the database each round to keep track of how many times we spammed them. There is a lot going on in this script, so ask lots of questions.

Note that we have a "dummy" spam function that just outputs something so we know we were inside it, without doing any real work. If you want to really send emails, uncomment the line that has "source" and comment out the spam function.

Also try using echo to figure out what the variables in the while loop contain, even though we only use one of them.


#source              # Whereever you defined the spam function last time                                                   
spam () {
    echo "spamming(): $1" 1>&2

echo "Starting spamming ..." 1>&2
IFS='|'                         # Input Field Separator                                                                               
EQUERY='select * from spammables;'
echo $EQUERY | sqlite3 spamDb.sqlite | while read EMAIL DEMO TIMES REST ; do
    spam $EMAIL
    echo "update spammables set timesSpammed = timesSpammed + 1 where emailAddress = '$EMAIL';" | sqlite3 spamDb.sqlite

echo 1>&2
echo "... finished spamming.  Here is the new table after updates: " 1>&2
echo $EQUERY | sqlite3 spamDb.sqlite 1>&2

Class meta discussion

  • Feedback about the class (also tell Laurel or post to list).
  • Feel free to edit wiki.
  • Teaching assistants -- this summer, I hope, and you all can be TAs!

Important ideas in software

  • Commenting code
  • Other languages
  • Software design
  • Version control systems
  • Crazy Unix programming (APUE)
    • Processes
    • Interprocess communication
    • File descriptors and input/ output
  • The character of Unix and programming:
    • DIY versus "shrinkwrapped"
    • Tools for later piping versus applications


  • How to help FG and practice programming with new skills
Personal tools