![]() |
CS1101: Laboratory 4(a)An Introduction to Shell ScriptingAcademic Year 2006-2007Lecturer: Dr. Barry O'SullivanDepartment of Computer Science University College Cork |
To familiarise students with the concepts of shell scripts.
This lab is based on material from Paul Anderson's book entitled "Just Enough UNIX".
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
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.
chex
which
does exactly this.
The description for chex
is as follows:
chex
- change a file to be executable.
chex <filename>
chex
:
sh
chmod u+x
to the file named as argument ($1
)
file
is executable
ls -l
to show the file's modes
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
chex
file executable. You know how to do this
from above.
chex
:
chro
and can be used to make
a named file read-only, i.e. read-only for every user.
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.
chex
to make your new scripts executable.
cs1101submit chro cs1101submit chrw
Return to the CS1101 Home Page