* On Sun, Dec 17, 2006 at 09:26:44PM +1100, john gibbons wrote:
> My needs would be pretty simple I think, Sonia.
> 
> For example, producing a self scoring questionnaire - I am a 
> semi-retired psychologist. It would be mostly text related stuff. I am 
> wary of a technical overload where I would not use much of the power of 
> a programming language anyway.

OK. As you've probably seen on this list, if you ask 2 Linux people a
question, you'll get 3 answers :-) Others have suggested using PHP and a
database - good for a bigger program but you want a simple text program,
so let's stay with shell scripting - nice and easy for beginners.

I'll give you a brief intro on how to write your program, but you'll
need to read up to get more understanding. Dave Taylor has written an
ongoing column in Linux Journal about using the shell to write a simple
blackjack card game - print out all his articles (in reverse order) from
this web page: http://www.linuxjournal.com/user/801564/track.  Also
there's a nice (longer) tutorial by Machtelt Garrels here:
http://tille.xalasys.com/training/bash/.

First step is how to store your questions. Other people suggested a
database - the right thing for experienced programmers but it'll make
things too complicated for you. Let's save your questions in a file that
looks like this:

$ cat questions.txt 
1:Which of these is a fruit? a) a brick b) an apple c) a shoe:b
2:What colour is a golf ball? a) red b) blue c) white:c

Each line in the file is a question and answer. Each line in the file is
divided up into fields, using the colon : character as a separator.
Field 1 is the question number, field 2 is the question + choices; field
3 is the answer.

Here's an example of a simple program that would print out the questions
and answers:

$ cat program
#!/bin/bash
while IFS=: read qnumber question answer ; do
    echo "Question number $qnumber"
    echo "$question"
    echo "$answer"
    echo
done < questions.txt

The first line (#!/bin/bash) says we're using bash for the script. The
second line (while IFS=: ...) says read variables in from the file
questions.txt (referenced on the last line with 'done < questions.txt').
The IFS=: says "use a : to separate out fields in the file). I then
print the variables (referred to using dollar signs) using the echo
command and double quotes "...".

Copy all the above to a file and save it. Make it executable (chmod u+x
program), then run it (./program). Voila - your first program:

$ chmod u+x program 
$ ./program 
Question number 1
Which of these is a fruit? a) a brick b) an apple c) a shoe
b

Question number 2
What colour is a golf ball? a) red b) blue c) white
c

All this is explained in lots more detail in the two web references
mentioned above. The example poker programs will show you how to ask the
user for a question, do maths to calculate a score, etc, etc. After that
you'd probably want to assign a weight to each question (in another
field), keep a running total of the score, put the answers into separate
fields, and so on and so on...

Hope this helps, good luck!

--
Sonia Hamilton. GPG key A8B77238.
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to