Lil*_*lás 1 bash io-redirection
在代码块语句中添加重定向而不是在它的末尾添加它有什么区别吗?
例如,有什么区别:
if cat <<< foo; then
code ...
fi
Run Code Online (Sandbox Code Playgroud)
和:
if cat; then
code ...
fi <<< foo
Run Code Online (Sandbox Code Playgroud)
?
提前致谢
这里:
if cat; then
code ...
fi <<< foo
Run Code Online (Sandbox Code Playgroud)
... stdin被修改为整个块.因此,code ...运行时stdin从此处连接到管道,并且无法从脚本的原始stdin中读取.
而在这里:
if cat <<< foo; then
code ...
fi
Run Code Online (Sandbox Code Playgroud)
...重定向的范围限定为一个cat命令,并且code ...运行时stdin连接到其原始源.
另外值得注意的是,如果你的块是一个循环,while cat <<< foo; do code ...; done会读取一个新的heredoc,它包含foo在每次迭代中,而while cat; do code ...; done <<<foo在整个迭代过程中读取单个heredoc(意味着,在这个特定的例子中,除了首先会发现输入源已耗尽).考虑BashFAQ#1的一些例子,这些区别很重要.
| 归档时间: |
|
| 查看次数: |
32 次 |
| 最近记录: |