在Bash版本4.2.47(1)-发行版中,当我尝试对来自HERE-dcoument的格式化文本进行分类时,如下所示:
cat <(fmt --width=10 <<FOOBAR
(I want the surrounding parentheses to be part of the HERE-document)
(Even the preceding unbalanced parenthesis should be part of it.
FOOBAR
) # I want this paranthesis to end the process substitution.
我收到以下错误:
bash: bad substitution: no closing `)' in <(fmt --width=10 <<FOOBAR
(I want the surrounding parentheses to be part of the HERE-document)
(Even the preceding unbalanced parenthesis should be part of it.
FOOBAR
)
我也不想引用HERE文档,即write <'FOOBAR'
,因为我仍然想在其中替换变量。
流程替代大致与此相同。
步骤#1-创建一个fifo,输出到它
$ mkfifo /var/tmp/fifo1
$ fmt --width=10 <<<"$(seq 10)" > /var/tmp/fifo1 &
[1] 5492
步骤#2-阅读FIFO
$ cat /var/tmp/fifo1
1 2 3 4
5 6 7 8
9 10
[1]+ Done fmt --width=10 <<< "$(seq 10)" > /var/tmp/fifo1
在HEREDOC中使用parens似乎也可以:
步骤#1-输出到FIFO
$ fmt --width=10 <<FOO > /var/tmp/fifo1 &
(one)
(two
FOO
[1] 10628
步骤#2-读取FIFO的内容
$ cat /var/tmp/fifo1
(one)
(two
我认为您遇到的麻烦是,流程替换(<(...)
)似乎并不关心内部的嵌套。
$ cat <(fmt --width=10 <<FOO
(one)
(two
FOO
)
bash: bad substitution: no closing `)' in <(fmt --width=10 <<FOO
(one)
(two
FOO
)
$
逃避父母的抚慰似乎有些安抚:
$ cat <(fmt --width=10 <<FOO
\(one\)
\(two
FOO
)
\(one\)
\(two
但是并没有真正给你想要的东西。使括号保持平衡似乎也令人安心:
$ cat <(fmt --width=10 <<FOO
(one)
(two)
FOO
)
(one)
(two)
每当我有复杂的字符串(例如在Bash中要使用的字符串)时,我几乎总是会首先构造它们,将它们存储在变量中,然后通过变量使用它们,而不是尝试制作一些棘手的内衬,最终导致脆弱的。
$ var=$(fmt --width=10 <<FOO
(one)
(two
FOO
)
然后打印:
$ echo "$var"
(one)
(two
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句