我有这个脚本
for ((i=0 ; $i<=$c ; i++))
do
cat <<"EOF" >my_file.html
line 1
line 2
...
EOF
done
它应该在每个循环中写入一些特定的行,但我的文件是空的,我总是收到此错误
syntax error: unexpected end of file
在此循环之前,我对 cat 进行了另一种使用,与此完全相同
cat <<"EOF" >my_file.html
line1
...
line n
EOF
运行脚本后,my_file.html 包含这 n 行,但没有循环行。
最佳答案
你的语法错误。
分隔符之前有一个空格,表示此处文档的结尾。
cat <<"EOF" >my_file.html
line 1
line 2
...
EOF # Remove the leading space from this line.
此外,如果您想在循环内重定向,您可能需要附加 >>
而不是重定向 >
。
关于bash - 在 shell 中使用循环使用 cat 编写多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21825272/