cs534 Operating Systems - The Bourne Again Shell (BASH)

Background

Shell Basics

There are three kinds of shells:

Running a startup file in the Current Shell

File Descriptors and Redirection

When a new process is spawned, three file descriptors are also created:
NameSymbolDefault Device
Standard In 0< Keyboard
Standard Out 1> Monitor
Standard Error 2> Monitor

Send Standard Out and Standard Error to different files
Assume that File2 does NOT exist

$ cat File2 File1 > holder.txt 2> error.txt

What Happened?

cat produced an error message that was redirected to
the file error.txt. The contents of File1 were redirected to
the file holder.txt.

$ cat holder.txt
This is File1
$ cat error.txt
cat: File2: No such file or directory

Send Standard Out and Standard Error to the same file

NOTE: The construct 2>&1 means redirect Standard Error to the
same place as Standard out...or,
Standard Error becomes a duplicate of Standard Out

$ cat File2 File1 > holder.txt 2>&1
$ cat holder.txt
cat: File2: No such file or directory
This is File1

NOTE: Order of the arguments is important.

$ cat File2 File1 2>&1 > holder.txt Problem:
Standard Error is redirected to Standard Out
BEFORE Standard Out is redirected to holder.txt

That means that Standard Out is still defaulting to
the monitor at the time Standard Error is redirected
to Standard Out, Standard Error goes to the monitor.
Standard Out goes to the file holder.txt

Now we can send both Standard Out and Standard Error through a pipe

$ cat File2 File1 2>&1 | tr "[a-z]" "[A-Z]"
CAT: FILE2: NO SUCH FILE OR DIRECTORY
THIS IS FILE 1

What Happened?

Since Standard Error is made a duplicate of Standard Out,
everything goes through the pipe and gets translated.

Shell Scripts

Example sh-bang statement

#!/bin/bash

Here is a small script

#!/bin/bash
echo "The current time and date is " `date` "
echo "These are the users that are currently on the system: "
who

Here's the output


rgrogan@analog24: $ source whoson.bash
The current time and date is  Wed Oct 17 19:33:49 PDT 2007
These are the users currently logged on to the system
cs53412  pts/0        2007-10-17 17:54 (207.233.45.19)
cs53419  pts/1        2007-10-17 19:16 (207.233.45.19)
cs53415  pts/2        2007-10-17 17:45 (207.233.45.19)
cs53432  pts/3        2007-10-17 19:18 (207.233.45.19)
cs53409  pts/5        2007-10-17 19:29 (207.233.45.19)
cs53405  pts/6        2007-10-17 19:14 (207.233.45.19)
rgrogan  pts/7        2007-10-17 19:29 (207.233.45.19)
rgrogan@analog24: $

Invoking a Shell Script

Separating and Grouping Commands

ShellCommandGrouping.pdf

Parameters and Variables

Some Considerations

The export Builtin

Example without the use of export

here are the scripts

bash$ cat extest1 // a little test script
cheese=american
echo "extest1 1: $cheese" // echo contents of cheese 1st time
subtest // execute script named subtest
echo "extest1 2: $cheese" // echo contents of cheese 2nd time

bash$ cat subtest // contents of script subtest
echo "subtest 1: $cheese" // subtest can’t see contents of cheese from //above
cheese=swiss // assign a value to cheese in subtest
echo "subtest 2: $cheese" // echo contents of cheese 2nd time

here's the run...no export

bash$ extest1 // run the script
extest1 1: american
subtest 1: // american is LOCAL to extest1
subtest 2: swiss // swiss is LOCAL to subtest
extest1 2: american // swiss is not global…american is LOCAL

Example using export

here are the scripts

bash$ cat extest2
export cheese // value of cheese is now GLOBAL
cheese=american
echo "extest2 1: $cheese"
subtest // run script called subtest
echo "extest2 2: $cheese"

bash$ cat subtest
echo "subtest 1: $cheese" // use GLOBAL value of cheese
cheese=swiss // assign a LOCAL value to cheese
echo "subtest 2: $cheese" // display LOCAL value of cheese

here's the run...using export

bash$ extest2
extest2 1: american // global
subtest 1: american // global
subtest 2: swiss // local
extest2 2: american // global

Example using export in combination with a variable declaration

here are the scripts

bash$ cat extest3
export cheese=american // global status and assignment on same line
echo "extest3 1: $cheese"
subtest
echo "extest3 2: $cheese"

bash$ cat subtest
echo "subtest 1: $cheese"
cheese=swiss
echo "subtest 2: $cheese"

here's the run...using export in combination with a variable declaration

bash$ extest3
extest3 1: american
subtest 1: american
subtest 2: swiss
extest3 2: american

Pathname Expansion

Removing a variable with unset

declare and typeset assign attributes to variables

the PATH environment keyword

The Command Line Prompts

Process Structure

Processes

Various Summaries

Single/Double Quoting in Aliases

The Order of Command Line Expansion

  1. Brace expansion
    Example:

    $ echo List_{AA, BB, CC}
    List_AA List_BB List_CC

  2. Tilde Expansion...remember the ~ stands for your home directory...see page 326
  3. Parameter and Variable Expansion - $myVar will be expanded
    ...quoting type comes into play here
  4. Arithmetic Expansion - $((expression))...will be discussed later
  5. Command Substition - $(command)...echo "The date is $(date)"
  6. Word Splitting: will discuss this later
  7. Pathname Expansion -
    Example:

    $ ls
    tmp1 tmp2 tmp3
    $ echo tmp*
    tmp1 tmp2 tmp3
    $ rm tmp*
    $ echo tmp*
    tmp*
    $