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>
<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
I guess this is something around carriage return or spaces.
Here’s the code:
#!/bin/bash $USERNAME="myusername" $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 ?
Thanks to @Quasimodo I finally managed to solve this issue.
First I enhanced the
PATTERN readability by splitting the
REPLACEMENT into two distinct variables.
Then basically what I made is to change single-quotes to double-quotes and vice versa.
REGEXP="preserve_hostname:sfalse" REPLACEMENT="preserve_hostname: truenmanage_etc_hosts: false" PATTERN="s/$REGEXP/$REPLACEMENT/" 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
1 Asked on November 16, 2021 by user32012
1 Asked on November 16, 2021 by flying-sheep
2 Asked on November 16, 2021 by die4ever2005
1 Asked on November 16, 2021 by johan-schuijt
1 Asked on November 16, 2021 by nathaniel-m-beaver
1 Asked on November 16, 2021
1 Asked on November 16, 2021 by archerindigo
0 Asked on November 16, 2021 by pfnuesel
0 Asked on November 16, 2021 by preeti-m-sobarad
1 Asked on November 14, 2021 by losethos
4 Asked on November 14, 2021
1 Asked on November 14, 2021 by mm3
Get help from others!