如何避免bash脚本出现sed错误? [英] How can I avoid sed error on bash script?

查看:172
本文介绍了如何避免bash脚本出现sed错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to add text after the last line, but I'm getting this error.

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

How can I avoid this error keeping the same actions?

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.

推荐答案

您在结尾加上双引号:

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

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

This, along with ShellCheck's warning, indicates that the whole sed command is actually part of a double quoted string. You should read more of the context around where this command was found.

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

For example, it could actually be part of a larger construct like this:

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

这将是使命令失败的真实错误,因此不应忽略该错误.在这种情况下,您可以跳过$ s.

This would be a real bug that makes the command fail, so it should not be ignored. In this case you would escape the $s.

这篇关于如何避免bash脚本出现sed错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆