-2

我需要在最后一行之后添加文本,但出现此错误。

如何避免此错误保持相同的操作?

sed -i -e '$a\  containers:' -e '$a\  - name: container' -e '$a\    image: httpd' -e '$a\    ports:' -e '$a\    - containerPort: 80' /path/to/file"
                            ^-- SC2154: a is referenced but not assigned.
4

1 回答 1

2

你有一个尾随双引号:

#                                                  Here --v
sed -i [...] -e '$a\    - containerPort: 80' /path/to/file"

这与 ShellCheck 的警告一起表明整个 sed 命令实际上是双引号字符串的一部分。您应该阅读更多有关找到此命令的上下文。

例如,它实际上可能是这样一个更大结构的一部分:

ssh myhost "
  [...]
  sed -i [...] -e '$a\    - containerPort: 80' /path/to/file"

这将是一个使命令失败的真正错误,因此不应忽略它。在这种情况下,您将逃避$s。

于 2020-07-14T21:45:16.893 回答