权限被拒绝 - 多行输出到文件
Permission denied - multiline output to file
我尝试向文件生成多行输出,但出现权限被拒绝错误。你知道错在哪里吗?
#!/bin/bash
something.html << END # Permission denied
<tag></tag>
Some text
more tags
more text
END
虽然 ... > file
(... >> file
) - output 重定向 - 可用于输出(通过附加)到 file
,但有没有对称file < ...
构造在POSIX-like shell中,例如Bash.
相反,输入重定向<
/<<
/<<<
隐式 向 stdin - from a here-document (<<
) 提供输入,在这种情况下。
你需要一个命令来读取标准输入以便处理它,即使只是将它发送到一个文件。
在后一种情况下,您 将 cat
与 输出 重定向 相结合: cat
只是将 stdin 输入复制到 stdout,这然后输出重定向发送到指定的文件:
#!/bin/bash
cat > something.html << END # cat with > sends stdin input to a file
<tag></tag>
Some text
more tags
more text
END
至于您看到的症状:
something.html << END # ...
导致错误,因为 something.html
- 作为命令行的 first 标记,总是被解释为 command 执行 - 失败,因为(通常)没有这样的命令:
特定错误 取决于第一个标记是否:
是一个纯文件名(例如,something.html
- 没有路径部分):
command not found
是一个路径(无论是相对的还是绝对的;例如,./something.html
),指的是:
- 没有现有项目:
No such file or directory
- 现有的不可执行文件:
Permission denied
- 意思是:试图执行该文件,但它不可执行。
- 现有目录:
is a directory
我尝试向文件生成多行输出,但出现权限被拒绝错误。你知道错在哪里吗?
#!/bin/bash
something.html << END # Permission denied
<tag></tag>
Some text
more tags
more text
END
虽然 ... > file
(... >> file
) - output 重定向 - 可用于输出(通过附加)到 file
,但有没有对称file < ...
构造在POSIX-like shell中,例如Bash.
相反,输入重定向<
/<<
/<<<
隐式 向 stdin - from a here-document (<<
) 提供输入,在这种情况下。
你需要一个命令来读取标准输入以便处理它,即使只是将它发送到一个文件。
在后一种情况下,您 将 cat
与 输出 重定向 相结合: cat
只是将 stdin 输入复制到 stdout,这然后输出重定向发送到指定的文件:
#!/bin/bash
cat > something.html << END # cat with > sends stdin input to a file
<tag></tag>
Some text
more tags
more text
END
至于您看到的症状:
something.html << END # ...
导致错误,因为 something.html
- 作为命令行的 first 标记,总是被解释为 command 执行 - 失败,因为(通常)没有这样的命令:
特定错误 取决于第一个标记是否:
是一个纯文件名(例如,
something.html
- 没有路径部分):command not found
是一个路径(无论是相对的还是绝对的;例如,
./something.html
),指的是:- 没有现有项目:
No such file or directory
- 现有的不可执行文件:
Permission denied
- 意思是:试图执行该文件,但它不可执行。 - 现有目录:
is a directory
- 没有现有项目: