Jump to next element in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Jump to next element in a loop
# 1  
Old 05-25-2012
Lightbulb Jump to next element in a loop

Hi, i want the loop to move to 3rd item, when it encounters 2 while runing

Code:
for i in 1 2 3 4 5 6
do
if [ $i -eq 2 ]
then
#jump to element 3
else
echo $i
fi
done

o/p
Code:
1
3
4
5
6

can anyone help, how this can be implemented ?
# 2  
Old 05-25-2012
There isn't a command to break out an if statement, the simplest solution should be:
Code:
if [ "$i" -ne 2 ]

# 3  
Old 05-25-2012
Or use "continue":
Code:
for i in 1 2 3 4 5 6
do
  if [ $i -eq 2 ]
  then
    continue #jump to element 3
  else
    echo $i
  fi
done

This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-25-2012
Quote:
Originally Posted by Scrutinizer
Or use "continue":
Code:
for i in 1 2 3 4 5 6
do
  if [ $i -eq 2 ]
  then
    continue #jump to element 3
  else
    echo $i
  fi
done

Sure, it's an if statement within a for loop... time for coffee... Smilie
# 5  
Old 05-25-2012
thanks,
i forgot the use of continue,
i was thinking it would make the loop start from 1st again..... my bad.. sorry for spoiling your time.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print the row element till the next row element appear in a column

Hi all I have file with columns F3 pathway CPS F2 H2 H4 H5 H6 no pathway CMP H7 H8 H9 H10 My expected output is F3 pathway CPS F2 pathway CPS (10 Replies)
Discussion started by: Priyanka Chopra
10 Replies

2. Shell Programming and Scripting

jump to a section in output

HI I have the following output from an autosys command . Now I would like to investigate further .i.e Start Dependent Job Name Status ... (3 Replies)
Discussion started by: ptappeta
3 Replies

3. Shell Programming and Scripting

Find if XML element has a matching required element

I want to check if every <Part> element has corresponding <Description> in this sample XML. ....<Lot Of XML> <Inv lineNumber="2"> <Item> ... (4 Replies)
Discussion started by: kchinnam
4 Replies

4. Solaris

Jump Start vs Flash Archive

Hi I'm trying to understand the difference between installation methods of Solaris 10, Jump Start vs Flash Archive ? thx for help. (6 Replies)
Discussion started by: presul
6 Replies

5. Shell Programming and Scripting

how to jump a shell !!

Hi, can someone point out a solution to my problem below. Within a shell script TEST.ksh I have to run two commands CmdA & CmdB one after the other as below. ------------- TEST.ksh #!/usr/bin/ksh A B ------------- I am running this script from KSH with PID 12345 as below. ... (5 Replies)
Discussion started by: krishnaux
5 Replies

6. Shell Programming and Scripting

HELP unsetting array element in loop

I have a loop and I need to be able to unset the array element that I am currently accessing in it. I was thinking of making a counter that increments with the loop and doing unset $dirs but if I do that I am not sure if the other members of the array would get shifted down in index (meaning that... (2 Replies)
Discussion started by: msf5042
2 Replies

7. Shell Programming and Scripting

[bash] jump from one txt file to another

Hi all, I need to create a bash script that reads a txt file on a remote (Windows 2003) server, gets the IP-addresses out of it and then fetches the folders to copy out of another txt file. (all files are auto-generated) The IP addresses that don't have a folder_list file should be ignored. At... (31 Replies)
Discussion started by: laurens
31 Replies

8. AIX

jump in a .KSH batch

hi everyone, i need to know if is that possible insert in a batch KSH a "GO TO" sentence to jump at the top or at the end of the job. e.g. top: step1 step2 go to top: thanks in advance (4 Replies)
Discussion started by: enge
4 Replies

9. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 Replies
Login or Register to Ask a Question