%20%E6%A8%A1%E5%BC%8F%E4%B8%8B%E7%9A%84%20Bash%20%E8%84%9A%E6%9C%AC%E4%B8%8D%E6%98%BE%E7%A4%BA%E2%80%9Ccat%E2%80%9D%E5%91%BD%E4%BB%A4%E7%9A%84%E5%8A%9F%E8%83%BD%E3%80%82%20%EF%BC%88%E5%85%B6%E5%86%85%E5%AE%B9%EF%BC%89.png)
在这个 bash 脚本中..它只是显示..
+cat
当它运行时。
#!/bin/bash
set -x
cat > /test << "EOF"
a
b
c
d
EOF
但我宁愿展示它所做的一切。包括
a
b
c
d
它添加到文件中。
答案1
尝试使用set -v
而不是set -x
答案2
发生这种情况是因为set -x
显示扩展的评估 - 这不是典型重定向的因素。
LC_ALL=C man set |
sed -n '/^ *-x/,/^$/p'
-x The shell shall write to standard error a
trace for each command after it expands the
command and before it executes it. It is
unspecified whether the command that turns
tracing off is traced.
来自POSIX 规范
重定向运算符
<<
和<<-
都允许重定向 shell 输入文件中包含的行,称为此处文档,到命令的输入。
正是cat
它接收输入,而不是 shell。所以没有扩张以便它在执行前报告。而且,在它交出那部分输入后,实际上就没有数据可供报告了。
这就是为什么 - 正如其他地方所建议的那样,set -v
确实有效,但-x
不起作用:
LC_ALL=C man set |
sed -n '/^ *-v/,/^$/p'
-v The shell shall write its input to standard
error as it is read.
在这种情况下,它会在读取时复制其输入stderr
- 在重定向之前 - 无论是否扩展它。
答案3
shell 中没有功能支持此操作。你可以实现这个目标具体的做的结果
tee /test << "EOF"
a
b
c
d
EOF
但这并不能完全适应您在 shell 脚本中可以执行的所有操作。
您可能想研究诸如screen
和 之类的程序Expect
。