Tutorial Week 4
Compare the shell language recognised by the MSDOS command interpreter command.com
to the zsh language. How does command.com handle pipelines? Hint: see the
``Batch Processing Commands'' of the MSDOS User Guide. Would it be possible
to have an Ada-like or Pascal-like command interpreter?
command.com has ``batch'' processing commands ECHO, FOR, GOTO, IF, PAUSE, REM,
SHIFT. This is smaller than the zsh set. command.com does not have `grave'
redirection. If a .bat file is executed from within another, it does not return
to the first one on completion. command.com can use ERRORLEVEL, string equality,
EXIST and NOT in IF commands. Apart from these, they have quite similar functionality.
command.com handles pipelines by running each command sequentially from left
to right, storing output in temporary files along the way.
There already exist Ada equivalents, which are Ada interpreters with additional
bits. The strong typing of Ada means that you have to declare all types and
variables as you go, making it a bit more cumbersome.
The Unix and MSDOS command interpreters use positional arguments in most commands.
Some other command languages use keyword arguments instead. Discuss the advantages
and disadvantages of each method.
Keyword advantages: easier to understand, don't have to remember meanings of
positions. Disadvantages: more cumbersome to type.
Write a command ask which asks its arguments as a question, reads a reply and
returns an exit code of zero if the reply began with y or Y, but otherwise
returns a non-zero exit code.
echo $*
read answer
case $answer in
Y*)) exit 0;;
y*)) exit 0;;
*)) exit 1;;
esac
Write a command that behaves the same as ``rm -i'' that prompts for removal
of a file first.
if ask "remove file $i"
then
rm $i
fi
Write a shell script which takes a file name as first argument and a list of
patterns as successive arguments. The script is to perform a ``grep'' for each
pattern in turn on the file. Hint: see ``shift'' in the shell documentation.
file=$1
shift
for i
do
grep $i $file
done
Laboratory Week 4
Create shell commands for questions 4.3, 4.4 and 4.5. Test each of them by
running the command
testit Question-number filename
where Question-number is one of 4.3, 4.4 and 4.5, and the filename is the name
of the file containing your shell script.
You can customise your environment using the files .profile, .zshrc, xinitrc,
.mwmrc and .Xdefaults. What does each of these files do?
.profile - read by zsh at login time, is the startup shell script
.zshrc - a shell script read and executed by any zsh when it starts.
.xinitrc - a shell script read and executed by X when it starts
.mwmwrc - the control file for the mwm window manager
.Xdefaults - the general control file for all X applications.