CS 279 - Week 8 Lecture 1 - 10-9-12

*   more on grep
    *   man grep 
        ...does lead to a proper man page that's
           just about grep!

    *   pp. 175-177 of the course text also go a bit
        further;

    *   we know that -E allows it to accept
        EREs (as does the command egrep)

    *   default behavior:
        unless you use an option that changes it,
        when you call grep with a pattern and a single
	   file name,
	   it outputs just the matching lines;
        when you call grep with a pattern and more
           than one file name (this includes a filename
	   expansion pattern),
           it outputs the pathname of the file as it
           was specified on the command line, a colon,
           and then the matching line

# for shell-script fun during this lecture... 8 - )
CS 279   CS 279

*   some options to change grep's default output:
    -n   precede each matching line by its line number
         (whether you get the file name too depends
	 on how many file names it was called with)

    -c   only show a count of matching lines

    -l   shows the names of files containing matching
         strings
         (see cheesy example in grep1.sh)

    -s   suppress error messages for non-existent or
         unreadable files

    -q   run quietly -- don't write ANYTHING to standard
         output -- BUT, exit with zero status (success)
         if any input lines are selected/matched

*   and here are a couple more -- from the MANY MANY
    on the man page...

    -v   select the lines that DON'T match

    -i   ignore the case of lines in making comparisons

bash shell programming feature of the day:
-------------------------------------------
*   the if statement!

*   basic syntax:

if <test-commands>
then
    <consequent-commands>
fi

if <test-commands>
then
    <consequent-commands>
else
    <alternate-consequents>
fi

if <test-commands>
then
   <consequent-commands>
elif <more-test-commands>
then
   <next-consequents-commands>
elif <more-test-commands>
then
   <next-consequents-commands>
else  # this is still optional
   <alternate-consequents>
fi

*   the <test-commands> list is executed,
    and if its return status is zero (success),
    then the if's <consequents-commands> are executed;
   
tests in bash...
-----------------
*   some conditionals work inside of [ ], which is the
    same as giving them as arguments to the test command

    *   the [ ] are actually considered a command,
        and so need to be surrounded by at least a blank

    here are an interesting selection of these:

    -e <filename>

    if [ -e $1 ]
    then
        cat $1
    fi

    (actually, the interesting selection is coming later)

    <string> = <string>   < true if the first <string>
                            if identical to the second

    <int> -eq <int>       < true if both ints are identical

*   there are also some commands that ONLY work within
    DOUBLE square brackets [[    ]]

    <string> =~ <regex>   true if <string> matches
                          <regex>

    ...and a few more;