-2

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.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Kaleby Cadorin
  • 179
  • 2
  • 13
  • This looks like a very convoluted way to say `echo stuff >>file`. Probably also look at `cat` with a here document, and `printf '%s\n' 'stuff' 'more stuff'` for appending multiple lines at once. Furthermore see also `tee -a` if you need to append to a privileged file (i.e. `sudo` etc, where you can't simply append with a redirect). – tripleee Jul 14 '20 at 16:02
  • 1
    You may have a quoting error (an unclosed quoted string) somewhere earlier in the script that's leading to confusion. – Gordon Davisson Jul 14 '20 at 16:32
  • 1
    could it be that you miss some double quotes. ... there is one in the end of the command. If there is one just before that, it might explain the error – kvantour Jul 14 '20 at 16:33

1 Answers1

2

You have a trailing double quote:

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

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"

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.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • Full command is this one. There's no missing quote. ```ssh -q desktop "sed -i -e '$a\ containers:' -e '$a\ - name: range-container' -e '$a\ image: httpd' -e '$a\ ports:' -e '$a\ - containerPort: 80' /path/to/file.yaml" ``` – Kaleby Cadorin Jul 16 '20 at 13:07
  • 1
    Then the suspicion was correct and ShellCheck was right. This command won't work because `$a` is treated as a shell variable instead of a literal sed commands. You should escape the `$`s, which would also avoid the warning – that other guy Jul 16 '20 at 16:24
  • Like that? sed -i -e '\$a\ containers:' – Kaleby Cadorin Jul 17 '20 at 17:05
  • Right, like that. To debug this and similar issues you can do a `set -x; sed ..` to have the remote shell print out what it's executing. Without escaping, it might show `sed .. -e '\ '` instead of `sed .. -e '$a\ '` which tells you that the string was not correctly escaped – that other guy Jul 17 '20 at 20:28
  • @kcadorin, it's important to include that full context in your question, not just hide it in a comment somewhere. That you're constructing a remote command to pass over ssh is a *crucial* detail; the problem doesn't reproduce properly without it, nor will fixes proposed without understanding that context be correct.. – Charles Duffy Jul 18 '20 at 18:18