LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 10-18-2007, 06:24 PM   #1
bourne
Member
 
Registered: Feb 2006
Location: Toronto, Ontario
Distribution: Fedora 10, centos
Posts: 181

Rep: Reputation: 30
help with bash scripting


Hello. I am currently attempting to write a script and I have bits and pieces of it working. I have a few questions regarding practices just to see if I am on the right track. I am new to scripting so you will have to bare with me.

both my question have to do with while statements:

1. I currently have if elfi fi statements within a single while statement. I am just wondering if this is ok to do? As in, will this cause me issues down the road? Or is it better to set it up another way?

2. If you have a while statement, and the purpose of the while statement is to validate user input, if the user inputs the correct information without trigger any errors the loop should end right? If the script stays in the loops, meaning a blank line with a flashing cursor just shows up instead what is supposed to come after the loops, what could be the cause of the loop not ending properly?

thanks in advance
todd
 
Old 10-18-2007, 06:39 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by bourne View Post
Hello. I am currently attempting to write a script and I have bits and pieces of it working. I have a few questions regarding practices just to see if I am on the right track. I am new to scripting so you will have to bare with me.

both my question have to do with while statements:

1. I currently have if elfi fi statements within a single while statement. I am just wondering if this is ok to do? As in, will this cause me issues down the road? Or is it better to set it up another way?
show how you do that.
Quote:
2. If you have a while statement, and the purpose of the while statement is to validate user input, if the user inputs the correct information without trigger any errors the loop should end right? If the script stays in the loops, meaning a blank line with a flashing cursor just shows up instead what is supposed to come after the loops, what could be the cause of the loop not ending properly?

thanks in advance
todd
I method i always use for this kind of user input task is :

Code:
while [True]
    ask the user for input
    check input
    if ok
        do something
    if input is quit
        exit out of loop
 
Old 10-18-2007, 07:44 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
"While" is a loop construct. Anything that is legal code can be inside a loop. You could have 100 sets of if/else(or elif)/fi or similar statements.

The program exits the while loop only if the test expression changes or with something like a "break" command. I don't think an error in user input will cause an exit from the loop unless there is code for that purpose.
 
Old 10-21-2007, 12:25 PM   #4
bourne
Member
 
Registered: Feb 2006
Location: Toronto, Ontario
Distribution: Fedora 10, centos
Posts: 181

Original Poster
Rep: Reputation: 30
Thanks for your response guys. I am sorry its taken me so long to respond. I have been really busy with school and unable to reply. I managed to get my loop working fine, my problem was as you mentioned, I did not have a 'break' statement anywhere in my loop. Once I figured out where that needed to be placed my loop looped perfectly. Also I ended up sticking with the 'elif' statements. They appear to have worked fine I used almost exactly the setup you suggested I believe.

I just wanted to thank you guys for responding and I apologize I wasn't able to get back to you earlier

thanks again
todd
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash scripting csdhiman Linux - Server 3 10-12-2007 12:15 AM
new to bash scripting peok Programming 2 07-15-2006 02:46 AM
bash scripting qu drigz Programming 12 06-15-2004 02:09 AM
Bash scripting JonCooperUK Programming 3 03-04-2004 08:55 PM
About bash scripting pazvant Programming 3 10-20-2003 11:12 AM


All times are GMT -5. The time now is 10:32 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration