Breaking out of an if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking out of an if statement
# 1  
Old 02-24-2013
Breaking out of an if statement

i have a nested if statement. i need to check for a condition (highlighted in red below). if condition is true, i need the if statement to break out of the statement and then return to the if statement section i have highlighted in blue.

is this possible?
Code:
if [ ... ] ; then
#return here
            echo "one" 
   if [ ... ] ; then
            echo "two"
      if [ ... ] ; then
             echo "three"
             if [ "$a" = "$b" ] ; then
                   break
             fi
         if [ ... ] ; then
fi
   fi
      fi
         fi

This User Gave Thanks to SkySmart For This Post:
# 2  
Old 02-24-2013
Take a look at the 'while' statement: Loop Control
# 3  
Old 02-25-2013
Your fi indentation is highly individual, unusual, and misleading, not for the shell, but for the occasional reader!
You can't break out of if constructs, only out of loops. To arrive where you are heading for, you need to reorder/rewrite the entire if- tree.
# 4  
Old 02-25-2013
You could write the if statement into a function and simply return:

Code:
function process_my_if() {
  if [ ... ] ; then
     #return here
     echo "one" 
     if [ ... ] ; then
        echo "two"
        if [ ... ] ; then
               echo "three"
               if [ "$a" = "$b" ] ; then
                     return
               fi
               if [ ... ] ; then
                  # nothing here
                  :
               fi
        fi
     fi
  fi
}


Last edited by Chubler_XL; 02-25-2013 at 11:58 PM..
# 5  
Old 02-26-2013
Quote:
Originally Posted by Chubler_XL
You could write the if statement into a function and simply return:

Code:
function process_my_if() {
  if [ ... ] ; then
     #return here
     echo "one" 
     if [ ... ] ; then
        echo "two"
        if [ ... ] ; then
               echo "three"
               if [ "$a" = "$b" ] ; then
                     return
               fi
               if [ ... ] ; then
                  # nothing here
                  :
               fi
        fi
     fi
  fi
}

looks like this could work..

will this return me to the part of the code that says "return here"?
# 6  
Old 02-26-2013
no, it will break out of function entirely.
RudiC statement above still stands.
# 7  
Old 02-26-2013
Quote:
Originally Posted by SkySmart
looks like this could work..

will this return me to the part of the code that says "return here"?
No, for that you would need a looping statement like while:

Code:
if [ ... ] ; then
   while true
   do
      #return here
      echo "one" 
      if [ ... ] ; then
         echo "two"
         if [ ... ] ; then
            echo "three"
            if [ "$a" = "$b" ] ; then
               continue
            fi
            if [ ... ] ; then
               # nothing here
               :
            fi
         fi
      fi
      break
   done
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. Shell Programming and Scripting

Breaking a pipe

Here's my code - for File in "${Files}"; do uuencode "${File}" "$(basename ${File} 2>&-)" 2>&-; done | mailx -s "subject" "a@b.c" Now I want to know if there a way to *not* send an email if the "Files" variables turns out to be blank. Any suggestions? Also, just to clarify, I... (4 Replies)
Discussion started by: nexional
4 Replies

3. UNIX for Dummies Questions & Answers

Breaking up at the second occurrence

hi, My input is: 123 1234|123|123|123 123|123|456 123|123|12 12 Expected output is: 123 1234|123 123|123 123|123 456 123|123 12 (1 Reply)
Discussion started by: pandeesh
1 Replies

4. Shell Programming and Scripting

Breaking out of loop

I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop. Let me explain with example here: I... (6 Replies)
Discussion started by: vpv0002
6 Replies

5. Shell Programming and Scripting

File breaking

Hey, I have to take one CSV file and break into more files. Let's I have a file prices.csv and the data in the file like 1,12345 1,34567 1,23456 2,67890 2,77720 2,44556 2,55668 10,44996 based on the first column, I want to create files. in this example 1 is repeated three times... (12 Replies)
Discussion started by: bond2222
12 Replies

6. Shell Programming and Scripting

Breaking up a file

Hi, I have a file that looks like this - lets call it fileA >hhm2 IIIIIIIIILLLLLLLMMMMMMMMMNNNNNNNNNNGGGGGGHHHHHHHH >hhm4 OOOOOKKKKKKKKMMMMMHHHHHLLLLLLLLWWWWWWWWWWW >hhm9 OOOOOOOIIIIIIIIIKKKKKKKKKMMMMMHHHHHHHHHHHLLLLLLLLLL So the file is pretty straight forward. The name is indicated... (2 Replies)
Discussion started by: phil_heath
2 Replies

7. Shell Programming and Scripting

Breaking line

My input file is like USER_WORK.ABC USER_WORK.DEF I want output file like ABC DEF (4 Replies)
Discussion started by: scorp_rahul23
4 Replies

8. Linux

breaking out of while loop

Hi , I am running this script ( pasting only error code ) to generate some ddl definition for tables . But what I want is to break out of the db2look part when the base_table is not like DIM_$TN or FACT_$TN . After this it should come back to while loop to read the next TN . I read the other... (3 Replies)
Discussion started by: capri_drm
3 Replies

9. HP-UX

Breaking Mirror

Can some one point this UNIX newbie to a web site or directions on the steps needed to break a mirror in HP-UNIX to change a bad hard drive. (4 Replies)
Discussion started by: egress1
4 Replies
Login or Register to Ask a Question