Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script continue keyword not breaking loop iteration

I am following a series of tutorials to learn Bash shell script. One of the exercises is to loop through files in the current directory and search for a pattern in those files. If the pattern is found then the script should sum the file size of those files.

#!/bin/sh
patern=echo
totalSize=0

for file in * 
do
    [ ! -f $file ] && continue  

    if grep $patern $file > /dev/null
    then    
        echo "pattern matched in $file" 
        echo "file size is `stat -c%s $file`"
        fileSize=`stat -c%s $file`

        totalSize=`expr $totalSize + $fileSize`
        echo "size so far is $totalSize bytes"
        echo                                                    
    fi
done

I have one other folder in the directory from which I am running the script. The folder is called "somedir". It is empty. I have took a snippet of output text pasted below. The middle 3 lines show when the loop iterates over the directory "somedir" which it prints as "sum_files"- my script name along with a file size.

I don't understand this behaviour. How can an empty directory have a file size?
But my main concern is as to why the continue keyword is not stopping the loop iteration. The output is showing that the script is running the below if statement containing the grep command even though it should stop if a directory is found.

I have verified that the test command[ ! -f $file ] does in fact return 0 when the loop gets to the directory and thus the && continue should be invoked. So why does the program continue with the rest of the loop code and try to grep the directory rather than just skipping the loop iteration at continue as expected? I know this is rather trivial but would like to know what's going on here.

Output text

pattern matched in retirement
file size is 396
size so far is 6385 bytes

pattern matched in sum_files
file size is 398
size so far is 6783 bytes

pattern matched in tp0
file size is 164
size so far is 6947 bytes

like image 560
Drok Avatar asked Sep 20 '15 14:09

Drok


People also ask

How do you continue a for loop in bash?

Using Bash Continue with a for LoopUse the continue statement inside a conditional if to control the flow of a for : #!/bin/bash for i in {1.. 10} do if [[ $i == '9' ]] then echo "Number $i!" continue fi echo "$i" done echo "Done!"

How do you skip the current iteration in for loop shell script?

The break command terminates the loop (breaks out of it), while continue causes a jump to the next iteration of the loop, skipping all the remaining commands in that particular loop cycle. The break command may optionally take a parameter.

How do you break a loop in bash?

Use 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!"

What is continue in shell script?

The continue statement is similar to the break command, except that it causes the current iteration of the loop to exit, rather than the entire loop. This statement is useful when an error has occurred but you want to try to execute the next iteration of the loop.

What is the use of break and continue in Bash?

In Bash, break and continue statements allows you to control the loop execution. The break statement terminates the current loop and passes program control to the command that follows the terminated loop. It is used to exit from a for, while, until , or select loop. s The syntax of the break statement takes the following form:

How do you break a loop in Bash?

Bash break Statement#. The break statement terminates the current loop and passes program control to the command that follows the terminated loop. It is used to exit from a for, while, until , or select loop. s The syntax of the break statement takes the following form: break [n] Copy.

What are break and continue statements in Python?

Break and continue statements are bash builtin and used to alter the flow of your loops. This concept of break and continue are available in popular programming languages like Python. The break statement will exit out of the loop and control is passed to the next statement in the loop.

How to use the “ continue” clause in bash code?

Let’s start the Bash code with the addition of bash extension i.e. “#!/bin/bash”. We have been using the “for” loop here to utilize the “continue” clause in it further. The loop will start from 1 and end at value 18 with an increment of 2 at each iteration. On increment, we will achieve 3, 5, 7, 9, 11, 13, 15, and 17.


2 Answers

continue continues the loop, as if it reached its bottom.

To break out of the loop use break.

More on bash scripting here: http://tldp.org/LDP/abs/html/index.html

like image 65
alk Avatar answered Sep 28 '22 09:09

alk


As to your question about how a empty directory can have a size: directories technically just files themselves, and in order to establish themselves as directories they need some data to let the file system know that.

enter image description here

like image 27
Nasty Sushi Avatar answered Sep 28 '22 08:09

Nasty Sushi