ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi friends..
i newbiee to shell scripting..and i want to write a script...when i ran the script then it will ask questions one by one with mentioned options like a,b,c,d...atlast..it needs to display result also....
i dont no how to start and where to start...
pleas help me..
waiting for your valuable replies
It is a basic task for shell scripting. You definitively have to read a good tutorial, like that one suggested above. Take a look at the read statement. Using the -p option you can prompt the user with a question and offer some choice. An example:
Code:
#!/bin/bash
read -p "Which fruit would you like?
a. strawberry
b. apple
c. banana
d. apricot
Do your choice: " ans
case $ans in
a) fruit=strawberry ;;
b) fruit=apple ;;
c) fruit=banana ;;
d) fruit=apricot ;;
esac
echo "You chose $fruit"
thank you...thanks alot...
suppose i have 100 questions..and should i need to write all the questions
or can i use any database to store all the questions..
If you have the questions stored in a text file, you can embed the code in a while loop, which reads from a file line by line, for example:
Code:
while read line
do
read -p "$line" ans
<other commands here>
done < file.txt
where file.txt is the file containing the questions, one per line. If you have multiple lines as in the example of my previous post, you have to find a way to parse the file and read some lines together. It all depends on the format of the file itself. If you post a real example, we can give more advices.
thanks for your reply..
here is my code and corresponding output..
the problem is after entering any option only it has to go to next questions..but it is displaying continuesly..please take a look
Code:
while read line
do
echo $line
done < questions.txt
output is..
Code:
srinivas@opensolaris:~$ ./tryingscript.sh
capital of Hungery
a.budapest
b.paris
c.pitsburgh
d.i dont no
your choice:
The longest river in the world
a.ganga
b.nile
c.bramhaputra
d.dont no
your choice :
Niagara Falls was discovered by
a.jhonny
b.Louis Hennepi
c.kempson
d.adrew heko
your choice:
srinivas@opensolaris:~$
hi friends..
i newbiee to shell scripting..and i want to write a script...when i ran the script then it will ask questions one by one with mentioned options like a,b,c,d...atlast..it needs to display result also....
i dont no how to start and where to start...
pleas help me..
waiting for your valuable replies
Sounds like homework to me ... Did you try Googling???
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.