简体   繁体   English

Bash中的<<<和<<()是什么?

[英]What are <<< and < <() used for in Bash?

I'm studying bash but I'm not able to understand this example code: 我正在研究bash,但无法理解以下示例代码:

while IFS= read -r line2; 
    do
        if [[ "$line1" == "$line2" ]]
        then
            (( i++ ))
        fi
    done <<< "$lines"

in particular the line done <<< "$lines" what do mean <<< ? 特别是done <<< "$lines"的行done <<< "$lines"是什么意思<<<

I find other example for example this: 我找到其他示例,例如:

while read line
  do
      echo "Word count per line: $line"
  done < <(cat list.txt | wc -w)

in this case because it uses < < and not <<< 在这种情况下,因为它使用< <而不是<<<

<<< specifies a here string . <<<指定此处的字符串

In your case, the content of $lines is sent to the standard input of the while loop. 在您的情况下, $lines的内容将发送到while循环的标准输入。


<(...) is a kind of process substitution . <(...)是一种过程替换

In your case, the output of cat list.txt | wc -w 在您的情况下, cat list.txt | wc -w的输出cat list.txt | wc -w cat list.txt | wc -w is sent to the standard input of the while loop. cat list.txt | wc -w发送到while循环的标准输入。

Process substitution is very useful when a program expects a file name as an argument. 当程序需要文件名作为参数时,进程替换非常有用。

Whenever you write a loop, you can feed it with the content of a file with the expression: 每当编写循环时,都可以使用以下表达式将其与文件内容一起馈入:

while ...; do
   # things
done < file

Then, you can take advantage of this by using a process substitution instead of a file itself. 然后,您可以通过使用进程替代而不是文件本身来利用此优势。 This way, you can feed a while loop with the result of a process without having to pipe. 这样,您无需处理就可以将while循环提供给进程结果。 Internally, it kind of creates a temporary file: 在内部,它会创建一个临时文件:

while ...; do
   # things
done < <(find -type f -name ".htaccess")

But, why is it important to use this approach instead of cat file | wc -w | while ... 但是,为什么使用这种方法而不是cat file | wc -w | while ...很重要? cat file | wc -w | while ... cat file | wc -w | while ... ? cat file | wc -w | while ... Because this one will open a sub-shell, so that whatever variables you set within the while loop will be lost once it finishes. 因为此操作将打开一个子外壳,所以您在while循环中设置的任何变量一旦完成将丢失。

For example, if you say: 例如,如果您说:

$ var=5
$ seq 10 | while read v; do var=$v; done

Then the variable $var is not set to 10 at the end of the loop, but it remains unchanged: 然后变量$var在循环结束时未设置为10 ,但保持不变:

$ echo $var
5

You can read more about it in I set variables in a loop that's in a pipeline. 您可以在管道循环中设置变量,以了解更多信息 Why do they disappear after the loop terminates? 为什么它们在循环终止后消失了? Or, why can't I pipe data to read? 或者,为什么我不能通过管道读取数据? .


Then, you have: 然后,您将:

while ...; do
   # things
done <<< "$variable"

This feeds the while loop with a here string . 这将为while循环提供一个here字符串 That is, the while loop keeps reading from the content of $variable until it is done. 也就是说, while循环会一直从$variable的内容读取内容,直到完成为止。

For example, the following code: 例如,以下代码:

while IFS=: read -r product price; do
   echo "$product cost $price euros"
done <<< "potatos:23
kiwis:45"

Will return: 将返回:

potatos cost 23 euros
kiwis cost 45 euros

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

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