Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/Ch06/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,16 @@ Bash shell 本身提供了调试方法:

`echo < file` 会输出什么?

!!! question "使用 sudo 重定向输出"

当需要将输出重定向到仅 root 用户可写入的文件时,很多人的第一反应是:

```shell
sudo command > output_file
```

但最终失败了。尝试解释这样做不可行的原因。

!!! question "设定 HTTP 请求头"

尝试查询 `curl` 和 `wget` 的文档,给出设定 HTTP 请求头的方法。
Expand Down
14 changes: 14 additions & 0 deletions docs/Ch06/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ icon: material/tooltip-question

而 `echo` 只输出了空行,因为 `echo` 不从标准输入读取内容(它的输入从参数读取)。

## 使用 sudo 重定向输出

??? info "解答"

如果尝试用 `sudo` 将输出重定向到仅 root 用户可写入的文件,会看到 Permission denied 的错误。

这是因为重定向符号 `>` 仍由当前 shell 处理,当前 shell 以普通用户权限运行,没有写入权限。

正确做法是使用 sudo 执行 `tee` 命令,配合管道进行重定向:

```shell
command | sudo tee output_file
```

## 设定 HTTP 请求头

??? info "解答"
Expand Down