Work in groups of four. You have one two hour supervised laboratory session to start work on the practical, the remainder of the laboratory must be completed in your own time before next week. The document 'Introduction to Unix' indicated in the tasks should be uploaded into the weekly upload system in pdf format. The reflection section of the laboratory is a group reflection. You should print out the Marking Scheme and bring the document 'Introduction to Unix' indicated in the tasks to the laboratory next week for marking.
Unix was developed in the early 1970s and has become one of the main operating systems used in industry. It is available on almost any machine.
The vogue in the 1970's was for complex multi-user systems on medium to large mainframe computers. At the same time, cheap minicomputers such as the Digital PDP series were being installed in increasing numbers in research laboratories. These machines could only be programmed in native assembly code, and the operating systems were primitive.
A group at Bell Laboratory's in the United States started work on a minimal operating system which would fit into the store of the 64k PDP-11. This was written initially in PDP-11 assembly code and so could run only on machines of that family, however, Thompson and Ritchie soon spotted the advantages to be had from writing this operating system in a high-level language. Thus, the development of the UNIX operating system, and the C programming language, went hand-in-hand.
The use of a high-level language for almost all of the operating system code (a small amount, perhaps 5%, had to remain in the native machine code) meant that UNIX could be put up on any machine with a translator for the C language. Unlike DOS and Windows which run only on Intel processors, UNIX can run on any machine.
The severe limitations on storage of the early UNIX machines forced the developers to think long and hard about UNIX. As a consequence, UNIX is extremely well-designed and well-engineered: its components fit together well, and its powerful shell programming environment permits complex manipulations to be built up from a tool-box of simple components.
The UNIX operating system consists of a small kernel of essential utilities for maintaining a file system and for running programs in multi-tasking mode. This kernel interfaces directly to the hardware. Surrounding the kernel, between it and the user, is the shell. The shell is the command language that provides the user interface to UNIX. The shell provides a fixed set of commands and will execute commands typed in from the keyboard or from a file. This is similar to a batch file in DOS. Files containing commands (called Shell Scripts) may be created allowing users to define their own, more sophisticated commands. UNIX is a stable operating system which retains a fierce loyalty amongst users, and is extremely flexible and powerful.
|
User |
||||||||
|
Shell |
||||||||
| UNIX Kernel | ||||||||
|
BIOS |
||||||||
| Hardware | ||||||||
UNIX File System
UNIX uses a hierarchical file structure, files, directories and even I/O devices are treated uniformly. There is no common conception of a 'drive' as in windows; all UNIX files 'hang' from one root, called /.

/home/ga606/ccs
/usr/local/work
UNIX can be accessed in the following ways
More information and help is available on the School's Unix systems at http://unix.cms.gre.ac.uk
TASK 1
|

TASK 2 - On Campus Students Only
TASK 2 - For Students Studying Off Campus
|
The GNU C compiler gcc can be used to compile C source code on any CMS Unix/Linux system.
To use gcc to compile a file called filename.c you would enter gcc filename.c -o filename.out
Further details on using gcc and other supported C compilers; including full documentation is available at http://unix.cms.gre.ac.uk/software/compilers.html
TASK 3
|
The shell is a command language that provides a user interface to the UNIX operating system. The shell executes commands either typed in at the terminal or from a file. Up to now you have only typed in commands at the keyboard. By providing the facility to read and execute a sequence of commands, you can effectively create your own commands with the same status as system commands. For example suppose you create a text file called setupusers, containing the UNIX commands to set up accounts for 100 users. If you typed at the command prompt: setupusers this would be executed in the same way as if you typed any other Unix command i.e. ls
The shell is both a command language and a programming language that provides you with the ability to set up an environment tailored to you or a group's needs. It can link applications together that would normally have to be initiated by separate commands from the command line (like a batch file in DOS). For example, it could link a compiler with an editor, such that if there were errors from your compilation it could automatically throw you back into an editor.
As a programming language the shell provides you with facilities above those provided by the raw UNIX system such as programming constructs, variables, wildcards etc. A file that contains shell commands is called a shell script.
There are three popular shells:
All three shells have a common core. Beyond this they have slightly different facilities and a different syntax for programming. There are 'pro et contra' to each one e.g. the C shell is slightly better than the Bourne shell for interactive work but slightly worse at script programming. At this level is does not really matter which one you choose, we shall concentrate on the Korn shell as the syntax is slightly easier.
To generate a shell script a set of commands is written to a file, and the file given execute permission using chmod. When a file has execute permission it means it can be run from the command line by typing its name.
File Execute Permissions
Before looking at shell script programming it is necessary to understand how to give a file execute permission using the chmod command.
At the UNIX prompt, you will see the permission flags for each file when you type ls -l e.g. -rwxr--r-- The first letter specifies a directory or not, the remaining are three groups of three letters stating the read, write and execute permissions for the owner, the group and everybody else.
To change the permission of a file requires three numbers, the first is the permissions for the owner, second for the group and third for everyone.
Suppose you wanted to give yourself read, write and execute access and the group and everyone read access only
You add 4 + 2 + 1 = 7 and assign yourself a value of 7 the group is assigned a 4 and everyone a 4. e.g.
chmod 744 sh1.ksh
For example, to give yourself and the group read and write access, but none to anyone else:
chmod 660 sh1.ksh
Korn Shells
Use the text editor gedit to create and run the following example Korn Shell Scripts, don't forget to change the file permissions using the chmod 700 command. It is important that you use the UNIX editor gedit, and do not import files into UNIX from a windows editor, as these files will often have control characters attached and the file will not function correctly.
Example 1:- sh1.ksh#! /bin/ksh
echo "Please enter your name: "
read name
echo "Your name is" $name
It is fairly obvious what this script does, here is a sample run (having given sh1.ksh execute permission i.e. chmod 744 sh1.ksh):
The file name is sh1.ksh however it can be anything. The .ksh is used for clarity to indicate it is a Korn shell script. The file must begin with a #! pathname (which in this case is /bin/ksh), telling the system to invoke a Korn shell.
It is this line, not the file name extension that determines which shell is used to execute the script. If none is given then the default is a Bourne shell. A # used anywhere else in the script, other than line 1 is interpreted as a comment e.g.
# This is a comment line
The echo command is a print statement e.g.
echo hello
Any words after the echo statement are printed on a new line. To print a blank line or achieve a carriage return just execute an echo statement with no text following it e.g.
Example 2: - sh2.ksh
This shell script expects command line parameters to be passed to it. The parameters are stored as $1, $2, $3 etc. where the number represents the order in which they were typed in i.e. in the example below $1 = Hello, $2 = Peter, $3 = Smith $* stands for: a list containing all the input parameters. An example run of sh2.ksh is:
Example 3:- sh3.ksh This shell script prints each input parameter in turn.
Example run:
The syntax of the for loop is for ... in ... do.......done where do.....done indicate the boundaries of the loop. To initialise and create a variable it is just assigned a value e.g. count=1 The let command forces evaluation of an assignment statement e.g. let count=count+1 Note: There must be no spaces in the arithmetic expression i.e. either side of the equals or plus sign in the example below.
Example 4:- sh4.ksh
Example run:
Example 5:- users.ksh The file is simply used to execute a one line UNIX command (who | wc -l) that is awkward to type on a regular basis. who - provides a list of the users currently on the system wc -l is a word count program that counts the number of lines (requested by -l) and hence the number of users (as who provides one per line). $( expression ) - tells the Korn shell interpreter to evaluate the expression within the brackets.
Example run:
Example 6:- menu.ksh
The read statement accepts input from the keyboard and stores the value in a variable e.g. read name where read is the command understood by the shell script and name is the variable containing the input. To refer to a variable in a program it is referenced with the $ sign e.g. echo $name
The while statement:- the syntax is While ...... do ..... done. While evaluates the condition and if it is true, it executes the statements between the do and done. It does this repeatedly until the while condition becomes false.
The condition test $stop -eq 0 tests the value of the variable stop and if it is equal to 0, then the condition is true. If it is true then the while statement will enter the loop. Given that set stop = 0 is before the while loop, the condition will always succeed the first time and hence the loop will always be entered.
The case statement is like an if statement only shorter. What it says is: test the value of the variable called reply against the following values and carry out the appropriate actions on the right side of the ) character:
case $reply in
"1" ) if reply is = 1 do these statements ;;
"2" | "3" ) if reply = 2 or 3 do these statements ;;
"4" ) if reply = 4 do these statements ;;
"5" ) if reply = 5 do these statements ;;
"6" ) if reply = 6 do these statements ;;
* ) if reply does not equal one of the above values then do this i.e.
I am the default ;;
esac - this ends the case statement.
The if statement
if test -f *.bak
then
rm *.bak
echo Files deleted
else echo No *.bak files found
fi ;;
This says (in pseudocode):
if there are any files called something.bak
then
delete them
tell the user that the files are deleted
else
tell the user that there are no .bak files to be found
endif.
Note: The if, then, else, fi must be on separate lines.
The test command:
There are many parameters available for use with the test command. Here are some of the useful ones:
-f True if filename exists as a non-directory
-d True is filename exists as a directory
-r True is filename exists as a readable file
-n True if string contains at least one character
-s True if filename contains at least one character
-w True if filename exists as a writeable file
-x True if filename exists as an executable file
-z True if string contains no characters
str1 = str2 True if string 1 = string 2
string True if string is not null
int1 -eq int2 True if int1 equals int2
int1 -ne int2 True is int1 does not equal int2
int1 -gt int2 True if int1 is greater than int2
int1 -ge int2 True if int1 is greater than or equal to int2
int1 -lt int2 True if int1 is less than int2
int1 -le int2 True if int1 is less than or equal to int2
Alternative syntax for conditional expressions used in e.g. whiles and ifs:
while [ $stop = 0 ]
if [ $stop = 0 ]
while test $stop -eq 0
if test $stop -eq 0
Testing for strings (this is very counterintuitive, you quote the variable in the test, not the string to be tested):
If test $reply=m
|
TASK 4
Use the text editor gedit to create the following Korn Shell Scripts, don't forget to change the file permissions using the chmod 700 command. It is important that you use the UNIX editor gedit, and do not import files into UNIX from a windows editor, as these files will often have control characters attached and the file will not function correctly.
Append the programs and the script files demonstrating the operation of the programs to the document 'Introduction to Unix'. |
| TASK 5 - Optional
Install Linux on your home computer Linux is a UNIX type Open Source computer operating system designed primarily for the PC but also available for a wide range of other systems. To install Linux on you home machine, follow the instructions at http://staffweb.cms.gre.ac.uk/~wd18/linux_install/ |
| TASK 6
- Reflection
Identify the sections of the laboratory you have understood and demonstrate your understanding - beyond the simple level of completing the laboratory - through cognitive processes such as analysing, explaining, interpreting, and evaluating. Illustrate, by the use of examples how the laboratory contributed towards your understanding and your Degree programme. For the sections of the laboratory in which you struggled with, or were uncertain of, identify why this was the case. Evaluate the effectiveness of your learning strategy, including factors such as, motivation, preparation, commitment, time management, communication, constraints and support. With reflection to past experience, identify how you could improve your learning and performance to overcome the barriers encountered in this laboratory such that they do not infringe upon the next laboratory you undertake. With relation to the sections of the laboratory you encountered difficulty with, state how, and by when you intend to gain competence in these areas. Critically appraise the laboratory; identify sections you thought were positive, facilitated your understanding and contributed to your Degree programme; identify sections that require improvement and state how and why would you change the laboratory to improve the laboratory for the next year's students. |