CS 279 - Week 10 Lecture 1 - 2022-10-24

TODAY WE WILL
*   announcements/reminders
*   a couple more Linux commands:
    sort and tee
*   intro to Bash arrays
*   if time: intro to BASH_REMATCH array (holds matches
    to regular expression subexpressions)
*   prep for next class

=====
another UNIX/Linux command: sort
=====
*   yes, it does what you think

*   a default alphameric sort, that you can modify based
    on its options

*   for example:

    cat stuff.txt | sort

=====
tee
=====
*   provides a way to capture the contents of a pipe
    in a file without disrupting the flow of information
    through that pipe

*   tee desired_file 

    ...causes standard input to be copied to
       standard output AND to the file (or files!) specified

    ls                  # see files on-screen
    ls | tee looky.txt  # see files on-screen AND put in looky.txt

    ...divert a copy of intermediate stuff? Yes:

    ls | tee looky.txt | wc -l

    *   looky.txt gets the plain ls output,
        wc -l also gets it and can count its lines

    *   this does work, also:

        ls | tee looky.txt | wc -l > num_files.txt

    *   just two options?!

        -a - append the output to each file instead of overwriting
	     the file

        -i - ignore interrupts (!)

=====
intro to Bash arrays
=====
*   source: a Linux Journal article:
    "Bash Arrays" - Mitch Frazier - June 2008

*   Bash arrays
    *   have numbered indexes only

    *   but! they are sparse, e.g. you don't have to define
        all the indices (!!)

*   An entire array can be assigned by enclosing the
    array items in parentheses, separated by blanks/whitespace

    arr=(Hello World)  # that's a two element array

*   individual items can be ASSIGNED using the familiar
    common array syntax:

    arr[0]=Bonjour

    *   BUT! you can simply add an element at an index
        (and it doesn't have to be contiguous...!)

    arr[8]=Linux

*   to refer TO an array element,
    you use: 	${array_name[index]}

    echo ${arr[8]}	# ought to echo Linux

    "the [curly] braces are required to avoid conflicts with
    pathname expansion" <-- file globbing!

*   here are additional possiblities here:

    ${arrayname[*]}    # all of the items in the array

    ${!arrayname[*]}   # all of the indexes in the array

    ${#arrayname[*]}   # number of elements in the array

    ${#arrayname[desired_index]}   # length of item with index desired_index

*   you CAN initialize a sparse array,
    using [desired_index]=desired_val IN the parentheses:

    arr2=(moo a b [10]=oink [15]=moo)

*   if you use @ instead of * and quote the expression,

    for element in "${arr[@]}"

    ...WILL maintain elements with blanks, etc. as single elements