CS 279 - Week 3 Lecture 2 - 2022-09-07

TODAY WE WILL
*   announcements
*   intro to file permissions
*   a LITTLE more on ls
*   intro to the chmod command
*   shell local variables?
    local shell variables?
*   [did not get to] using backquotes in the shell
*   prep for next class

*   some more reading:
    *   Paul Cobbaut 2021 revision course text:
        Section II - Chapter 13 - Sections 13.1 - 13.5

=====
intro to file permissions!

*    three levels:
     permissions for the:
     *   owner - typically the creator of the file
     *   group - a specified group of users
     *   "world" - everyone who has an account
                   on that UNIX/Linux system

     *   for each of those levels,
         for each file,
	 one can specify if, for those in that level,
	     that file is:
	     r - readable
	     w - writable
	     x - executable

         *   common way of expressing in UNIX/Linux:

             *   always in the order read then write then
	         execute

             *   put the letter if that file HAS
	         that permission,
		 put a dash if a file does NOT

             *   rwx -> has read AND write AND execute
	         --- -> has NO permissions! no read,
		        no write, no execute
		 rw- -> has read and write but not
		        execute
		 r-x -> has read and execute but not
		        write

             *   AND you give these for the owner
	         level, then the group level,
		 then the world level

                 rwxr-xr-- -> owner has read, write
		     execute, group has jusr read and
		     execute, world has just read

     *   consider plain text files!
         *   permissions r and w mean exactly
	     you think -- can someone in that
	     group read this file, write this file

         *   a plain text file (such as a bash shell
	     script) needs to have permission x
	     to be typed as a command to be run

    *   it's a little weirder for directory files!
        *   r, for a directory, lets you find out
	    WHAT is in the directory,
	    BUT it is INSUFFICIENT for ACCESSING
	    the files whose names appear in it

            *   (you cannot, for example, use cat
	        to view the contents of a file
		that's in a directory you only have
		r access to)

        *   w, for a directory, is required to
	    ADD files to it or DELETE files from it

            BUT! w permission is NOT required to
	    MODIFY a file listed in the directory
	    or to delete its *contents* (subject to
	    the *file's* permissions...)

        *   x, for a directory, lets you operate
	    on the names in the directory IF you
	    know them, but does NOT let you find
	    out what they are if you don't

        *   r-x turns out to be pretty common for
	    a (publicly-accessible) directory

            --x can be weird, but occasionally
	    useful, for a directory;

    *   some additional important permissions
        info:
	*   the permissions a directory has
	    can (to some degree) "override"
	    those of the files within it

            for example, if a directory does
	    not have world read and execute,
	    the world will not be able to
	    "reach" the files within, even
	    if they are world-readable

=====
two more ls options: -l and -d

*   option l is for "long listing"

    ls -l ...

    gives more information, including file
    permissions

*   option d is for a directory listing...?
    from the man page:

    "Directories are listed as plain files
    (not searched recursively)."

*   (for options that don't themselves have
    arguments, you can combine them with
    a single dash --

    ls -ld  -> both of these work for a
    ls -dl     non-recursive long listing of
               a directory
	       
    ls -al  -> both of these work for a long
    ls -la     listing of invisible files

=====
intro to the chmod command

*   SEVERAL approaches to this!
    ...see course text, 2021 revision,
       Sections 33.6 and 33.7!

*   "old school chmod" - you specify the permissions
    for all of the levels using an octal code -- with
    an octal digit representing the binary representation
    for each level

    r, then w, then x
    r-x

    WHAT IF you expressed this as
    1 if the file has that permission for that level,
    and 0 if it does not?

    rwx -> 111
    r-x -> 101
    r-- -> 100

    What if you then expressed that binary
    value in base 8?

    111 is 7,
    101 is 5,
    etc.

    And if you write that octal value for
    the owner, then the group, then the world,
    THAT can be the 1st argument to chmod

    (and the 2nd argument is what file(s)
    are getting those permissions)

*   for example:

    chmod 700 just-for-me.sh

    ...111 000 000
    ...rwx --- ---

    chmod 600 mine-only.txt

    ...110 000 000
    ...rw- --- ---

    chmod 711 public_html
    
    ...111 001 001
    ...rwx --x --x

    chmod 644 all-can-see.txt

    ...110 100 100
    ...rw- r-- r--

    chmod 755 all-can-see-dir

    ...111 101 101
    ...rwx r-x r-x

=====
intro to shell local variables!

*   see 2021 revision of course text,
    Section II, Chapter 13, sections 13.1 - 13.5

*   you CAN declare a local variable by just
    setting a name to a value, or optionally
    you can put declare in front of it

    BUT!!!! you can NOT have ANY blanks
    to the immediate LEFT or RIGHT of the
    assignment statement, = !!!!!!

    $> declare cow=moo
    $> animal=cow

    *   when you USE the local variable,
        you PRECEDE its name with a $

        (but if you RESET it, don't use a $
	in that assignment statement...!)

    $> echo $cow
    moo
    $> echo $animal
    cow

    $> animal=kitty
    $> echo $animal
    kitty

    # various bugs demo'd!

    $> echo cow
    cow
    $> echo $cow
    moo
    
    $> $cow=holstein
    -bash: moo=holstein: command not found
    
    $> declare oopsie = 3
    -bash: declare: `=': not a valid identifier
    -bash: declare: `3': not a valid identifier

    $> declare oopsie= 3
    -bash: declare: `3': not a valid identifier

    $> declare oopsie =3
    -bash: declare: `=3': not a valid identifier

    $> declare oopsie=3
    $> echo $oopsie
    3

*   what's the scope of a local variable?

    *   created interactively?
        I *think* it is the lifetime of that particular
	shell process it was created within

    *   created within a bash script?
        I *think* it is from where it is declared
	until the end of that script

*   see little shell script make-local-looky,
    which uses a local variable to help to make
    a local directory named looky within its present
    working directory when it is called