Executing sed throws the error "unexpected EOF while looking for matching"

Unix & Linux Asked by TwystO on November 7, 2020

Here’s the problem.
I’m trying as "root" user to execute a command as "myusername" user.

That’s why I’m using su - $USERNAME -c <command>
The <command> itself is something like sudo sed -i 's|SEARCH_REGEX|REPLACEMENT|' /etc/cloud/cloud.cfg

But each time I execute my script I got an error which seems to be due to the REPLACEMENT part.
I guess this is something around carriage return or spaces.

Here’s the code:

$USERPWD="[email protected]"
PATTERN='s|preserve_hostname:sfalse|preserve_hostname: truenmanage_etc_hosts: false|'
su - $USERNAME -c 'sudo -S bash -c "sed -i '$PATTERN' /etc/cloud/cloud.cfg"' <<< $USERPWD

And here’s the error I get:

 truenmanage_etc_hosts:: -c: line 0: unexpected EOF while looking for matching `"'
 truenmanage_etc_hosts:: -c: line 1: syntax error: unexpected end of file

The ressources that helped me:

What am I doing wrong ?

One Answer

Thanks to @Quasimodo I finally managed to solve this issue.

First I enhanced the PATTERN readability by splitting the REGEXP and REPLACEMENT into two distinct variables.
Then basically what I made is to change single-quotes to double-quotes and vice versa.

REPLACEMENT="preserve_hostname: truenmanage_etc_hosts: false"
su - $USERNAME -c "sudo -S bash -c 'sed -n -i "$PATTERN" /etc/cloud/cloud.cfg'" <<< $USERPWD

Note that I escaped the double-quotes around $PATTERN in the sed command.
And that's it!

Answered by TwystO on November 7, 2020

Add your own answers!

Related Questions

No GTK settings in Opensuse 13.2, KDE Plasma 5

1  Asked on November 16, 2021 by user32012


Use a specific font for emojis

1  Asked on November 16, 2021 by flying-sheep


How to change poll rate of usb “gamepad” drum controller

1  Asked on November 16, 2021 by johan-schuijt


add status of sudo session to PS1 without extending session

1  Asked on November 16, 2021 by nathaniel-m-beaver


syslog_ng cannot start succesfully

1  Asked on November 16, 2021 by laogao


How to prove “reboot -f” command is blocking?

1  Asked on November 16, 2021 by archerindigo


Manually generate password for /etc/shadow

10  Asked on November 14, 2021


can’t install libcups2-dev

1  Asked on November 14, 2021 by semyon-labzov


Evince not saving annotations

1  Asked on November 14, 2021 by dins2018


Debian 8 apt-get update showing 404 Not Found

3  Asked on November 14, 2021


How to quickly wipe a disk?

4  Asked on November 14, 2021 by tomwassing


Piping tar archive via mv on macOS

1  Asked on November 14, 2021


Linux Installation not detecting Macbook SSD

1  Asked on November 14, 2021 by mm3


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP