Caveat Lector: These are some notes for a basic course on Unix which I co-taught many years ago at the University of Calgary. I keep them online in the hope that they may be useful to someone. However, they are not maintained. They may contain information specific to one (historic) network, which will not apply universally. Links may be broken.

[ home ]


Computer Science 001

Introduction to Unix

September 1998

Part Two




 

Shell scripting:


An Example Shell script:

#! /bin/csh -f

# must have at least one argument on the command line

if ($# < 1) then

    cat <<EOF;
    Usage:
            $0:t [argument]+

EOF
   exit

endif

# x becomes the number of command line arguments
set x =  $#

# print out info on the command line arguments

while ($x > 0)

    echo "===========  $1 ==============="
    if ( -e /home/staff/dutton/$1) then
        echo "Exists in /home/staff/dutton"
        if ( -r /home/staff/dutton/$1) then
            echo "and is readable"
        else
            echo "but is not readable"
        endif
        if ( -w /home/staff/dutton/$1) then
            echo "and is writeable"
        else
            echo "but is not writeable"
        endif
    else
        echo "Does not exist in /home/staff/dutton"
    endif

## determine the system we are on
## useful in a .cshrc file

    switch ("`uname -a`")
    case IRIX64*:
        echo "You're on and Irix 6.4 machine"
        breaksw
    case IRIX*:
        exho "Your on an Irix 5.3 machine"
        breaksw
    case SunOS*4\.1*:
        echo "You're on a SunOS machine"
        breaksw
    case SunOS*5\.*:
        echo "You're on a Solaris machine"
        breaksw
    case Linux*:
        echo "You're on a Linux machine"
        breaksw
    default:
        echo "You're not on our system"
        breaksw
    endsw
 

    @ x--
    shift        # shifts argv to the left one place
end

foreach i ( `/bin/ls `)
    if ( ! -d /home/staff/dutton/oo1/$i) then           # if it's not a d
irectory
    ## print some info on it (notice the back ticks)
    /usr/bin/file   /home/staff/dutton/oo1/${i}
    endif
end
 


some sample output...
 

dutton@coral:oo1>./cshdemo
    Usage:
            cshdemo [argument]+

dutton@coral:oo1>!!
./cshdemo one
===========  one ===============
Does not exist in /home/staff/dutton
You're on a Linux machine
/home/staff/dutton/oo1/#outline#: ASCII text
/home/staff/dutton/oo1/Daytwo: HTML document text
/home/staff/dutton/oo1/Notes: HTML document text
/home/staff/dutton/oo1/cshdemo: C shell script text
/home/staff/dutton/oo1/kernel-2.0.35-2.i386.rpm: RPM v3 bin i386 kernel-2.0.35-2
/home/staff/dutton/oo1/manpage: English text
/home/staff/dutton/oo1/outline: HTML document text
/home/staff/dutton/oo1/source.tar: GNU tar archive
/home/staff/dutton/oo1/webmin-0.54.tar.gz: gzip compressed data, deflated, original filename, last modified: Thu Jul  2 06:24:32 1998, os: Unix
/home/staff/dutton/oo1/xmeter: sparc demand paged dynamically linked executable
 
 

dutton@coral:oo1>./cshdemo .cshrc .login .logout
===========  .cshrc ===============
Exists in /home/staff/dutton
and is readable
and is writeable
You're on a Linux machine
===========  .login ===============
Does not exist in /home/staff/dutton
You're on a Linux machine
===========  .logout ===============
Does not exist in /home/staff/dutton
You're on a Linux machine
/home/staff/dutton/oo1/#outline#: ASCII text
/home/staff/dutton/oo1/Daytwo: HTML document text
/home/staff/dutton/oo1/Notes: HTML document text
/home/staff/dutton/oo1/cshdemo: C shell script text
...
 
 

A closer look at the script:

 

Emacs and Vi:

Good resource site: http://www.geek-girl.com/emacs/

vi

Some programs use vi as their default editor. To get out of vi, type :q and press enter. If you want to be a sysadmin, read the vi man page. Otherwise, you might want to
setenv EDITOR emacs

(excerpts from the) GNU Emacs Reference Card

Starting Emacs

start editor                                       emacs
edit 'myfile'                                      emacs myfile
edit 'myfile', put cursor on line 12               emacs myfile +12

Leaving Emacs

suspend Emacs (or iconify it under X)              C-z
exit Emacs permanently                             C-x C-c

Files

read a file into Emacs                             C-x C-f
save a file back to disk                           C-x C-s
insert contents of another file into this buffer   C-x i
write buffer to a specified file                   C-x C-w
In the Minibuffer (the bottom line of the screen), pressing TAB will complete the filename/command you are typing, or show you a list of possible completions.

Getting Help

You might be able to use F1 insead of C-h.
tutorial                                           C-h t
apropos: show commands matching a string           C-h a
show the function a key runs                       C-h k
describe a function                                C-h f
read "info" pages                                  C-h i
when you can't find your answer anywhere else      M-x doctor

Error Recovery

abort partially typed or executing command         C-g
undo an unwanted change                            C-x u or C-`
redraw garbaged screen                             C-l

Incremental Search

search forward                                     C-s
search backward                                    C-r
repeat last search                                 C-s C-s
repeat last search backwards                       C-r C-r
goto line XX                                       M-x goto-line

Motion

entity to move over                        backward      forward

character                                  C-b           C-f
line                                       C-p           C-n
screen                                     C-v           M-v
go to line beginning (or end)              C-a           C-e
go to buffer beginning (or end)            M-<           M->

Killing and Deleting

character delete                                   DEL or C-d
kill (to end of) line                              C-k

set mark here                                      C-@ or C-SPC
exchange point and mark                            C-x C-x

kill region                                        C-w
copy region to kill ring                           M-w
yank back last thing killed                        C-y
replace last yank with previous kill               M-y
To kill 5 lines, pass a numeric argument to the kill function:
M-5 C-h

Query Replace

interactively replace a text string                M-%
At each occurrance you will be asked if you really, really want to replace. Answer either y or n, or press ! to replace all.

Multiple Windows

delete all other windows                           C-x 1
split window in two                                C-x 2
switch cursor to another window                    C-x o

Buffers

select another buffer                              C-x b
list all buffers                                   C-x C-b
kill a buffer                                      C-x k

Keyboard Macros

start defining a keyboard macro                    C-x (
end keyboard macro definition                      C-x )
execute last-defined keyboard macro                C-x e

Modes

Emacs has a number of modes which make it handy for programming. E.g. in C mode, the tab character will indent the line as appropriate to the depth of your nested loops/if statements. To change mode, use M-x XXXX-mode where XXXX can be C, java, html, etc.

Special Characters

To enter a control character in your file (say, ^H) prepend it with C-q.

Guru Stuff

On many keybords, the backspace key returns the same sequence as C-h, which can be annoying. To change the binding of a key, (say, C-h, to be backspace,) use
M-x global-set-key, then press the key (the backspace key), and then name the function which you would like to bind to it (in our case, delete-backward-char). You may then have to run the 'help' command by typing M-x help, or you can rebind this to another key. To check what function a key is bound to, use C-h k followed by the key. A good place for key bindings is in Emacs' initialization file, .emacs. Mine contains things like:
;; The following line of code allows the BACKSPACE key to be used
;; as the DELETE key.
(define-key global-map "\C-h" 'delete-backward-char)

;; The following line defines C-c C-h to get help.
;; Normally the BACKSPACE key is used for help.
(define-key global-map "\C-c\C-h" 'help-for-help)

;; make f8 the goto-line key
(global-set-key [f8] 'goto-line)

;; show what line number you're on at the bottom of the screen
(setq line-number-mode t)
.emacs can also be used to change to fonts/colours for comments or strings in your programs. For an example see ~rf/.emacs
           Copyright c 1993 Free Software Foundation, Inc.
              designed by Stephen Gildea, May 1993 v2.0
              for GNU Emacs version 19 on Unix systems
Permission is granted to make and distribute copies of this card provided the 
copyright notice and this permission notice are preserved on all copies.
For copies of the GNU Emacs manual, write to the Free Software Foundation, 
Inc., 675 Massachusetts Ave, Cambridge MA 02139.

 
 
 

Makefiles and Compiling:

See the makefile example in the mf/ directory. The file mf.run is the output generated by some of the scripts in the file.

Common Compiling Problems:

make: *** File `life.c' has modification time in the future make: *** No rule to make target `life.h', needed by `life.o'. Stop. make: `life' is up to date. You try to run the program, but it's either not found or does something completely different. Compilation doesn't give any errors, but ld complains about unresolved symbols. ld.so complains about not being able to load yadda_yadda.so.6.3 (or something like that) The program you compiled at 1:00 am last night won't run this morning. Your program had a seg fault and now emacs says that you've exceeded your disk quota and won't save your file.
 
 

Setting up your Web Page: