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.
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
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!"
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.
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!"
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.
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:
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.
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.
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.
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
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.
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