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 scripting - Day 1

Admin

Texas state survey and Roll.

Goals

In this session you will:
  • learn what a shell script is
  • understand how unix treats scripted and binary executables
  • learn which shell scripting language to use and when
  • run a simple script
  • assign and access variables (parameters)
  • pass arguments to the script

what is a shell script, anyhow?

  • batch files?
  • no line terminator; relatively inflexible
  • leverages your exiting knowledge about shells; no new info
  • heavy use of variables, redirection, tempfiles and utilities
  • shell built-ins v. utilities

Use a shell script when...

  • you are going to do a series of commands more than 1-2x in a row; or,
  • you have to use an ugly one-liner frequently; or,
  • you want to do something the same way each time; or,
  • you want to ease the scheduling of the job (cron, at, batch)

Use something else when...

  • the job is bigger than is practical or fun in shell
  • re-usability and modularity are critical
  • you need to control namespace

The bottom line is use a tool that fits. Shell scripts are a good place to start on most lightweight scripting jobs.

Quotable quote: "About the only safe generalization to make is that Csh is the wrong choice for any programming task:-)." - bennett.

executables, Windows and Unix approaches

  • associations v. file types
  • file filename
  • .sh extensions are for you
  • invoking shell interpreters explicitly (shellname scriptname) or implicitly (shebang)
  • the +x bit
  • $PATH

the Usual Suspects

We are not concerned with interactive ("login") behavior of the shells here, only their scripting behavior.
name executable pronounced pro con use it...
Bourne Shell sh born shell or just plain shell Everywhere; the shell of choice for portable scripts vanilla shell, missing turbocharged features of fancier shells all the time, unless you've got a specific reason not to.
C Shell csh sea shell Syntax similar to c. Syntax similar to c. :-), seriously though, see this anti-csh rant. you're most familiar with C, and you need to knock out something quick/dirty.
Bourne Again Shell bash bash extended form of sh, although most of the extensions are for interactive stuff. not everywhere. you know bash best, and the script will only be run on the present machine. Commonly used on linux machines.
Korn Shell ksh corn shell extended form of sh, although most of the extensions are for interactive stuff. not everywhere. Not free, although free clones like pdksh do exist. you know ksh best, and the script will only be run on the present machine. Commonly used on Solaris machines.
perl perl pearl so-called "swiss army chainsaw" of shell scripting. Extremely feature rich, glomming together the greatest hits of sed, awk, grep, etc. Just-in-time compilation offers sophisticated syntactical checks, debugging, etc. Extensive unix hooks. Can be compiled. OOP. A bloated pig. Not everywhere. Often accused of ugliness. Not a shell in the normal sense. your task becomes cumbersome in a traditional shell. This class is not about perl, so we won't deal with it any further.

your first shell script

make an initials dir...
  • name
  • shebang or magic line
  • your programatic content

practicum: hello world

comment in function and your contact info. write in both csh and sh. test.

variables

Program variables are scoped within your script. They are traditionally lower or mixed case.
[_a-zA-Z][_a-zA-Z0-9]*
see all vars with set

Environmental variables are available to all scripts and shells on your login. They are traditionally UPPER CASE to provide visual (not technical) distinction.
see all ENV vars with export
sh csh
defining vars var='somevalue' set var='somevalue'
accessing vars echo ${var} (braces can be optional if separated and legal) echo ${var} (braces can be optional if separated and legal)
globalize vars to ENV export ${VAR} setenv VAR=value
reading in vars from input read var FIXME
defining arrays
accessing arrays ${var[i]}

built-in vars

sh csh
name of script $0 $0
number of args $# ${#argv}
first arg $1 ${#argv[1]}, $argv[1}
all args $* $*, $argv[*]
exitcode of last command $?
PID $$

Setting multiple vars on one line: foo="lala" bar="haha"

destroying variables

  • unset foo
  • foo=""
  • foo=

positional parameters

  • $0 command name
  • $1-9 args
command lines are space-delimited unless quotes are present

You can shift to get to positions greater than 9.

shells, children, and variable scope

  • all shells as child of login shell; stacked shells, invoked scripts
  • populating ENV by value not by reference.
  • can pass vars to subshell prior to command name.
practicum: verify how ENV parsing works.

constants

readonly foo. This attribute is not passed to subshells, and some ENVs cannot be made readonly. Can't revert.

Homework

  • math, pp. 132-33
  • flow control, pp. 149, 169-71
  • test, pp. 153-55


http://www.mousetrap.net/syllabus/shell/day1.html
$Id: day1.orb,v 1.12 2002/09/10 01:59:16 mouse Exp $

Remember, your login is based on your machine's hostname, not on any other number.
~/[initials] refers to the subdirectory under your homedir, named after your initials. Everything except for .dotfiles will be stored in your ~/[initials] directory.


© 1995-2001 jason carr
Distributed under the terms of the GNU Free Documentation License.