LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2009, 07:42 AM   #1
srinivasmiriyalu
Member
 
Registered: Mar 2009
Location: Banglore-India
Distribution: RHEL5.2,Fedora9&11,Opensolaris 5.11,SXDE 1/08,CentOS-5.3,Ubuntu 8.10
Posts: 149

Rep: Reputation: 16
need help...shell scripting...


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
 
Old 04-16-2009, 07:54 AM   #2
Zmyrgel
Senior Member
 
Registered: Dec 2005
Location: Finland
Distribution: Slackware, CentOS, RHEL, OpenBSD
Posts: 1,006

Rep: Reputation: 37
You can make that kind of script easily with the help of a good tutorial.
 
Old 04-16-2009, 08:16 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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"
 
Old 04-16-2009, 09:35 AM   #4
srinivasmiriyalu
Member
 
Registered: Mar 2009
Location: Banglore-India
Distribution: RHEL5.2,Fedora9&11,Opensolaris 5.11,SXDE 1/08,CentOS-5.3,Ubuntu 8.10
Posts: 149

Original Poster
Rep: Reputation: 16
need help... shell scripting...

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..
 
Old 04-16-2009, 11:04 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 04-17-2009, 08:22 AM   #6
srinivasmiriyalu
Member
 
Registered: Mar 2009
Location: Banglore-India
Distribution: RHEL5.2,Fedora9&11,Opensolaris 5.11,SXDE 1/08,CentOS-5.3,Ubuntu 8.10
Posts: 149

Original Poster
Rep: Reputation: 16
n

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:~$
 
Old 04-17-2009, 08:37 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
while read line
do
echo $line
done < questions.txt
What is the content of questions.txt? You missed the read answer part to get user's response. You just echo the current line.
 
Old 04-23-2009, 06:37 AM   #8
jisjis
LQ Newbie
 
Registered: Apr 2009
Posts: 18

Rep: Reputation: 0
Quote:
Originally Posted by srinivasmiriyalu View Post
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???

Linux

Last edited by jisjis; 04-26-2009 at 04:45 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-26-2008 11:50 PM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 08:18 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 03:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration