For those of you who may need an account, I want to stress that this account does not give you unlimited access nor storage on the Department's computers nor will it continue past this semester unless you are also taking courses which are part of the Computer Science major beyond CS102.
For those of you who already have accounts, please login so we can see if there are any problems.
A few of questions for all of you. If the answer to any of these is 'NO', raise your hand. 1. Do you know basic UNIX file system? 2. Do you understand file permissions and how to change them? (chmod) 3. Do you know how to use some email program under UNIX? (pine,mh,mailx) 4. Do you know how to use some editor under UNIX? (vi,pico,vim,gvim)
man is invoked by typing the word 'man' at the Unix prompt followed by the name of a command, utility, function or word you hope might be there. If there is a manual page available for that item in the users MANPATH, the manual page is displayed using the users default PAGER (eg. more). We will discuss MANPATH and PAGER later.
Most pages consist of a header which indicates which section of the manual you are in currently, the name of the man-page, a synopsis of the command/utility/function usage and then a description. 'man man' should get you something that looks like this
User Commands man(1)
NAME
man - find and display reference manual pages
SYNOPSIS
man [ - ] [ -adFlrt ] [ -M path ] [ -T macro-package ]
[ -s section ] name ...
man [ -M path ] -k keyword ...
man [ -M path ] -f file ...
DESCRIPTION
There are several different command line options to man. For a listing of all the manual pages for the "item" use 'man -l item' For all the man-pages for a specific name use 'man -a item'. If you don't know the name but think it has to do with a specific topic you can try 'man -k topic'. This is the "keyword" search and may just give you a screen full of useless info but... A reason for using the '-l' option is so that you can chose which page to look at if there are multiple pages for an item.
Lets try the following man man to read the manual page for the command man. man -a print this may return anywhere from 1 to 4 or perhaps more pages. if it returns more than one, you need to figure how to use your pager to display them all. For more just continue to page down. If any of you are using less, :n goes to the next file/man-page. man -l read You get several listings. You might note that some are from /usr/man and some from /usr/local/man. If you look at pages from the corresponding sections you will find that they are the same. man -S2 read This gets you the manual page on the C programming language command read(), which is man page section 2. Many, but by no means all, of the C commands are in section 2. And if you don't specify this section, or -a you will probably get something other than what you want. Try man read.
This is all I am going to say about man. Other than to warn you that a common response to a request for more in-depth information will be "read the man-page".
So what exactly is the shell?
The UNIX Shell is a UNIX program which gives users access to operating system features for launching programs and managing files. The shell can be used interactively or non-interactively from lists of shell commands stored in files called shell scripts. Some shells provide a set of menus (rather than a command line) from which a user can make selections. We are NOT going to be using that type of shell. Commands entered by the user are passed by the shell to the operating system which executes them. The results are then passed back by the shell and displayed for the user. A shell can be any "easy-to-use" user interface.
I said earlier that I would talk about MANPATH and PAGER and this seems like a reasonable time to do it. How many of you are using csh? How many are using some other shell? How many don't know? Well, for those who have not explicitly requested to have your shell changed, in this department you are probably using csh. If you would like to be sure try
UNIX> echo $SHELLIf you want to know more about echo, try the man pages. The reason I ask is to try to tailor this next bit if necessary. When you first login the process that handles login does some things besides checking your password and user name. It also finds out which shell you use and starts up a copy of that shell using some default files. Part of this operation sets some environment variables. These variables are used by certain programs to keep track of information and determine how to act in some situations. There are many of these and all shells don't use the same variables but there is a subset that you can pretty much depend on. Here are some of the most common:
These can be checked by typing
UNIX> echo $VARIABLE_NAMEat the prompt. These are also defined in the dot file .cshrc. When you log in the operating system opens the .cshrc and(or) .profile file and reads your default settings. You can view your default settings by typing
UNIX> more .cshrc (or) UNIX> more .profileFor some shells, any or all of these may be lower case. If some program doesn't run properly but you can't see why, check the man-page for that program and see what if any environment variables are used. If it still does not make sense, ask me if it has to do with class or mail "help".
Bourne C TC Korn BASH
________________________________________________________
command history No Yes Yes Yes Yes
command alias No Yes Yes Yes Yes
shell scripts Yes Yes Yes Yes Yes
filename completion No Yes Yes Yes Yes
command line editing No No Yes Yes Yes
job control No Yes Yes Yes Yes
________________________________________________________
The rest of this lecture will cover basic mechanics of using the Bourne shell (/bin/sh). Even though most people use csh as the interactive interpreter to execute programs, many find the Bourne shell to be much simpler for writing scripts. The general opinion is that equivalent csh scripts often cannot be produced without a lot of pain and agony.
The man-page for the Bourne shell (``man sh'')is excellent. After these two lectures , you should be able to read it without problems, in order to learn things not covered. Please create a working directory and copy into it all the files in /home/cs300/Sh.
Example: UNIX> cd ~ UNIX> pwd /home/myname UNIX> mkdir cs300 UNIX> cd cs300 UNIX> mkdir Sh UNIX> cp /home/cs300/Sh/* Sh UNIX> cd Sh UNIX> ls
UNIX> ls -l
UNIX> chmod 0750 {filename} or
UNIX> chmod +x {filename}
This will set the executable bit on your program.
After the first line, you may execute programs pretty much like in the
csh. For example, the lshome program prints your home directory, and then
lists its contents. Try it out:
CS300> lshome /lymon/homes/cs300 AdobeFnt.lst Jgraph Python attend mail Awk Mail Sed bin www-home Cat Make Sh cs300.tar.gz Expect Misc Tcl grades Gcc Perl archive junk CS300>
You can also run the shell script by typing ``sh lshome'' (and then you don't need the ``#!/bin/sh'' line). Doing ``sh -x script'' can help you debug a shell script. The -x switch tells the shell to print each command as it is executed so that you can see where the script fails. Try that with lshome.
CS300> sh -x lshome + cd + pwd /homes/cs300 + ls AdobeFnt.lst Jgraph Python attend mail Awk Mail Sed bin www-home Cat Make Sh cs300.tar.gz Expect Misc Tcl grades Gcc Perl archive junkA last point on running the Bourne shell is that you can run it interactively by simply typing sh at a command prompt. This means that you can check out some of these things we are talking about without having to edit/create script files.
A little background on this next subject might help. In the C programming language, which some of you may be familiar with, there are numbers assigned to the streams stdin, stdout and stderr ; 0, 1 and 2 respectively. UNIX, and most of the shells that run on UNIX, are written in C. So there is logic behind the following.
If you have been using UNIX for any time indirection and pipes are probably second nature to you by now. If not here are the basic indirection cases:
$ cat < f1 $ cat f1 f3 > f2 $ cat f1 f3 2> f2 $ cat f1 f3 2>&1 > f2 $ cat f1 f3 > f2 2>&1 $ cat f1 f3 >&2 2> f2 $ cat f1 f3 2> f2 >&2 $ cat f1 f3 2>&1 >f2 | cat > f5Make sure you understand the output of each of these (when you test it, make sure you're running sh and not csh).
Pipes "pipe" standard output of one command into standard input of another. Again, I assume that this is something you already know. For example, a simple way of printing the 5th line of the file f is to do the following:
UNIX> head -n5 f | tail -n1
(Yes, there are better ways of doing that).
We will take up here next week. I'll finish the Bourne shell and give the first lab assignment.