Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi all,
i'm currently on a unix course and i have been set a task to create a shell script in the korn shell.
the program has to:
-take a students name
-the group they are in (one of A, B , C)
-there tutor (one of Mr. White, Mrs. Smith or Mr. Brown)
-the date and time they entered the information.i have created a file called info that this will be stored in and i know i have to use the following line at the start of the script:
#!/bin/kshdoes anyone know how to start this??
any help very much appriciated,
Sarah.

Ok so far we have:
#!/bin/ksh
echo "Please enter your name"
read nameGood so far, though I may suggest trying:
read name?"Enter Your Name: "Looks a lot nicer as you enter the information after the : You can do this with all the fields. So you write the rest of it, post it here, the we can help out with the storing of said information.
=) Good luck!

Whoops, missed the choices =)
For that you cant really use the above example I posted. Try echo'ing all the choices then just reading the input.

I GOT THIS SO FAR:
WILL IT WORK OR AM I JUST ANOTHER DUMB BLONDE?
#!/bin/ksh
echo “Please enter your first name – “
read firstecho “Please enter your surname – “
read surnameecho “Hello $first $surname, please enter your group – “
read groupcase $group in
A | B | C)
Echo “Group accepted”
;;
*)
echo “Error in group data”
exit
;;
esacecho “Please enter your Tutor’s name – (initial and surname)”
read tutorcase $tutor in
J.Rosbottom | V.Wong | S.Walters)
Echo “Tutor confirmed as $tutor”
;;*)
echo “Invalid tutor name”
exit
;;
esacecho “What project activities have you undertaken in the last week? – “
read activitiesecho “Please enter date of meeting – “
read meeting
echo “Please enter time of meeting - ”
read time
echo “Please enter place of meeting - ”
read placeecho “Confirmation of meeting on date $meeting at time $time and place $place Y/N ”
read ans
if [$ans = “N”] then
echo “Meeting information incorrect, please re-start”
exit
else
echo “Meeting information confirmed”
fi--------------
AND THIS TOOK ME ALL DAY!!
ANY CHANGES LET ME KNOW.THANKS
SARAH

First and surname are fine.
The case statements need a little work though.
The format for a case statement is something alone the lines of:
echo "A) Choice 1"
echo "B) Choice 2"
echo "C) Choice 3"
read makechoicecase $makechoice in
a|A)
actualchoice="Choice 1"
echo "Group Accepted"
;;
b|B)
actualchoice="Choice 2"
echo "Group Accepted"
;;
c|C)
actualchoice="Choice 3"
echo "Group Accepted";;
*)
echo "Invalid choice!"
;;
esacSo it echoes the choices, then it accepts only a/A b/B c/C as a choice. I then takes that information (case $option in) and determines what to do with it
a)
echo "xyz"
;;I whipped up a quick script that is a bit advanced but really uses just basic scripting ideas. If you have any questions regarding it, please feel free to e-mail me at the above address. There are some comments in there to guide ya along.
See the script at:
http://lankrypt0.com/testscript.htmlNow you must promise not to just take this and use it, it is meant as a learning tool only! Seriously.

thank you,
i'm in the middle of re-doing those case studies (can't believe i was that far out).
the script you wrote is very advanced but i understand a little more on how unix works, thanks. i previously only used ada and pascal and they seem alot different to what unix is.
after i have sorted my case statements out, how do i append the information with date to a file? do i use the >> notation i.e.
echo $name >> studentdata.log
will this append the data to the student log file?
thanks for all your help,
sarah

i think i got it done, all i have to do now is a search using the grep command but i think i should be o.k with that. Then after that its the write up (should be easy enough)
thank you again for your help LANkrypt.
I think i learnt more about unix from you than i did in my tutorials.
sarah xx

No problem, we all need to help each other out =)
Using >> will append whereas > will overwrite, so you want to use the >>. If you need some more help, shoot me an e-mail.

Instead of checking makechoice for upper and lowercase variations, declare it an uppercase var in the first place:
typeset -u makechoice
Also I agree with LK about streamlined prompts:
read name?"Enter Your Name: "
not
echo "Enter your name: "
read nameAlso strictly speaking [] and echo are not ksh. ksh has [[ ]] and print.

#!/bin/ksh
STUDENTLOG="studentdata.log"
if [ ! -f $STUDENTLOG ] ; then
touch $STUDENTLOG
fi
IFS=":"
read name?"Enter Your Name: "
echo "$name, please select your group."
PS3='Group? '
select group in A B C quit ; do
case $REPLY in
1 | 2 | 3 ) break ;;
q | 4 ) exit ;;
* ) echo 'invalid' ;;
esac
done
echo "$name, please select your tutor."
PS3='Tutor? '
select tutor in "Mr. White" "Mrs. Smith" "Mr. Brown" quit; do
case $REPLY in
1 | 2 | 3 ) break ;;
q | 4 ) exit ;;
* ) echo 'invalid' ;;
esac
done
set -x
# echo "$name $group $tutor"
if [ `grep -c "^$name:" $STUDENTLOG 2>/dev/null` -gt 0 ] ; then
mv $STUDENTLOG ${STUDENTLOG}.old
sed -e 's/^$name.*/$name:$group:$tutor:`date +%Y%m%d_%H:%M:%S`/' ${STUDENTLOG}.old > $STUDENTLOG
else
echo "$name:$group:$tutor:`date +%Y%m%d_%H:%M:%S`" >> $STUDENTLOG
fi

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |