Introduction to Unix


  1. Instructions

    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.

  2. Objectives

  3. Introduction

    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 /.

    The path concept is used in UNIX, so, typical directories might be:

         /home/ga606/ccs

         /usr/local/work

    UNIX can be accessed in the following ways

  4. More information and help is available on the School's Unix systems at http://unix.cms.gre.ac.uk

  5. Procedure

    1. Access to UNIX using Putty (SSH)

      TASK 1
      1. In windows, from the programs menu select > Host Access > Putty (SSH) > ssh student.cms.gre.ac.uk

      2. At the login: prompt, enter your user id. At the password: prompt, enter your password.

      3. Script files keep a record of the commands typed and the system's textual responses. When a recording of a session is required, type: script filename where filename is the name of a file, chosen to record the session. If no filename is given, the default name is typescript. Start a script file. i.e. script task1

      4. Examine the effect of the following commands


      5. Repeat the cal 6 3000 command, but this time, add a redirection: cal 6 3000 > june3000.txt Nothing should appear on the screen, except the UNIX prompt. Use the ls command to show a directory list of your area. A new file - june3000.txt - will have appeared in your directory.

      6. Make a copy of the june3000.txt file using the cp command: cp june3000.txt june3k.txt and repeat the ls command.

      7. More information can be displayed about files by using the -l (the letter l, not the digit one '1') option with the ls command (i.e. ls -l). This command lists each of the files in the current directory, the files permissions, the size of the file, date of the last modification, and the file name or directory.

        Ten Permission Flags Directories Group Size in Bytes Date Directory or File
        drwx------ 2 users 4096 Nov 2 19:51 mail/
        drwxr-s--- 35 www 32768 Jan 20 22:39 public_html/
        -rw------- 1 users 3 Nov 25 02:58 test.txt

        Permissions Flags Notation:- The first letter specifies a directory or not, the remaining are three groups of three letters stating the read (r), write (w) and execute (x) permissions for the owner, the group and everybody else, if some permission is denied, then a dash "-" is used instead.
        Use cd to change to directory /home/sp02/unixfiles and display the contents using ls -l Check the current directory using pwd and return to your home directory by typing cd The cd command will return you to your home directory from anywhere in the file system. Check you are in your home directory using pwd

      8. Make a directory called ccs using the mkdir command. i.e. mkdir ccs

        Change to the ccs directory, and give the command to copy all the files from the directory /home/sp02/unixfiles/ to your own area. For 'all files', use the wildcard character * i.e.

        cp /home/sp02/unixfiles/*.* .

        Note the destination (.) which denotes 'the current directory'.

        You could have stayed in your home directory, and instead given the cp /home/sp02/unixfiles/*.* ccs command.

        Use ls - l to find the sizes and owner(s) of these files.

      9. Display the file poem.txt using cat and more (2 separate commands). The cat command takes a filename, and sends its output to the screen.

        The output of cat can be redirected to a file. Try cat poem.txt > poem2.txt no screen output should result, but ls -l should tell you what has happened.

        Now try a slightly different redirection: cat poem.txt >> poem2.txt The double greater-than (>>) works like > except that it appends data to the end of the named file.

        Try to anticipate what will happen with the cat poem.txt >> poem2.txt command. Use ls -l and more to verify your hypothesis:

      10. To delete a file you need to remove it using the rm command e.g. rm poem2.txt

      11. Stop recording to the script file by entering the exit command.

        Copy the script file to your area on the PC network (Your Unix area is mapped to drive U: in your PC account), and insert the script file contents into a word document. Name the word document 'Introduction to Unix'

      12. Log out. Type logout and press return.



    2. Access to UNIX using Xming

      Xming is a program which runs on a Microsoft Windows PC to communicate with Unix/Linux servers and provides a graphical user interface for using these servers. Thus applications on Unix/Linux servers that require graphical displays can be run, producing graphical output on the PC screen.

      TASK 2 - On Campus Students Only
      1. From the programs menu select > Host Access > X Windows > CMS Student Solaris Server and login to UNIX. This will then launch the Sun Java Desktop (based on Gnome - a desktop for Linux or UNIX platforms).

      2. From the Launch button in the bottom left corner select Applications > Accessories > Text Editor. The editor is reasonably intuitive with regard to open and saving files.
        • Change all occurrences of 'Shalott' in poem.txt to Onion (poem.txt is in the ccs directory)
        • Change all the abbreviations such as 'd into ed
        Save the edited files under different names.

      3. On the desktop find the icon This Computer and double click it. Then in the Location bar enter /home/your_userid e.g. /home/aa001. This will display all of your files in your Unix home area.

      4. Right click on the desktop and select Open Terminal. In this terminal window type firefox (all lower case) and browse the web.

      5. Copy the new version of the poem and web page you surfed to and append to the document 'Introduction to Unix'.

      TASK 2 - For Students Studying Off Campus

      1. Download and install Xming, Start Xlaunch, choose 'Multiple windows' then Next. Select 'Start a program' then Next, In the Run Remote select 'Using Putty'. Now enter student.cms.gre.ac.uk as the 'Connect to computer', your userid and password then Next. Continue by clicking next you can save the configuration and click finish. This will launch and Xterm on a local Xserver.

      2. In the xterm type gedit & The editor is reasonably intuitive with regard to open and saving files.
        • Change all occurrences of 'Shalott' in poem.txt to Onion
        • Change all the abbreviations such as 'd into ed
        Save the edited files under different names.

      3. In the xterm type nautilus & Then in the Location bar enter /home/your_userid e.g. /home/aa001. This will display all of your files in your Unix home area.

      4. In the xterm type firefox & and browse the web

      5. Copy a web page you surfed to to the word document 'Introduction to Unix'.

    3. Running Programs in UNIX

      Two kinds of files can be run under UNIX: those which contain executable code, usually the result of compiling a program from a high-level language such as C into machine code; and files called shell scripts which contain a sequence of commands in the Shell programming language.

      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
      1. Start a script file i.e. script task3
      2. From the ccs directory compile the C program hello.c and run it by typing hello.out You should see the familiar Hello World displayed.
      3. Use the text editor to change the Hello world program so it will now display Hello Your Name
      4. Compile the C program oddeven.c and run it by typing oddeven.out
      5. Append the script file to the document 'Introduction to Unix'. 


    4. UNIX Shell Programming

      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:

      • Bourne Shell (bash)
      • Korn Shell (ksh) - superset of the Bourne Shell
      • C Shell (csh)

      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

      • Read = 4
      • Write = 2
      • Execute = 1

      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):

      Please enter your name:
      Peter
      Your name is Peter

      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.

      • /bin/csh would invoke a C shell
      • /bin/sh would invoke a Bourne 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.

      echo
      echo hello
      echo

      Example 2: - sh2.ksh

      #! /bin/ksh
      echo parameter 1 = $1
      echo parameter list = $*

      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:

      sh2.ksh Hello Peter Smith
      parameter 1 = Hello
      parameter list = Hello Peter Smith

      Example 3:- sh3.ksh This shell script prints each input parameter in turn.

      #! /bin/ksh
      for i in $*
      do
      echo parameter $i
      done

      Example run:

      sh3.ksh Hello Peter Smith
      parameter Hello
      parameter Peter
      parameter Smith

      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

      #! /bin/ksh
      count=1
      for i in $*
      do
      echo parameter $count is $i
      let count=count+1
      done

      Example run:

      sh3.ksh Hello Peter Smith
      parameter 1 is Hello
      parameter 2 is Peter
      parameter 3 is Smith

      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.

      #! /bin/ksh
      echo There are $(who | wc -l) users on the system

      Example run:

      There are 21 users on the system

      Example 6:- menu.ksh

      #! /bin/ksh
      stop=0
      # test int1 equals int2
      while test $stop = 0
      do
      echo
      echo
      echo Example Menu Program
      echo
      echo 1 : Print the date
      echo 2, 3 : Print working directory
      echo 4 : Directory listing
      echo 5 : Delete *.bak files
      echo 6 : exit
      echo
      echo 'Please enter your choice? '
      read reply
      case $reply in
      "1" ) date ;;
      "2" | "3" ) pwd ;;
      "4" ) ls ;;
      "5" ) if test -f *.bak
      then
      rm *.bak
      echo Files deleted
      else echo No *.bak files found
      fi ;;
      "6" ) stop=1 ;;
      * ) echo illegal choice ;;
      esac
      done

      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.

      1. Write a shell script to prompt the user for a variable, test the value of the variable, if it = 1 then write to the screen, "The value was 1", else write to the screen "The value was not 1".

      2. Write a shell script program that accepts three parameters and displays their average to the user. Note: you will probably have to do two calculations, one for the total and one for the division.

      3. Write a shell script program that accepts a variable number of parameters and displays their average, and the number of numbers input, to the user.

      4. Write a shell script program that accepts a file name from the user, displays the file to you using the less command, then calls the editor (command is gedit) to allow you to edit that file, once you quit the text editor, your shell script will continue to run. It should then display the file using the less command again. Make some changes in the text editor so you can see that the file has changed. Do not forget to test if the file exists before you attempt to do anything with it. If it does not exist then output an appropriate error message.

      5. Write a shell script program to provide the following facilities to a user in the form of a menu:
        1. Allow the user to use an editor e.g. gedit (which can be called from the command line using gedit filename) to write a poem.
        2. Run a word count on your poem using wc
        3. Display the output to the screen and display a page at a time using more or less (ask the user which one they want to use).
        4. Sort the output using the UNIX sort command and put the results into a file, then display the output of the sorted file using cat or more. (ask the user which one they want to use)
        5. Exit

      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.