How to Return to a While Loop From The Bottom of Code?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Return to a While Loop From The Bottom of Code?
# 1  
Old 05-04-2013
How to Return to a While Loop From The Bottom of Code?

Hello All,

I was wondering how after processing a program down to the bottom of the program one can return to the only while loop in the program.

Thanx,
# 2  
Old 05-04-2013
Using a Bourne type shell, it is not possible to set a goto to a specific arbitrary point in a shell script. Of course, you can use continue within a loop to return to the top of the loop. But Bourne type shells do not have the goto statement.

One alternative is make part of the script a function. Later in the script, you can call the function again. But that does not return you to a previous point in the script. It just calls the function again. Functions are useful.

Another alternative is to have the shell script call itself, with some argument that makes it skip down to the part of the script where the while loop starts. That sounds complicated and problematic to me.

Another alternative is to copy and paste the while loop code, and other logic, as needed, at the point you would like to put the goto. This would work, just be kind of lengthy.

Finally, csh and tchs (neither of which I use) do have the goto statement. Smilie
# 3  
Old 05-05-2013
Thanx for the response. What I had to do was to wrap the code up in a while loop and use a continue at the bottom so that the program continues from the top of the loop again until the user makes a keyboard entry to exit. I appreciate the direction provided, it helped me decide on what approach to take.
# 4  
Old 05-05-2013
You could try something like this:
Code:
echo enter y/n
while read answer
do
  case $answer in 
    [yY]) echo yes; break ;;
    [Nn]) echo no ; break ;;
  esac
done

# 5  
Old 05-05-2013
You may want to split your single while loop into two: one to ask for and evaluate the user input (as scrutinizer posted), and a second, inner loop for your "real" code.
# 6  
Old 05-06-2013
Quote:
Originally Posted by RudiC
You may want to split your single while loop into two: one to ask for and evaluate the user input (as scrutinizer posted), and a second, inner loop for your "real" code.
Yes, I do have other while loop in there now as well as a case structure.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return multiple values using for loop in perl

I am using a for loop to copy files from say DIR1 and DIR2 to DIR3.I have to check whether files are copied from DIR1 and DIR2 and print the respective message. @path=("$DIR1","$DIR2"); foreach (@path) { $rc=system("cp $_/*xml $DIR3"); if ($rc == 0) { print "Files were copied... (1 Reply)
Discussion started by: liyakathali
1 Replies

2. Shell Programming and Scripting

How could I use the value of return code

Hello, I am woring on a script where I am getting strange situation.This script actually fetch the source code and tar that code and send to NAS location.This code resides in MKS tool...and we are fetching the source code on checkpoint label basis and script is working fine.First it synch the... (0 Replies)
Discussion started by: anuragpgtgerman
0 Replies

3. Shell Programming and Scripting

return code help

Hello folks, I have a question that if i type ls command and type echo $? it always show "0", how i could do this change that when i type ls it will show me 1, actually i want to change the return code of commands from 0 to 1. Thanks Bash (5 Replies)
Discussion started by: learnbash
5 Replies

4. Shell Programming and Scripting

Need help with return code 1...

Hi Guys,, I am having a unix script which is running the DB2 Insert command. For the insert command, there were no records to be updated. SQL0100W No row was found for FETCH, UPDATE or DELETE; or the result of a query is an empty table. SQLSTATE=02000 + + echo 1 STAGE_RC=1 + ] ... (6 Replies)
Discussion started by: mac4rfree
6 Replies

5. Shell Programming and Scripting

Pick up the return code for every iteration and display the result only once in loop.

Hi All, I amlearning UNIX scripting. I have a small query. I would be thankful if any one helps me out. I have a below piece of code which delets the files. If file dosent have the permissions to delete a particular file I have used 2>>operator to track the error code. But my objective is... (1 Reply)
Discussion started by: manas6
1 Replies

6. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

7. Shell Programming and Scripting

asking about return code

hi all my system is linux red hat i have a script that runs some object . the object return some code to the system i see the code by writing echo $? i want to ask in the script if $? equals 14 how shell is do that in the script thanks (3 Replies)
Discussion started by: naamas03
3 Replies

8. UNIX for Advanced & Expert Users

return code of a process

two programs A and B writting in c++ I am using A to B and I want to know the return code of B. in B ------------------------ int main() { return 11; } ------------------------ in A ------------------------ int main() { system(A); } ------------------------ Is it the right way... (1 Reply)
Discussion started by: filedeliver
1 Replies

9. UNIX for Advanced & Expert Users

Return code from PL/SQL Code

Hi Guys, I was just wondering if anybody can help me with this problem. OK, how we can get a value back from PL/SQL Script (not stored procedure/function) See the below example: (for example aaa.sh) #!/bin/ksh VALUE=`sqlplus -s user/password@test_id <<EOF @xxx.sq EOF` echo $VALUE ... (7 Replies)
Discussion started by: Shaz
7 Replies

10. Shell Programming and Scripting

return code from oracle

I am writing a unix script that logs into oracle and does a count on a table. I want to return that count to my unix script. this is my script: #!/bin/ksh typeset retcode; numin=0 sqlplus -s <<-EOSQL myuser/mypass@mydb variable ret_val number; begin ... (3 Replies)
Discussion started by: lesstjm
3 Replies
Login or Register to Ask a Question