[Top page] [Appendix A]

APPENDIX B - EXAMPLE SCRIPTS


1. Some examples of commands in action

read

echo How far can you count?
read first second third
echo $first 
echo $second 
echo $third

for

for x in 1 2 3 4 5 6 7 8 9
do
echo $x
echo -n "Do you want to continue: (y/n) "
read f
if test $f = n
then break
fi
done
echo Done

case

echo "Give me a letter:"
read l
case $l in
[aeiou]) echo "That's a vowel!";;
[b-df-hj-np-tv-z]) echo "That's a consonant!";;
[A-Z]) echo "I said lower case!";;
[1-9]) echo "I said a letter, not a number!";;
*) echo "What's that?" ;;
esac
echo "Thank you and goodbye."

until

n=1
until test $n = 10
do
echo $n
n=`expr $n + 1`
done

while

flag=y
while test $flag = y
do
echo Do it again?
read flag
done

if

echo -n "Give me a number: "
read n
echo
if test $n 
then
echo The number is $n
if test $n -gt 100
then	
echo That's a big number!
else
if test $n -le 100
then
echo That's a nice number!
fi
fi
if test $n = 69
then 
echo That's a bit rude!
fi
fi
echo
echo Byee!

while

while echo 'Give me  a word:'; read name
do
  echo "Hello , " $name
done

test

echo "Type something please:"
read a
if test -d $a
then echo "Thank you"
else echo "Thanks for nothing"
fi
if test $# = 3
then echo "There are three parameters"
fi

sed

Can you work out what this bourne shell command does?

sed -e "s/./&\\
/g" file_name | tr A-Z a-z | sort | uniq -c | sort -r
Try it!

2. A few useful algorithms

Incrementing a variable

n=1
until test $n = 10
do
echo $n
n=`expr $n + 1`
done

Using flags

flag=y
until test $flag =  n 
do
echo Do it again?
read flag
done
flag=y
while test $flag = "y"
do
then echo Do it again?
read flag
done

3. Using a dictionary

The following script will look up regular expressions in the Computer-Usable Version of the Oxford Advanced Learners Dictionary (CUVOALD). It was designed to be of use to crossword players, who know the number of letters in a word, and have some of the letters.

x

echo "This program looks up in the OALD words you don't know in the crossword."
echo
echo "Type your word with periods ('.') for the letters you do not know"
echo "and do not type spaces in words (e.g. busstop):"
echo "(type ^c to interrupt)"
echo
read re
echo
cut -f1 /home/gps_20/ecl6rsh/cif/ctape/oald.mitton/cuv2  \
| tr -d " " | grep "^$re$"

lookup

This script is a more complex version of the above, that makes full use of all the information in CUVOALD. See Chapter 11 for more information on the dictionary.

comment="For looking up words in the oald, giving full listing of the entry"
echo "What are you looking for:"
echo
echo "a - a word"
echo "b - a pattern that is part of a word"
echo "c - a transcription"
echo "d - a partial transcription"
echo "e - a word class tag"
echo "f - a pattern to match any of the above categories"
echo
read select
echo
echo "Type in the word or pattern:"
read re
echo
echo Looking for $re
dict="/home/gps_20/ecl6rsh/cif/ctape/oald.mitton/cuv2"
case $select in
a) grep "^$re	" $dict | more ;;
b) grep ".*$re" $dict | more ;;
c) grep "	$re	" $dict | more ;;
d) grep "	.*$re" $dict | more ;;
e) grep "$re[,$]" $dict | more ;;
f) grep $re $dict | more ;;
esac
echo
echo -n  "Press any key to continue, or CTRL-C to stop: "
read n
lookup

4. A cloze test

The following script runs a cloze test. This is a vocabulary test for language learners. The student is presented with a text from which several words are missing, and he has to guess the words form the context. This probably represents the limits to which Bourne shell scripts can be used for tasks normally done with a programmng language. It would be interesting to compare this script with a program in C or Icon. Note that the different modules (the text, the missing words and the script) are separate files. This means that more texts can easily be added by the teacher.

cloze

clear
n=1
until test $n = 10
do
echo
n=`expr $n + 1`
done
echo "                  ********************"
echo "                  **** CLOZE TEST ****"
echo "                  ********************"
echo
echo
echo '          Type CONTROL-C to exit at any time'
sleep 3
echo
until test ${name.tmp} 
do
echo -n 'Please type in your name: '
read name.tmp
done
echo
until test $text
do 
echo 'Please choose a text. Type one of the following:'
ls *.txt | sed "s/\.txt//"
echo
echo -n ': '
read text
done
cp ${text}.txt $HOME/$name.tmp
clear
cat $HOME/$name.tmp
echo
echo "You must guess the missing words."
echo "Read the entire text, then press return."
echo "-----------------Press return-----------------"
read rubbish
n=1
until test $n = 9
do
ans=`head -$n ${text}.sol | tail -1`
flag=
until test $flag
do	
clear	
cat $HOME/$name.tmp	
echo
echo -n "Guess word number ${n} (just type RETURN to give up): " read guess	
if test $guess	
then sleep 1 
else echo 'The answer is ' $ans ; break fi	
if test $guess = $ans
then echo Right! ; sleep 1 ; flag=y else echo 'Wrong!' ; sleep 2 ; continue fi
done
sed "s/\[$n\]/$ans/" $HOME/$name.tmp > tmp.$$ mv tmp.$$ $HOME/$name.tmp
n=`expr $n + 1`
done

UK.txt

 The UK is one of the world's great trading powers and financial
centers, and its economy [1] among the four largest in Europe. The
Thatcher government halted the expansion of welfare measures and
promoted extensive reprivatization of the government economic sector.
Agriculture is intensive, highly mechanized, and efficient by European
standards, producing about 60% of food [2] with only 1% of the labor
force. Industry is a mixture of public and [3] enterprises, employing
about 27% of the work force and generating 22% of GDP. The UK is an
energy-rich nation with large coal, natural gas, and oil reserves;
primary energy production accounts for 12% of GDP, one of the highest
shares of any industrial nation. In mid-1990 the economy fell into [4]
after eight years of strong economic expansion, which had raised
national output by one quarter. Britain's inflation rate, which has
been consistently well [5] those of her major trading partners, is
expected to decline in 1991. Between 1986 and 1990 unemployment fell
from 11% to about 6%, but it is now [6] rapidly because of the economic
slowdown. As a major trading nation, the UK will continue to be greatly
affected by world boom or [7], swings in the international oil market,
productivity trends in domestic industry, and the terms on which the
economic integration of [8] proceeds. 

UK.sol

ranks
needs
private
recession
above
rising
recession
Europe
[Top page] [Appendix A]