CS 140 Handbook

 

 

An Accompaniment for

CS 140

Introduction to Computer Science

 

 

 

Computer Science Department

California State Polytechnic University, Pomona

September, 1998


 

Preface

This handbook is intended for students in CS 140, Introduction to Programming and Problem Solving, although others may find it useful as well.  It is organized such that Chapters 2 through 5 serve as a guide to laboratory exercises that introduce the use of the Sun SPARC workstation computers, the Unix operating system, and a variety of software capabilities that will be important tools for the computer science student.

 

This edition is a major revision of the January, 1996 edition, reflecting the change to Java as the introductory programming language and the incorporation of the Computer Science laboratory into the intranet.  Handbook editions earlier than this one were based on the use of the Ada programming language.

 

We are very interested in obtaining feedback from students regarding the Handbook’s usefulness, the clarity of its presentation, and the scope of its coverage.  Please feel free to make comments about the Handbook to your instructors.  If you prefer, you may convey written suggestions (anonymously if you so choose) through the department office.

 

 

 

Computer Science Department

California State Polytechnic University, Pomona

September, 1998


 

If you find errors, please email H. N. Riley


Table of Contents

Corrigenda Read First!

 

 

              Chapter

 

                          1.       The Study of Computer Science                                        1

 

                          2.       Campus Computing Facilities                                            6

 

                          3.       Files and Directories under Unix                                     12

 

                          4.       Compiling and Running Java Programs               24

 

                          5.       Programming Style                                                         27

 

 

              Appendix

 

 

                          A.      Disk Storage                                                                 35

 

                          B.       Sub-Directories                                                             36

 

                          C.      Bibliography:  Java and Unix                                         38

 

 

 

 

 

 

CHAPTER 1

Chapter 1

The Study of Computer Science

Why Computers Are So Important to Us

The first electronic computer is generally considered to have been the ENIAC, built at the University of Pennsylvania in 1946.  Over the next several years, numerous electronic computers of increasing capability were constructed at universities and government research facilities.  Most of these computers were one of a kind.  The first commercially manufactured computer appeared in 1952, the original Univac.

 

By 1962, there were approximately 10,000 electronic computers in use.  In 1972, the number had grown to 100,000.  In addition, there were several thousand of the "minicomputers" that had been introduced in the middle 1960s.  These numbers were reported in the Computer Yearbook for 1972 which further predicted that by 1980 there would be 355,000 computers.

 

Of course, that prediction did not foretell the advent of the microprocessor and the personal computer (PC).  The January 1983 issue of Time Magazine featured the personal computer as the "Man of the Year" and reported that in 1982 alone, over 100 companies had produced a total of nearly 3,000,000 personal computers.  An article in the September 1987 issue of Unix/World quoted Steve Jobs (co-inventor of the Apple Computer) as saying there were 10 million PCs in use.  PC Magazine predicted in December 1986 that there would be 35 million PCs by 1990.  In January 1990, it reported that there were 50 million.  Nearly that many new computers were shipped during the first half of 1998.

 

So we approach the 21st century "surrounded" by hundreds of millions of computers, and “connected” to the world through the Internet.  Every aspect of our lives is touched by computers.  How many people have a computer at home, have another that they use on the job, and encounter still others every time they buy something?  (Note also that these numbers don't count tens of millions more microprocessors that inhabit our cars, VCRs, cameras, etc.)  Virtually every business or other organization of any size depends on a computer to some extent.  A company such as Federal Express is as much built around its computers as it is around its airplanes.  Increasingly, the Internet is a force in commerce as well as in research and in entertainment.  The number of computers connected to the Internet has doubled each year for the past several years.

 

The technological advances that have been made in the development of computers are astounding.  Achievable computing speeds have increased by at least a factor of 10,000 since the first computers.  Costs have come down dramatically.  We can buy computing power today for a few dollars that far exceeds that of the early million dollar computers.  If the same kinds of advances had been achieved in the production of automobiles or in airline service, transportation today would be instantaneous and free.

 

What is Computer Science?

In the late 1980s, a task force of the Association for Computing Machinery (ACM) made a detailed analysis of the field of computer science.  They defined a somewhat broader discipline of "computing" since computer science has so much in common with computer engineering.  They provided this definition of computing:

 

The discipline of computing is the systematic study of algorithmic processes that describe and transform information:  their theory, analysis, design, efficiency, implementation, and application.  The fundamental question underlying all of computing is, "What can be (efficiently) automated?"

 

This means that studying computer science includes studying

 

1) How computers "work."  The computer scientist understands how computers do what they do.

 

2) What kinds of things computers do.  Computer science includes an appreciation for the uses that are being made of computers in science, industry, government, and in enhancing our personal lives.

 

3) What kinds of things computers CAN (and CAN'T) do.  Computer scientists must have an appreciation of what our expectations for computers can be.  What are the things that are easy for computers?  What things are difficult?  Impossible?

 

4) How computers are used effectively.  Effective use means getting the job done—doing the right thing and at the right time.  It means understanding and defining requirements for the work to be accomplished, deadlines to be met, accuracy to be achieved.

 

5) How computers are used efficiently.  Efficient use means getting your money's worth and not wasting resources.  Efficient computing produces effective results for the least cost.

 

6) What fundamental principles apply to all of the above.  Computer science is a science, and there are underlying concepts that must be learned and appreciated.

 

As in other sciences, computer science includes experimentation.  The computer scientist is always seeking to find or evaluate new uses for computers, new ways to apply them, and new principles to guide further research and study.

Some Definitions

Some simple definitions may help.

 

Computer:  A device which can perform computation and other processing of data.

 

Computation:  Arithmetic or logical operations.  For example

 

Arithmetic:  2 + 3,                        X * Y,                        SALARY - TAXES

 

Logical:  IS X = Y?                        IS SALARY > $20,000?

 

Data:  Facts.  For example, facts about

 

A person:  name, address, height, age, hair color

Weather:  temperature, humidity, wind velocity

 

Data processing:  Manipulation and use of facts.  For example

 

Computation:  As above

 

Searching:  Finding a particular name, finding the highest

                  temperature

 

Arranging:  Sorting into alphabetical order,

                  putting last name first

 

Formatting:  Putting in commas, periods, dollar signs.

 

Computer system:  A computer and its associated devices and programs (hardware and software).

 

Algorithm:  A set of clear, precise steps or rules for carrying out a task.

 

Program:  An ordered set of instructions to perform a computation or data processing task.  An algorithm in a form to be used by a computer.

 

Instruction:  That which is communicated to a computer to cause it to perform one or more fundamental operations.

 

Hardware:  The physical parts of a computer system.  Circuits, cables, keyboards, screens, printers.

 

Software:  Programs (and their documentation).

Computer Science Topics

The ACM task force also identified nine categories into which the study of computer science can be divided.  The nine subject areas as described in the ACM's "Computing Curricula 1991" are:

 

 

Algorithms and Data Structures

This area deals with specific classes of problems and their efficient solutions.  The performance characteristics of algorithms and the organization of data relative to different access requirements are major components.

 

Architecture

Methods of organizing efficient, reliable computing systems provide a central focus of this area.  It includes implementation of processors, memory, communications, and software interfaces, as well as the design and control of large computational systems that are reliable.

 

Artificial Intelligence and Robotics

The basic models of behavior and the building of (virtual or actual) machines to simulate animal and human behavior are included here.  Inference, deduction, pattern recognition, and knowledge representation are major components.

 

Database and Information Retrieval

The area is concerned with the organization of information and algorithms for the efficient access and update of stored information.  The modeling of data relationships, security and protection of information in a shared environment, and the characteristics of external storage devices are included in this area.

 

Human-Computer Communication

The efficient transfer of information between humans and machines is the central focus of this area.  Graphics, human factors that affect efficient interaction, and the organization and display of information for effective utilization by humans are included.

 

Numerical and Symbolic Computation

General methods for efficiently and accurately using computers to solve equations from mathematical models are central to this area.  The effectiveness and efficiency of various approaches to the solution of equations, and the development of high-quality mathematical software packages are important components.

 

Operating Systems

This area deals with control mechanisms that allow multiple resources to be efficiently coordinated during the execution of programs.  Included are appropriate services of user request, effective strategies for resource control, and effective organization to support distributed computation.

 

Programming Languages

The fundamental questions addressed by this area involve notations for defining virtual machines that execute algorithms, the efficient translation from high-level languages to machine codes, and the various extension mechanisms that can be provided in programming languages.

 

Software Methodology and Engineering

The major focus of this area is the specification, design, and production of large software systems.  Principles of programming and software development, verification and validation of software, and the specification and production of software systems that are safe, secure, reliable and dependable are of  special interest.

CS 140

CS 140 is the beginning course for Computer Science majors at Cal Poly.  It is the first course in an "introductory sequence" of courses by which the CS student is introduced to the field.

 

This course is designed to give the student an appreciation of what computer science is.  It provides an overview of computer hardware and software, a look at how computer systems are formed and how and why they are used.  The student is introduced to the computing facilities at Cal Poly and how to make use of them.  About half of the course is devoted to beginning the study of problem analysis, designing algorithms, and writing computer programs.

Programming

We begin in computer science by studying programming and problem solving.  Programming is the process of translating problem solutions into a form that can be used to direct the operation of a computer.  It is used in all nine of the subject areas above.  To the computer scientist, learning programming is not an end in itself, but rather a tool to be used in the exploration of the field.

 

Before problem solutions can be communicated to a computer, the solutions themselves must be defined.  Everywhere we look, there are problems to be solved—in science, in business, even in our everyday lives.  Many of these problems have solutions that have been known for a long time.  A problem such as finding the greatest common divisor of two integers was solved centuries ago, and a clear procedure called an algorithm was created for finding the answer.  But we are continually faced with new problems to be solved, and a part of the computer scientist's role is to understand the process for designing these solutions, these algorithms.

Why Java?

At Cal Poly, we begin with the study of the Java programming language.  Java has been chosen because of the strengths it provides in examining each of these nine subject areas.

 

Java is one of the newest programming languages, having only been introduced in 1995.  It has gained popularity rapidly, largely because of the large amount of supporting software available to make the writing of programs easier.  This software is referred to as the Java Development Platform or Java Application Programming Interface (API).  This support is especially strong in the area of Graphical User Interfaces (GUIs), and is the reason that Java is being so widely used in the development of “web” based applications.

 

Java is object oriented and is so to a greater degree than some other “OO” languages.  Object orientation facilitates programming in a number of ways.  The objects that make up computer programs readily relate to the real world objects that the program must model in order to solve problems.  This reduces the “semantic gap” between the way we think and the way a computer must be instructed to perform its work.  Objects are easier to “reuse” in other programs and this makes writing new programs less costly. Objects also aid in the creation of very large programs by teams of people.  As computer applications get more and more complex, such collaboration becomes essential.

 

Java originated with Sun Microsystems.  They have described it as follows:

 

Java:  A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, and dynamic language.

 

We can hope that CS 140 will provide the basis for understanding what all those “buzzwords” mean.

 

Chapter 2

Campus Computing Facilities

Computers on the Cal Poly Campus

There are probably two thousand computers on the campus, which are used by faculty, staff, and students.  Some of these computers are centrally administered and are available for use by everyone at Cal Poly.  Other computers are provided in a number of computer laboratories maintained by individual colleges or departments.

 

Most of the computers on campus are connected to each other through a fiber optic “backbone” (FOB), a cable that provides for many pairs of computers to communicate with each other simultaneously.  This cable connects many of the buildings on campus.  The backbone is in turn connected to the Internet so that all of these computers can communicate with other computers on the Internet anywhere in the world.

 

The central computer facility is actually a "cluster" of DEC Alpha computers that act as a single system and are shared by many users at once.  Such computers are called "timesharing" systems because their computing time can be shared by multiple computer programs that take turns making use of the computer's capabilities.  The Alpha cluster can run in excess of one hundred programs at the same time, thus serving over one hundred users simultaneously.  The Alpha cluster is located in the CLA Building (Bldg. 98).  All students are given accounts on this computer (VMS mail accounts) when they register.

 

A major “subnetwork” of computers is the “intranet.”  Support for computers connected to the intranet is provided by some dedicated computers that provide file storage and other services.  These services greatly extend the capability of the computers connected to this network. 

 

Most of the computers on campus are connected to either or both of these “subnetworks,” so their power is accessible from labs and faculty and staff offices in virtually every building.  In addition, computers in locations away from the campus can connect to the Alpha cluster by "dialing in."

Computer Science Department Lab

The CS Department has its own laboratory that provides a number of personal computers, workstations, and other equipment for use with undergraduate and graduate courses.  The important part of this lab in relation to CS 140 is the Sun SPARC workstation classroom laboratory.  This network of workstations is part of the “intranet” and supports the teaching of CS 140 and other CS courses.

 

Each SPARC workstation is a powerful computer in itself.  Each is capable of serving multiple users in the timesharing fashion described earlier.  Only one user at a time can use a SPARC from its console, but other users may access it remotely from other computers or terminals.

Other Labs

There are other labs for general student use that provide either IBM or Macintosh personal computers that allow for connection to the Alpha cluster or to the intranet SPARC computers, or to the internet.  Two such labs are located in the CLA Building in room 98-C5-13 and in the Campus Center Building room 97-121.

Account Numbers and Passwords

Every student is issued an account number for the use of the DEC Alpha computer system, and each Computer Science student will be issued an account number for the use of the SPARC computers.  Each time you use a SPARC (or Alpha) computer you must supply your account number and your password.  The account numbers identify those who are entitled to use the SPARCs and enable the SPARCs to protect your programs and files so that only you will have access to them.  The use of a password assures that others cannot falsely identify themselves as you in order to gain access to the computer.  Account "numbers" are not really numbers, as a rule, but strings of characters.  They are also referred to as account names, user names, or user codes.  The terms are interchangeable.  Passwords are generally not words, but are also strings of characters.  They are almost universally referred to as passwords.

 

Your account number will be assigned to you along with an initial password.  You should change your password immediately and subsequently change it from time to time in order to keep it secret.  Others will know what your account number is, but no one should know your password.

 

Choosing a Password

The main thing about choosing a password is that it shouldn't be easy to guess.  This means that you don't use your name as your password!  It also means you should avoid using the names of friends, pets, cars, hobbies, or other things that people would associate with you.  In fact, you shouldn't use names or even words, but rather use arbitrary strings of characters.  Some rules:

 

Don't use fewer than six characters.

Don't use just letters, include some digits and/or punctuation characters.

Don't write it down.

Don't make it too difficult to remember.

The Login Process

In order to use the SPARCs (and most computers that are shared by multiple users) you must go through a process called "logging in" or "logging on" to the computer.  This is the process by which you identify yourself by entering your account number and password.  Similarly, when you have finished using the computer on each occasion, you will terminate your "session" by "logging off" or "logging out."  The computer keeps track of which terminals are connected, which users are using the system, and when and what they are doing.  It also records all of this as a historical "log" of all of its activities.

Notation

Throughout this document, the following notation will be used in reference to using the computer:

 

Bold Text shown in bold typeface represents user input

or response to the computer.

 

<RETURN> This symbol means that you press the key marked

"return" or "enter" (the "carriage return" key).

 

<CTRL/c> This symbol means you enter a control signal by

holding down the control key (Ctrl) and pressing

the "c" key.  For example, <CTRL/Z>

means to press the control key and the letter Z

key simultaneously.

 

italics Characters in italics indicate parameters that

change depending on the situation.  You will need

to supply the actual values.

 

Logging On to a SPARC Workstation

1.  If the workstation has not been used recently, the screen will be dark.  To reactivate the display, move the “mouse.”  Movement of the mouse will return power to the screen, but it will take a few seconds for it to warm up.  (If it doesn’t, be sure the computer itself is not turned off.)  If the computer has a reflective mouse-pad, the mouse must be operated on that pad.

 

2.  The SPARC will display a window with two “spaces” for you to fill in with your account number and password.

 

3.  You enter your account number (in lower case letters) following the word “Login:” and then enter <RETURN> (i.e., press the <RETURN> key).

 

4.  Then enter your password following the word “Password:” and press <RETURN> again.  Your password will NOT show on the screen as you type it.  Note that the password is “case sensitive” so you must type uppper and lower case exactly as it appears on your account sheet.

 

5.  The display will go blank and then display the “desktop” with a couple of “windows” on it.  The desktop is the blank background area.  When you move the mouse, the “cursor” arrow will move about the desktop or in and out of windows.

 

6.      Move the cursor into the blank desktop area and click the right mouse button.  A “Workspace” menu will be displayed.  Move the cursor to the “Programs—Terminal” entry on the menu and hold it there until a “Programs” menu appears.  Then move the cursor to “Terminal” and click the left mouse button.  A new window, labeled “Terminal,” will appear.  In the window there will be a “%” prompt.  This prompt means it is ready for you to type commands that it will carry out.  The % prompt will be preceded each time by the name of the network, i.e., “intranet.”  In the examples that follow, the prompt will simply be shown as % without that name.

 

After Logging On

Once you have logged on to the SPARC, you may enter commands to the Unix operating system.  Commands are simple declarative statements or just single words or groups of letters that direct the computer to do something, i.e., something that it already knows how to do.  For example, the command "date" will cause Unix to display the current date and time.  By contrast, the process of programming a computer is that of providing the computer with detailed instructions for doing something that it does not yet know how to accomplish.  The percent sign prompt is issued by the SPARC each time it has completed carrying out a command and is ready for a new one.  The prompting comes from the Unix “shell,” the command processor, and Unix carries out the execution of the commands that are entered.  Unix is itself a program (actually a collection of programs) called the "operating system" of the Sun SPARC computer.  Actually, it is called Solaris 2 by the Sun computer company (also sometimes called SunOS 5), the name given to their particular version of the Unix operating system.

 

In order to enter commands, you must use the mouse to activate a particular window.  Move the mouse until the arrow is in the window you want to use, then click the left button.  The active window will be indicated by its cursor.  The cursor is the rectangle following the prompt.  When the window is active, the cursor will be a solid color.  The cursor will move as you type in that window.

Files

In order for the SPARC to be most useful, it must save the programs and data that you enter from the terminal keyboard so that they may be used again at a later time.  These programs and data are saved in "files" and kept in the "file system" maintained by Unix.  The names of all of your files are kept in a "directory," and the command “ls” will cause a list of those file names to be printed.  The first time a user logs on, there may be no files yet and ls would have nothing to report.  On the intranet SPARC computers, there is a file “index.html” placed there by the system administrator.

 

You can easily create a file using the command "cat."  For example, typing

 

cat > test.dat <RETURN>

 

after the prompt will tell Unix that you want to create a file with the name “test.dat.”  Unix will then wait for you to type whatever you want put into the file.  You signal that you are finished by typing <CTRL/D>.  So, if you entered the command and then type two lines of text, those two lines are placed in the file.  When you enter <CTRL/D>, it will not appear on the screen, and the screen will look like this:

 

% cat > test.dat

this is a line of text

this is another

%

 

Now, we can ask Unix to display the content of the file with the same command, “cat.”  The content of the file (the two lines entered previously) will be displayed:


 

% cat test.dat <RETURN>

this is a line of text

this is another

%

 

It is the “>“ character that makes the difference in the two uses of cat.  In the first case, cat takes what is typed on the keyboard and copies it to a file.  In the second case, it takes the content of the file and displays it on the screen.  This will be explained later.  The directory will now contain the name of this file, and the command “ls” will display its name:

 

% ls <RETURN>

index.html

test.dat

%

Other Commands

Earlier it was suggested that you change your assigned password.  This is accomplished with the “passwd” command.  When the command is entered, Unix will ask for your old password and your new password.  For verification, you will be asked to enter your new password twice.

 

If you would like to see if anyone else is using the computer, the command “who” will display the usernames (account numbers) of all those who are currently logged on to the computer you are using.  Since you are using the computer at its “console,” any other users would have to be logged on remotely from another computer on the network.

 

Logging in begins a "session" of computer use.  When you have completed a session, you must tell the SPARC computer.  Entering the command “exit,” or “logout” will terminate the particular xterm window you are using, but it will not log you out.  Before you leave the computer, you must use the mouse (left button) to click on the “Exit” button at the bottom of the screen.  Another menu will then appear asking you to confirm that you want to exit.  Again click the left mouse button to choose to exit.  The screen will then return to the “login” window.  Otherwise, you are still logged on to the computer and someone else can use it as though they were you.  DO NOT turn the power off!

 

Certainly one of the most useful of all commands is the man (short for “manual”) command.  Man does just that—it displays pages of the manual that documents the Unix operating system.  In particular, man followed by the name of a command such as ls will cause Unix to respond with a detailed explanation of how to use the command and how to interpret the results.  Most man pages also give examples of the use of the command.  Type

 

% man cat <RETURN>

 

and see the explanation of the cat command used earlier.

 

If you enter the command man for the command man (man man), it will give you help in using man!

Electronic Mail

One of the major advantages of a network of computers such as the network of SPARCs is the ability to communicate with other users by means of electronic mail, or simply email.

 

Email messages are sent by specifying the account number of another user as that user's "address."  The computer will then deliver the message to that user if he or she is logged on, or will save it for them until their next session if they are not currently logged on.

 

Email can be sent by entering the command “mail” followed by a username.  This causes the mail program to run, and just as with the cat command used earlier, the computer will just wait for you to type on the following lines.  Just as with cat, you signal that you are finished by typing <CTRL/D>.  Thus, it will look like this:

 

 

%mail zprof

This is really fun!  I am glad that I decided

to major in Computer Science.

          A. Student

^D

 

The % prompt will reappear and the message has been sent.

 

If someone sends you mail, then the next time you log on to any SPARC in the CS lab you will get a message saying that you have mail.  When you invoke the mail program, your mail will be displayed.  The mail program has its own prompt to show that it is ready to respond to mail commands.  Following the display of the most recent message, a question mark (?) prompt will be displayed.  Pressing <RETURN> will cause the next message (if there is more than one) to be displayed.  When you have finished, respond to the mail prompt with the mail command “x.” This will terminate the mail program and return you to the Unix prompt.

 

While using the mail program, there are some other commands that will be useful.

 

h                      will display a list of your mail messages that you have saved.  The messages are numbered, and you can display one by typing its number.

 

s file-name       will extract the content of the message you are reading and place it in a file with the name you specify.  Entering the command without a file name causes the message to be saved in a file named mbox.

 

r                       (reply) permits you to reply to the message you are reading without having to provide the address of the recipient (it automatically goes to the sender of the message you are reading).  The message is then deleted

 

?                      gives you additional information about the mail program.  It will list all of the mail commands and explain each one if you request it.  The Unix man command explained earlier also gives the man pages for mail with a more detailed explanation.

 

 

Chapter 3

Files and Directories under Unix

File Names

File names can be any combination of any of the ASCII characters except the “slash” (/), which has a special meaning.  However, it is best to avoid most of the “special characters” since they may also have a special meaning in some situations.  Thus it is best to limit the characters used in file names to letters, digits, periods, the underscore character (_), and the hyphen (-).  The name may start with any character.

 

Letters used in file names are treated as "dual-case" meaning that upper and lower case versions of the same letter are considered to be distinct.  Thus, each of the following file names would be considered to be different from the others:

 

            mydata             Mydata            MyData            MYDATA

 

The period is often used to add an “extension” to a file name.  Thus file names often appear as:

 

            Myprogram.java                     results.dat                   report.txt

 

The extension is normally short, and used to indicate what the content of the file is.  For example, the .java extension above indicates a Java source language program; the .dat extension would indicate a file of data; and the .txt extension indicates a file of text.  The extension is part of the file name, and is sometimes required by some software that makes use of the file.  In other cases, it is simply a convenience to make the file name more meaningful.  Unix itself attaches no significance to the period, so multiple periods may be used in a file name and a name such as “homework.CS140.assignment1.notes” is perfectly legal.

Directories

Each directory has a name, and the "home" or “login” directory for each user has the same name as the user's "account name."  Thus the directory of files belonging to user xyz would be named xyz.  The directory xyz is itself a file, a file containing the names of files.  The name of this directory is in turn kept in a file.  The name of this file is "user" (for all accounts issued to students, faculty, or staff).  The name of this directory file is kept in another file named “dfs.”  So the directory for user xyz is referred to as /dfs/user/xyz, meaning that the directory dfs contains an entry “user” which is the name of a file containing the user names of all of the users (one of which is named “xyz”) and the file xyz contains the names of all of xyz's files.  If user xyz were to create a file containing a Java program, say Proga.java, then that file's more complete identification would be

 

/dfs /user/xyz/Proga.java

 

The name of the file Proga.java is in a directory named xyz.  The name of the directory file xyz is in a directory named user.  The name of the directory file user is in a directory named dfs.

 

Note the use of the slash (/) to separate the names of the directories.  The first slash is itself a directory called the “root” directory.  So the name of the directory file home is in the root directory.  The sequence of directory names leading up to the name of the file (in this case the Java program) is called the “path” to the file.  This allows the same file name to be used more than once.  Each user could have a file named Proga.java, but Unix would know the difference because they have different paths.  They "reside" in different directories.

 

The name “/dfs” is equivalent to the longer name:

 

/…/intranet.csupomona.edu/fs

 

and is much easier to use!

Viewing and Copying Files

When a user logs on to the Sun, the user's directory automatically becomes the default directory used by Unix (thus the term “login” directory).  Consequently, the user only needs to refer to his or her own files by using their names.  Unix will look for them within the user's directory.  When necessary, the user can specify some other directory or change the default directory.  Additionally, any file in the system can be specified by giving a more complete name.  For example

 

/dfs/user/hnriley/words.dat

 

This is the name of a file in a different directory from your own.  You can see the content of this file by using the command “cat.”  The command “cat” in this case means display the file at the terminal.  So we see the file’s content by typing

 

% cat /dfs/user/hnriley/words.dat

 

or, equivalently

 

% cat  ../hnriley/words.dat

 

The double period (..) indicates "use the directory name above the current directory," or in other words, the directory that contains the name of the current (default) directory.  In this case, the current directory is your directory, the one above it is the parent directory of your directory.  Using this twice ( ../../ ) means the directory that is the parent of the parent of your directory.

 

You may also make a copy of this file.  The command “cp” means copy.  Thus

 

% cp /dfs/user/hnriley/words.dat  words.dat

 

makes a copy of the file and places it in your directory, giving the copy the same name as the original.  When this is desired it may be accomplished more simply by typing

 

% cp /dfs/user/hnriley/words.dat  .

 

or just

 

% cp  ../hnriley/words.dat  .

 

The final single period, or “dot” (.) indicates the current directory, and not specifying a name means "give the copy the same name."  If one wants to give the copy a name that is different from that of the original, then typing

 

% cp /dfs/user/hnriley/words.dat  mywords.dat

 

copies the file and gives the new file (the copy) a different name.

Protection of Files

The file used in the previous examples can be displayed or copied because the owner has given "read permission."  Each file has associated with it a set of permission codes that determine whether its contents are private to the owner or are to be shared.

 

The command

 

% ls -l filename

 

will show the "directory entry" for the file including its protection codes.  The hyphen symbol in the command indicates an "option" that is added, in this case the option to display more information than just the file name, including the protection codes that would otherwise NOT be displayed.

 

After copying the file words.dat, the command

 

% ls -l words.dat

 

will display something like

 

-rw-------   1 yourname  cs  2313 Sep  7 14:32 words.txt

 

meaning that the owner (you) has permission to read or write the file.  This is the default protection given to your files.  No one else may access the file in any way.  You may now change the protection for the file by

 

% aclmod go=rw words.dat

 

Note that there must be no space between the “go” and the “=” or between the “=” and the “rw.”

 

Now the ls command with the -l option would produce

 

-rw-rw-rw-   1 yourname  cs  2313 Sep  7 14:32 words.txt

 

 

indicating that you have added the ability for others in your group to read or write the file, and for everyone else (“others”) to read or write it.

 

The permissions are

 

r read permission means it can be displayed or copied

w write permission means it can be changed

x execute permission means

a)  if a program, it can be "run"

b)  if a directory, it can be "searched" to find a file

 

A file that contains a program that can be read, changed, or executed, by anyone would have a set of permissions that appear as follows:

 

-rwxrwxrwx  (more readably:  -  rwx  rwx  rwx)

 

Notice that the permission codes appear in three positions, or groups, of three codes each.

 

The three positions indicate, from left to right,

 

the owner of the file (the person who created it, the “user”)

the group that the owner belongs to (by default the group is “csupomon”

and includes everyone in the user directory)

others (ALL users of the system)

 

In setting or changing permissions, the letter g refers to “group” and the letter o to “others”, as in the example above.  Similarly, u means “user” (the owner).

 

The hyphen (-) at the beginning of the permissions in the examples above indicates that the file is a “plain” file (text, program, data).  A “d” in this position indicates that the file is a directory.

 

The command

 

% aclmod ugo=r rules.txt

 

would change the protections on the file rules.txt so that everyone could read it, but no one, including its owner, could change it.  However, the owner can always change the protections again so that it can be changed or deleted.

 

Whatever the permissions on a file are, it can not be accessed by others unless they also have permission to search your directory.

 

% aclmod go=x /dfs/user/xyz

 

would make the directory of user xyz searchable by others in the csupomon group and by all other users.  If you give “others” permission to search your directory, and give “others” permission to read a file, then you have made that file "public."

 

It is also possible to permit access to your files to other users individually.  For more detail on the use of aclmod, type the command word by itself:

 

 

% aclmod

Deleting Files

When a file containing a program or data is no longer needed, it can be deleted (removed):

 

% rm words.dat

 

Since space on the disk for file storage is limited, files should be deleted when they are not needed.

 

 

When deleting files, it is usually a good idea to use the confirmation option, -i.  If you type

 

% rm -i words.dat

 

then Unix will respond with the file name and ask if you want to remove it.  This gives you the opportunity to confirm that you really typed the correct file name.

Wildcards

The asterisk (*) character may be used as a "wildcard."  In other words, its presence in the command says "anything will be considered to match this."  This wildcard character can be used in most commands involving file names.  For example

 

% ls *.adb

 

will cause the ls command to list the names of all files with the extension .adb.  On the other hand,

 

% ls progx.*

 

would cause ls to list any file with a name starting with progx, regardless of what its extension might be.

 

There is also a single character wildcard, the question mark (?).  When it is used, any single character is considered to match it.  Thus

 

% ls prog?.adb

 

would list the names of any files whose names begin with the letters prog and are followed by one other character and the extension .adb.  For example, progx.adb, prog1.adb, prog9.adb, proga.adb would all be included in the response to the ls command.  prog12.adb would not be included.

The Directory Structure

Each file is listed in a directory, and each directory is in turn a file listed in another directory "above" it.  These directories form a structure such as that depicted in Figure 3-1.

 



Editing Files

There are several editors (programs used to create new files or make changes to existing files) used with Unix.  Perhaps the easiest to learn and to use is the "vi" editor.  The vi editor is standard with all Unix systems.  Some of the others are not always available.  The vi editor is "invoked" with the command

 

% vi filename

 

If the file exists, the first several lines of it will be displayed on the screen.  If the file does not, a blank screen will appear, and you may type what you want the file to contain.  Either way, the file is “opened.”  You may simply type new text into that file or corrections to existing text.

 

There are two “modes of operation” in vi, command mode and insert mode.  When the editor first opens the file, the editor is in command mode.  The editor will perform various operations when you press particular keys or combinations of keys.  The letter “i” (insert) changes the editor to insert mode.  In this mode, you may type text into the file.  The “cursor” (black rectangle) indicates the position where the next character will be inserted.  The backspace key will cause the cursor to move backwards.  The characters passed over will not be erased, but when you resume typing, they will be replaced.  The “return” key will cause the cursor to move to the next line and to the left side of the window.  In general, you should not type lines wider than the visible part of the window.

 

The escape key (ESC) will return the editor to command mode.  In command mode, the arrow keys will cause the cursor to move to different positions.  When in insert mode, pressing an arrow key will change to command mode (without moving the cursor).  A second pressing of the arrow key will cause the cursor to move.  In command mode, the letters h, j, k, and l will also cause the cursor to move.  Since not all situations in which the vi editor may be used will support arrow keys, you should try moving the cursor with the letter keys.

 

In command mode, the letter “x” will delete the character under the cursor.  Pressing the letter “d” twice will delete the current line.

 

The letter “a” (append) will also change the editor to insert mode.  The difference is that the first character inserted will be positioned after the cursor position.  Entering insert mode with the “i” causes the first character to be inserted where the cursor is.  In either case, existing characters to the right of the cursor will be moved over to make room for the inserted text.

 

The letter “o” (open) also invokes insert mode.  The lower case “o” opens a line (inserts a blank line) below the current cursor position and move the cursor to the beginning of it.  The upper case “O” opens a line above the current line.

 

Some other useful commands:

 

            $                      move the cursor to the end of the current line

            0 (zero)            move the cursor to the beginning of the current line

            r                       replace (overwrite) the single character under the cursor

            R                      begin overwriting continuously

            J                       join the current line and the following line

 

The colon key “:” lets you enter an ex editor command on the “status line” at the bottom of the window.  One of these commands is particularly useful.  This is “q!” which causes the editor to quit without saving the changes to the file.  This can be helpful if you want to abandon an editing session where you have made errors and want to start over.  Entering “:” then  “q” then “!” (:q!) will cause this to happen.

 

Use the editor to edit the file words.dat.  As you have seen, the file just contains a random selection of words randomly placed.  Use the editor to rearrange the words into a single column.  Place the cursor at the end of the first word in a line and press the return key.  That inserts an "end of line" and moves the remaining part of the line to the next line.  Repeat as necessary.  Note that the last line displayed says simply "End of file."  This marks the end, but is not part of the file's content.  You may delete some words if you like, or add some new ones.

 

When you have finished editing, you indicate this to the editor by typing

 

:wq

 

This causes the editor to “write” or save the file, then terminate.  View the file by entering the command

 

% cat words.dat

 

You now can have the computer sort this file (i.e., put the lines in alphabetical order).  Try

 

% sort words.dat > sorted.dat

% cat sorted.dat

 

You should see a list of the words in a single column, left justified, in alphabetical order.

 

The syntax of the SORT command is

 

            sort  filename1 > filename2

 

The content of the file with the name given for filename1 is sorted and the result is placed in a new file with the name specified by filename2.  Note the use of the “>“ symbol.  This causes “file redirection.”  This is what we saw earlier with the cat command.  Using the > symbol followed by a file name causes the output from the command to be redirected to that file instead of coming to the screen as it would otherwise.

 

The name of a file can be changed with the “mv” command.  Its syntax is

 

            mv  filename1  filename2

 

so the command

 

% mv  sorted.dat  newwords.txt

 

would change the name (and the extension) of the file sorted.dat to newwords.txt.

 

Another file that you can edit is the file /dfs/user/hnriley/lib/sample.txt.  The content of that file is shown in Figure 3-2.  Copy this file and edit it.

 


 

 


                This is a text file containing some samples of lines of text.
                This is the last line of this awful fyile.
                This sentence are easy to read  There are some awful errors in spelling,
                punctuation, and grammar in this file
                These airors would correspond to "syntax errors" in a program.  In other
                words, these are errors that arise from violating the rules of the language.
                There are also some errors in the texxxt that appears here that would correspond
                to the "logic errors" in a program.  In other werds, there are sentences that
                are syntactically correct (no spelling errors, etc.) but that say the wrong
                thing.  To read this entire text, begin here and read to the end.
               
                In the text above:
                1)  Fix the "syntax errors."
                2)  Fix the "logic errors."
                3)  Change every occurrence of the word "text" to all caps (upper case).
                4)  Change the text so that each sentence starts on a new line.
                5)  Remove all occurrences of the word "awful" from the text.
                6)  Insert the word "beautiful" before each word "program" in the text.
 

Figure 3-2

The File SAMPLE.TXT

 

Creating a ".cshrc" File

The ls command gives us a list of file names.  When we used the -l option, the command gave us more information, including the permission codes and a character that identifies the directories.  If we use the -F option, the ls command will also identify files containing programs.  This option puts a slash “/” after each directory name, and an asterisk “*” after each program name.  If we want, we can make a new command that does both the “long” listing and the identification.  We do this using the “alias” command:

 

%  alias lsl ls -lF

 

For the remainder of the session, the command lsl produces the listing that would be produced by ls -lF

 

We could do this each time we log in, or we can put this in a file of commands that will be automatically executed each time we log in.  The file that will accomplish this is named .cshrc.  Unix will look for a file by that name in your directory when you log in.  If it exists, then Unix will execute the commands that it contains before issuing the % prompt.  You should create a .cshrc file with a few commands to start with.  As you become more familiar with Unix, you will want to add other commands.

 

 Start with the example that follows.  Note that the first line must start with the pound sign “#.”  Also, anything following a pound sign (#) will be ignored by Unix and serves only as a comment.

 

The command

 

% vi .cshrc

 

invokes the editor to create the file, then you type:

 

 

#

alias lsl ls -lF        #make a new ls command

echo "hello"

date                     #display time and date

 

The next time you log in, this file will be "executed" by Unix.  You will see the greeting "hello," and the date and time of day will be displayed.  The other actions in the .cshrc file, such as creating the command lsl will take effect.  You can also cause this file, or any shell file, to be executed at any time by entering the name of the file as a command.  For example

 

%  .cshrc

 

will cause the file .cshrc to be executed.  Thus, you can see some of the effect of this file as soon as you have created it without having to log in again.  You will learn a number of other commands that you will want to add to your .cshrc file.

Some Other Commands

There are three commands (actually just control characters) that are important in communicating with Unix while it is doing something else!

 

            <CTRL/S>

 

Typing <CTRL/S> while Unix is sending output to the screen will cause the output to pause or "stall"; i.e., Unix will quit sending to the screen, the screen will stop scrolling, and Unix will wait until you signal it to continue.  This is done with

 

            <CTRL/Q>

 

The <CTRL/Q> character will cause Unix to resume sending and the screen to continue scrolling ("S" for "stall," "Q" for "quit stalling").

 

            <