continue
: Skips the rest of the current loop iteration and proceeds with the next iteration.break
: Exits the loop immediately.
Here are examples demonstrating the use of continue
and break
in each type of loop:
#!/bin/bash
for file in "$PWD"/*
do
if [[ $file == *".skip" ]]; then
continue # Skip files with ".skip" extension
fi
echo "Processing file: $file"
if [[ $file == *".stop" ]]; then
break # Stop processing if file has ".stop" extension
fi
done
#!/bin/bash
files=("$PWD"/*)
for ((i=0; i<${#files[@]}; i++))
do
if [[ ${files[i]} == *".skip" ]]; then
continue # Skip files with ".skip" extension
fi
echo "Processing file: ${files[i]}"
if [[ ${files[i]} == *".stop" ]]; then
break # Stop processing if file has ".stop" extension
fi
done
#!/bin/bash
files=("$PWD"/*)
i=0
while [ $i -lt ${#files[@]} ]
do
if [[ ${files[i]} == *".skip" ]]; then
((i++))
continue # Skip files with ".skip" extension
fi
echo "Processing file: ${files[i]}"
if [[ ${files[i]} == *".stop" ]]; then
break # Stop processing if file has ".stop" extension
fi
((i++))
done
#!/bin/bash
files=("$PWD"/*)
i=0
until [ $i -ge ${#files[@]} ]
do
if [[ ${files[i]} == *".skip" ]]; then
((i++))
continue # Skip files with ".skip" extension
fi
echo "Processing file: ${files[i]}"
if [[ ${files[i]} == *".stop" ]]; then
break # Stop processing if file has ".stop" extension
fi
((i++))
done
#!/bin/bash
select file in "$PWD"/*
do
if [[ $file == *".skip" ]]; then
continue # Skip files with ".skip" extension
fi
echo "Processing file: $file"
if [[ $file == *".stop" ]]; then
break # Stop processing if file has ".stop" extension
fi
done
In these examples, the continue
statement is used to skip processing files with the .skip
extension, and the break
statement is used to stop the loop if a file with the .stop
extension is encountered.