i have a code which reads a file line by line using a while loop. Inside the while loop, i have certain conditions. Is there a way using which i can skip the current line and read the next line based upon the condition ? Let me be precise:
while read Line
do
//some sample conditions
a=$Line
if [ "a" == "b" ]
//i want to go to the next line from this point.
done < **inputfile**
Any help would be appreciated.
Using head to get the first lines of a stream, and tail to get the last lines in a stream is intuitive. But if you need to skip the first few lines of a stream, then you use tail “-n +k” syntax. And to skip the last lines of a stream head “-n -k” syntax.
Breaking from a while LoopUse the break statement to exit a while loop when a particular condition realizes. The following script uses a break inside a while loop: #!/bin/bash i=0 while [[ $i -lt 11 ]] do if [[ "$i" == '2' ]] then echo "Number $i!" break fi echo $i ((i++)) done echo "Done!"
Both “break” and “continue” are used to transfer control of the program to another part of the program. It is used within loops to alter the flow of the loop and terminate the loop or skip the current iteration.
break is a special built-in shell command. In the tcsh shell, break causes execution to resume after the end of the nearest enclosing foreach or while. The remaining commands on the current line are executed. Multilevel breaks are thus possible by writing them all on one line.
Use continue
http://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-continue-in-bash-loop/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With