squeak!
Syllabus Homepage
Course Overview
Course resources
Day 1
Day 2
Day 3
Day 4
Common errors
Internet Glossary
About Your Instructor
Credits: This site powered by the vi text editor, apache webserver, perl scripting, and Debian linux.
squeak!

shell - Day 4

Goals

In this session you will:
  • pick up some shell odds/ends
  • use advanced shells: bash ksh csh
  • pick up some useful revision control utils
  • build an editing wrapper
  • back up your files to take with you

POST

Overview: sh-lib.sh will only contain the pdebug function. day4post.sh will use getopts to set the "debug" flag.
  1. log into your solaris workstation
  2. make a file called sh-lib.sh, which will be your main library file to hold definitions, functions, etc.
  3. build a function in your library, and call it pdebug (ie, "print debugging info"). This function will:
    • check for the presence of a variable called "debug" that can contain "1" or some other value. For this exercise, a value of 1 means to turn on debugging. All other values mean turn it off.
    • if debugging is off, abort the function
    • if debugging is on, check for the existence of an argument passed to this function. If no argument was passed (ie, no debugging output), then print an error saying so and abort the function.
    • if debugging is on and an argument was passed, print some kind of visual marker (*** or something you can see easily) and the contents of the passed argument to STDOUT (or STDERR if you're clever).
  4. build a script called day4post.sh. This file should:
    • be run like day4post.sh [-d] number
    • source (include) the library file above
    • use getopts to see whether or not a debug switch was passed. If it is passed, then set your "debug" variable to 1.
    • make liberal use of the pdebug () function to print messages that will be helpful during development
    • count from -5 to the number specified on the command line
  5. run day4post.sh with and without the debug switch

odds/ends

  • line continuation
  • command; stacking
  • here documents
    
    cat <<eot
    This is a here document.
    it keeps you from having to 
    echo out everything.
    eot
    
  • ( sub; shells)
  • { command; grouping }

advanced shells

  • setting vars:
    csh: set foo='bar'; setenv FOO; set FOO='bar'
    ksh/bash: same as sh, only can export foo='bar' in one step.

  • doing math:
    csh: @ var = $var2 + 3; echo $var
    ksh/bash: let num=$num+100 or sum=$((4+5)) Some differences will exist

  • increment/decrement:
    csh: @ var++; echo $var
    ksh: let var++ (only in some newer versions of these shells)
    bash: let var++

  • function declaration:
    unavailable in csh.
    ksh/bash: function myfunc { foo; bar }
    ksh: functions can be exported: typeset -fx function
    ksh: functions can be listed: typeset -f
    ksh: functions can be autoloaded from FPATH. set -o autoload. Forward declarations with typeset -f funcname
    ksh: functions can be unset: unset -f funcname

  • variable localization:
    unavailable in csh.
    ksh: typeset var; var='foo' (inside function)
    bash: local var; var='foo' (inside function only)

  • arrays: note that ${brackets} are highly recommended and frequently mandatory with arrays
    csh: set myarray=(some stuff here); echo ${myarray[1]}. base 1!
    ksh: set -A myarray some stuff here; echo ${myarray[1]} base 0!
    bash: declare myarray; myarray=(some stuff here); echo ${myarray[1]} base 0!

revision/spelling resources

  • spelling ispell filename
  • checking out from RCS co -l filename
  • ci -u filename (RCS dir or file,v)
  • rcslog filename
  • $Id: day4.orb,v 1.17 2002/11/27 02:14:31 mouse Exp $ (remember to comment out)

final practicum

Using whichever shell you prefer, build an editor wrapper that:
  • has a simple name
  • checks your file out of RCS
  • edits the file using the editor specified in EDITOR
  • spellchecks your file
  • checks the file into RCS
If you prefer, you can backup to a dated filename instead of using RCS commands.

restricted shells

Generally speaking this is how restricted shells work:
  • cannot cd
  • cannot use a slash (/) in a path
  • cannot alter critical ENV vars (PATH, SHELL, etc)
  • full security requires a jail, jailpath, etc.

Conclusion

Where to go from here


http://www.mousetrap.net/syllabus/shell/day4.html
$Id: day4.orb,v 1.17 2002/11/27 02:14:31 mouse Exp $


© 1994-2002 jason carr.
distributed under the terms of the GNU Free Documentation License.

jason carr

Reminders

  • Classroom temperature can be wildly variable. Dress lightly and bring layers.
  • your username is based on the class title and the last two digits of your workstation's hostname.
  • remember to take your work with you.