0

I was wondering why isn't this code working, it is simple but for some reason it's not working.

WIREGUARD_TEMP_NEW_CLIENT_INFO="# ef37a62148810e97daa4deb88fa7f8add6532c39d2feb56d712a0ab1e8f5fd13ec52cfb1751ad3812e21f33a1e0508f14b7d start
[Peer]
PublicKey = L5SCXFRI4Mhyo1nKTjs5d64CSGd36ehC8MD8jg2FbEM=
PresharedKey = 3sejb0srnD3ZHy4I3rydIqy6CPXEHwXWeoX2Yu/2msU=
AllowedIPs = 10.0.0.4/32,fd00:00:00::4/128
# ef37a62148810e97daa4deb88fa7f8add6532c39d2feb56d712a0ab1e8f5fd13ec52cfb1751ad3812e21f33a1e0508f14b7d end"

sed -i "$((6 * 4 - 6 + 11))i${WIREGUARD_TEMP_NEW_CLIENT_INFO}" /etc/wireguard/wg0.conf

The error I am getting is

sed: -e expression #1, char 113: unknown command: `['

What is the code doing?

It's just going to a given line number and adding the content to the given live number.

2 Answers2

1

Your second argument to sed, "$((6 * 4 - 6 + 11))i${WIREGUARD_TEMP_NEW_CLIENT_INFO}" expands to the multiline string:

29i# ef37a62148810e97daa4deb88fa7f8add6532c39d2feb56d712a0ab1e8f5fd13ec52cfb1751ad3812e21f33a1e0508f14b7d start
[Peer]
PublicKey = L5SCXFRI4Mhyo1nKTjs5d64CSGd36ehC8MD8jg2FbEM=
PresharedKey = 3sejb0srnD3ZHy4I3rydIqy6CPXEHwXWeoX2Yu/2msU=
AllowedIPs = 10.0.0.4/32,fd00:00:00::4/128
# ef37a62148810e97daa4deb88fa7f8add6532c39d2feb56d712a0ab1e8f5fd13ec52cfb1751ad3812e21f33a1e0508f14b7d end

The sed command i inserts the string following it on the same line. The next line is interpreted as a new command by sed. Since [Peer] isn't a valid sed command, it emits the error message you are seeing.

Tilman Schmidt
  • 4,101
  • 12
  • 27
0

This is the code that someone needs to use if they run into the same issue.

#!/bin/bash

TEMP_VALUE="# ef37a62148810e97daa4deb88fa7f8add6532c39d2feb56d712a0ab1e8f5fd13ec52cfb1751ad3812e21f33a1e0508f14b7d start\n[Peer]\nPublicKey = L5SCXFRI4Mhyo1nKTjs5d64CSGd36ehC8MD8jg2FbEM=\nPresharedKey = 3sejb0srnD3ZHy4I3rydIqy6CPXEHwXWeoX2Yu/2msU=\nAllowedIPs = 10.0.0.4/32,fd00:00:00::4/128\n# ef37a62148810e97daa4deb88fa7f8add6532c39d2feb56d712a0ab1e8f5fd13ec52cfb1751ad3812e21f33a1e0508f14b7d end"
sed -i $((1 + 1))i"${TEMP_VALUE}" file