LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with bash scripting (https://www.linuxquestions.org/questions/programming-9/help-with-bash-scripting-592870/)

bourne 10-18-2007 06:24 PM

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

ghostdog74 10-18-2007 06:39 PM

Quote:

Originally Posted by bourne (Post 2929018)
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


pixellany 10-18-2007 07:44 PM

"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.

bourne 10-21-2007 12:25 PM

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


All times are GMT -5. The time now is 04:36 PM.