Bash 脚本 continue 关键字不会破坏循环迭代

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32680285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:35:38  来源:igfitidea点击:

Bash script continue keyword not breaking loop iteration

linuxbashloopscontinue

提问by Drok

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.

我正在学习一系列教程来学习 Bash shell 脚本。练习之一是遍历当前目录中的文件并在这些文件中搜索模式。如果找到该模式,则脚本应该对这些文件的文件大小求和。

#!/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.

我在运行脚本的目录中有另一个文件夹。该文件夹称为“somedir”。它是空的。我在下面粘贴了一段输出文本。中间 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. 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 && continueshould 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.

我不明白这种行为。空目录如何具有文件大小?
但我主要关心的是为什么 continue 关键字没有停止循环迭代。输出显示脚本正在运行包含 grep 命令的以下 if 语句,即使它在找到目录时应该停止。

我已经验证,[ ! -f $file ]当循环到达目录时,测试命令实际上确实返回 0,因此&& continue应该调用 。那么为什么程序继续执行其余的循环代码并尝试 grep 目录,而不是像预期的那样在 continue 时跳过循环迭代?我知道这很微不足道,但想知道这里发生了什么。

Output text

输出文本

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

退休
文件大小匹配的模式是 396
大小到目前为止是 6385 字节

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

在 sum_files
文件大小中匹配的模式是 398
大小到目前为止是 6783 字节

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

tp0
文件大小匹配的模式是 164
大小到目前为止是 6947 字节

回答by alk

continuecontinues 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

回答by Nasty Sushi

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

在此处输入图片说明