Wednesday, October 26, 2022

[SOLVED] Sed putting through weird errors?

Issue

I cannot for the life of me figure this out.

I have a sed command being executed here (it's an M1 Mac, which I know has a different version of sed than GNU sed, which could be the problem? But i need the command to act the same on Linux/Windows/Mac:

firstString="SQLITE_KEY=\"?.*\"?[^\w\d]"
secondString="SQLITE_KEY=\"?${SQLITE_KEY_GENERATED}\"?"

sed -i '' -r "/SQLITE_KEY/ s/${firstString}/${secondString}/" .env

I have a set of environment variables in another file, and these are injected into the .env file:

SQLITE_KEY=zCokzf3aVzS0T7cH3mJiyrqUBK5YpETwqVf4tg==

However when I run it, I get an error like this.

sed: 1: "/SQLITE_KEY/ s/SQLITE_K ...": bad flag in substitute command: 't'

The goal here is to take the environment variables from the source file, and inject them into the .env file where SQLITE_KEY is. The "bad flag" warning changes letters every time, so I'm suspecting it's something to do with the formatting of the password.

What am I doing wrong?


Solution

You have a syntax conflict involving double-quotes (").

secondString has escaped '"'.

Those are then interpreted by the shell command after the variable substitution for sed.

So ... you need to replace the outside double-quotes by single-quotes on the sed, in this way:

eval sed -i \'\' -r \'/SQLITE_KEY/ s/${firstString}/${secondString}/\' .env

This way, the double-quotes will be correctly carried thru into the .env file.



Answered By - Eric Marceau
Answer Checked By - Cary Denson (WPSolving Admin)