LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-05-2008, 08:40 PM   #1
googleee
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Rep: Reputation: 0
Looping in a program.


I'm trying to create a command menu. Using a loop, i want the program to continue to execute until the user selects option 4 (exit). Im very frustrated at this point, and cannot figure how to loop it on my own. I would appreciate if someone could point me in the right direction.

heres what i have so far. Keep in mind i am a newbie to linux and im learning on my own. Now i'm not sure where to place the loop and what kind, i've tried many things, i've given up. I'm using VI.
"
!#/bin/bash
echo "Enter your name: "
read name
echo "Menu for $name"

echo "1. Display a long listing of the current directory"
echo "2. Display who is logged on the system"
echo "3. Display the current date and time"
echo "4. Quit"
echo "Enter an option: "
read answer
echo

case $answer in
1) ls -a;;
2) who;;
3) date;;
4) break;;
esac
"
 
Old 11-05-2008, 08:50 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
you would want a while loop. this page looks like it will be a good reference: http://tldp.org/LDP/Bash-Beginners-G...ect_09_02.html

ie, you will loop "while" your input variable is not equal to "4" (or whatever your exit input number corresponds to)

by the way, it is good that you included your code in your post (however even better if you use the CODE tags!). if you had not (and had not specified which scripting language, or even compiled language ie C for that matter), then your first response you would get would likely be "what language". the reason for this is that there are hundreds (if not thousands) of programming languages, and all support different constructs (ie while loop) and will have different syntax.
 
Old 11-05-2008, 09:27 PM   #3
googleee
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
I've implemented the while loop, how do i tell the loop to only loop once, and then let the user select another option? Right now, it's just continuously looping through the user selected option.

!#/bin/bash
echo "Enter your name: "
read name
echo "Menu for $name"

echo "1. Display a long listing of the current directory"
echo "2. Display who is logged on the system"
echo "3. Display the current date and time"
echo "4. Quit"
echo "Enter an option: "
read answer
echo
while [ $answer -ne 4 ]
do

case $answer in
1) ls -a;;
2) who;;
3) date;;
4) break;;
esac
done
 
Old 11-05-2008, 10:23 PM   #4
googleee
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
I figured it out. I used the 'until' loop instead. Thanks for the help, nadroj!!! I'm sure ill be back.
 
  


Reply

Tags
linux+



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
Not able to do looping in linux Amey Joshi Linux - Newbie 7 08-13-2008 03:25 AM
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 03:54 PM
login looping plaircpa Slackware 3 10-31-2005 08:10 PM
How to stop program looping? twirl Programming 5 10-10-2005 06:05 AM
[C] for looping problem wuck Programming 6 10-19-2003 04:29 PM

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

All times are GMT -5. The time now is 05:13 AM.

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