TurnsCoffeeIntoScripts
TurnsCoffeeIntoScripts

Reputation: 3918

Sed command doesn't seem to process quote character properly

I have a little issue with a sed command. So I have a Dockerfile which contains the following:

LABEL maintainer="abc" \
    authors="abc" \
    version="1.0.0" \
    description="API desc..."

I'm trying to automate my release process so I have a script which increment the version in the Dockerfile before building it. Here's the sed command I use:

sed -r 's/(version=)([0-9])\.([0-9])\.([0-9])/echo "    \1\\\"\2.\3.$((\4+1))\\\" "/ge' Dockerfile

The result is this:

LABEL maintainer="abc" \
    authors="abc" \
sh: 1: Syntax error: Unterminated quoted string

But as far as I can see all my strings are properly quoted. The echo itself has both opening and closing " and within the string itself I have both \\\" to print the " character itself. So I don't quite understand why I'm getting this error.

I have tried this

sed -r 's/(version=)([0-9])\.([0-9])\.([0-9])/echo "    \1\\\"\2.\3.$((\4+1))\\\" \""/ge' Dockerfile

Where I added \" before the closing " of the echo: \""/ge' and this actually works. Again I don't quite understand why this would work thought.

Thanks for your help

Upvotes: 0

Views: 68

Answers (2)

HardcoreHenry
HardcoreHenry

Reputation: 6387

You're missing some quotes:

sed -r 's/(version=)\"([0-9])\.([0-9])\.([0-9])\"/echo "    \1\\\"\2.\3.$((\4+1))\\\" "/ge' Dockerfile
                    ^^                         ^^

Which outputs:

authors="abc" \
version="1.0.1"
description="API desc..."

Upvotes: 0

chepner
chepner

Reputation: 532418

Don't modify the Dockerfile. The script that builds your image should pass the new version as an argument.

ARG version

LABEL maintainer="abc" \
      authors="abc" \
      version=$version \
      description="API desc..."

Then

docker build --build-arg version=<newversion> .

Whatever calls docker build is responsible for producing the next version number from the old one, which should be stored somewhere outside the Dockerfile.

This could be as simple as a single file that contains your version number, modified using awk:

$ cat version
1.2.3
$ awk -v OFS=. -F . '{$3=$3+1; print}' version > new_version && mv new_version version
$ cat version
1.2.4

Upvotes: 3

Related Questions