CS1101: Laboratory 4(a)

An Introduction to Shell Scripting

Academic Year 2006-2007

Lecturer: Dr. Barry O'Sullivan
Department of Computer Science
University College Cork

b.osullivan@cs.ucc.ie


Objective of this Lab

To familiarise students with the concepts of shell scripts.

This lab is based on material from Paul Anderson's book entitled "Just Enough UNIX".


Submission

During this lab you will be asked to write 2 shell scripts of your own. These must be submitted by 5pm Friday the 24th of November (this coming Friday).

Submit both of your scripts using the following commands:

cs1101submit chro
cs1101submit chrw


Background

Until now we have used the UNIX shell as a command-line interpreter. The shell can also be used as a high-level programming language. Instead of entering commands one at a time in response to the shell prompt, you can put a number of commands in a file, to be executed all at once by the shell. A program consisting of shell commands is called a shell script.

All shell scripts that you write, for this course, should be based on the sh shell, otherwise known as the Bourne Shell. A HTML version of a tutorial is available online.

A simple shell script written for the sh shell is given below:

#!/bin/sh
# A simple shell script
cal
date
who

An a simple exercise for yourself, create a file called commands.sh and copy each of the above lines of code into it. You should use the vi editor for this.

Ensure that the script is executable by you by running the following command at the command prompt in your shell window:

chmod u+x commands.sh

Now execute the script by issuing the name of the script as a command at the command prompt:

./commands.sh

Note what happens - the script behaves like a program. You have written your first shell script.


Tasks

  1. If you are going to write a lot of shell scripts, you will find it useful to have a script that makes files executable. We will create a shell script here called chex which does exactly this. The description for chex is as follows:
  2. The code for the above shell script is given below. You should create a file called chex which contains this script using the vi text-editor.
    #!/bin/sh
    # Make a file executable
    chmod u+x $1
    echo $1 is now executable:
    ls -l $1
    
  3. Make the chex file executable. You know how to do this from above.
  4. Your task is to develop two more shell scripts which are similar to chex:
    1. the first script is called chro and can be used to make a named file read-only, i.e. read-only for every user.
    2. the second script is called chrw and can be used to grant read and write permissions to the user of the file. It should also ensure that all other privileges are revoked for all other user groups.
  5. Use chex to make your new scripts executable.
  6. Submit both of your scripts using the following commands:
    cs1101submit chro
    cs1101submit chrw
    
  7. You're all done!

b.osullivan@cs.ucc.ie

Return to the CS1101 Home Page