简体   繁体   English

Bash脚本继续关键字不破坏循环迭代

[英]Bash script continue keyword not breaking loop iteration

I am following a series of tutorials to learn Bash shell script. 我正在学习一系列教程来学习Bash shell脚本。 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". 该文件夹名为“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. 中间的3行显示循环遍历目录“somedir”,它打印为“sum_files” - 我的脚本名称以及文件大小。

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. 但我主要担心的是为什么continue关键字不会停止循环迭代。 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. 输出显示脚本正在运行包含grep命令的if语句,即使找到目录也应该停止。

I have verified that the test command [ ! -f $file ] 我已经验证了测试命令[ ! -f $file ] [ ! -f $file ] does in fact return 0 when the loop gets to the directory and thus the && continue should be invoked. [ ! -f $file ]实际上在循环到达目录时返回0,因此应该调用&& continue 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? 那么为什么程序继续使用其余的循环代码并尝试grep目录而不是仅按预期跳过循环迭代? 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 文件大小为396
size so far is 6385 bytes 到目前为止的大小是6385字节

pattern matched in sum_files sum_files中匹配的模式
file size is 398 文件大小为398
size so far is 6783 bytes 到目前为止的大小是6783字节

pattern matched in tp0 模式在tp0中匹配
file size is 164 文件大小为164
size so far is 6947 bytes 到目前为止的大小是6947字节

continue continues the loop, as if it reached its bottom. continue循环,就像它到达底部一样。

To break out of the loop use break . 要打破循环使用break

More on bash scripting here: http://tldp.org/LDP/abs/html/index.html 有关bash脚本的更多信息,请访问: 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. 至于你关于空目录如何具有大小的问题:目录技术上只是文件本身,并且为了将自己建立为目录,他们需要一些数据让文件系统知道。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM