LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   add literal string with simple and double quotes to a file (https://www.linuxquestions.org/questions/programming-9/add-literal-string-with-simple-and-double-quotes-to-a-file-4175675574/)

ntubski 05-24-2020 06:50 PM

Quote:

Originally Posted by vincix (Post 6126713)
But if I wanted to use regex for matching the pattern and then substitute it with a literal string, like the sub command does, then all of a sudden I have a problem, in that I yet again have to be careful about what the variable contains, right?

Not necessarily:

Code:

awk -v a="${var}" -v b="${var2}" '{
  while(i=match($0,a))
    $0=substr($0,1,i-1) b substr($0,i+RLENGTH)
  print $0
}' file.txt


boughtonp 05-25-2020 05:31 AM

Quote:

Originally Posted by vincix (Post 6126640)
I wanted to restrict the thread only to this particular problem without giving the bigger context, but now the context has become much more relevant.

It usually does, and should always be mentioned so people who know the answer to your real problem can go straight to providing the simpler solution that everyone else is already using, instead of either reinventing the wheel or using an ugly hack.


Quote:

The point was to do something somewhat intentionally dumb, that is to say, to change an existing docker-entrypoint from an official docker image (of php-apache) through an additional Dockerfile, where I'd run the sed commands.
The here document would have been a great solution under normal circumstances, and it hadn't occurred to me anyway, but unfortunately it doesn't work in Dockerfile :)

What I did eventually (which I find much more sensible) was to actually copy only the entrypoint from github, add the sed lines that would replace the php configuration settings, and then copy it into the new image, ensuring, of course, that it has execution permissions (being an entrypoint).
I haven't needed to use Docker yet, so that doesn't make complete sense to me, but it sounds like you want to change the password in a config file, which seems a sufficiently common problem to have a common solution.

How does everyone else using Docker solve it?

What are you doing differently that stops you using their solution?


vincix 05-25-2020 12:33 PM

Well, if you're not familiar with docker it's a little bit harder to explain, I guess, but the solution I've chosen, given that I don't have other orchestration tools (like swarm or kubernetes, which offer so called config files that can be dynamically mounted into the container and being used as such even after you've built the containers), should be the right one in this context. It's just a sed replacement based on environmental variables - this is rather common.

What was weird about what I was trying to do (I'd have stuck with it only if the solution had turned out to be much easier, or maybe not even then), was changing an existing entrypoint (which is just a bash script running every time the container starts), which had already been built in the container image, by adding a few sed lines before the last line. Yeah, I don't think people do that. So that's why I simply copied the whole entrypoint, adjusted to my liking, and made it part of the build (using a Dockerfile of my own - meaning adding another container layer in the image).

I hope it made a little bit of sense :)


As far as the context is concerned, sure, I agree 100%, I just thought this time I could get away with it :D

@ntubski, thanks for the answer!

pan64 05-26-2020 01:36 AM

Quote:

Originally Posted by vincix (Post 6127178)
Well, if you're not familiar with docker it's a little bit harder to explain, I guess, but the solution I've chosen, given that I don't have other orchestration tools (like swarm or kubernetes, which offer so called config files that can be dynamically mounted into the container and being used as such even after you've built the containers), should be the right one in this context. It's just a sed replacement based on environmental variables - this is rather common.

That can be made with docker too. You can "mount" even a simple file into the docker image. Also you can pass environment variables to the docker image. But obviously it all depends on the details....


All times are GMT -5. The time now is 08:52 AM.