So I have the following variables defined in a bash script:
new_commit="back:h3912kk"
old_commit="back:1.0.1"
file = docker-compose.yml
Then I have this yml $file
:
version: '3'
services:
mongo:
container_name: mongo
image: mongo:3.4
volumes:
- /home/mongo_files:/data/db
networks:
my_network:
back:
container_name: back
image: index.docker.io/gg/back:1.0.1
networks:
my_network
networks:
my_network:
I would like to replace the line
image: index.docker.io/gg/back:1.0.1
for
image: index.docker.io/gg/back:h3912kk
I am using the following sed
command in my bash script:
echo sed -i "s/$old_commit/$new_commit/" $file
However the following error appears:
sed: -e expression #1, char 12: unterminated `s' command
Of course the docker-compose file does not update.
What am I doing wrong?
if I do an echo command this is displayed:
sed -i s/back:1.0.1
/back:h3912kk/ docker-compose.yml
Why the echo is not displaying the command on the same line?
EDIT:
As @Kusalananda♦ says, the old_commit variable is not a static file, and that is the main reason I guess why I am being unable to reproduce the answers you provide with the $old_commit
involved ( I though it was not going to suppose a problem)
In order to obtain the old_commit
parameter I use the following while that looks in the yml file for the line that satisfies the condition I am interested in, here is my full code:
output='back:h3912kk'
# here I split the name of the repo and the commit by ':'
readarray -d ':' -t array_new_commit <<< "$output"
repo="${array_new_commit[0]}"
file=docker-compose.yml
# here I loop over the lines of the yml file:
while read -r line; do
# if a line contains the repo name str and image str I generate a line with the new commit
if [[ $line == *$repo* && $line == *"image"* ]]; then
# old_line=$line
readarray -d '/' -t array_old_commit <<< "$line"
old_commit=${array_old_commit[-1]}
# new_line="${array_old_commit[0]}:${array_old_commit[1]}:$new_commit"
fi
done<"$file"
Somehow ( dont know why, I am new into bash) generating this variable this way literally does not allows me to use it as if it was a static variable. (I would really want to understand this)
Could you please give me a workaround to replace the old_commit
variable by the new_commit
into the yml file?
Thank you again Kusalananda♦ for pointing out this issue
PD I am using a ubuntu 0.18.4 distro
EDIT: Using a second piece of code:
yq -Y --arg new "$new_commit" --arg reposit "$repo" \
'".services." + $reposit + ".image |=index.docker.io/gg/" + $new' docker-compose.yml
my output was:
.services.back.image |=index.docker.io/gg/back:h3912kk
...