Home > OS >  How to parse with Docker bash command a double quoted string multiline?
How to parse with Docker bash command a double quoted string multiline?

Time:05-31

I have something like this in my Docker compose file:

command: >
      bash -c "
        ...some statements here...
        gcloud ...some params... --ddl=\
         \"CREATE TABLE ....; \
          CREATE UNIQUE INDEX...; \
          CREATE UNIQUE INDEX...; \
          CREATE TABLE ...; \
          CREATE TABLE ...; \
          CREATE TABLE ...;\"
      "

the problem here seems that the backslash \ is not working properly within a string "..." where you can see some (truncated for readability) DDL statements. How can I fix that? I tried to replace the \ with \\ and \n\ but none has worked.

CodePudding user response:

There is a website that illustrates the different ways to do multi-line strings in yaml, https://yaml-multiline.info, but unfortunately this special case isn't covered in their examples.

The problem you're running into is that in bash, within double quotes, we do not need do escape carriage returns. It's also easiest to remove the escaped line ending in --ddl=\ and move the next double-quote there, so that the quoted text begins at that point. Also, we need to strip the line-endings inside the yaml multi-line syntax using >-. So the result is:

command: >-
    bash -c "
      ...some statements here...
      gcloud ...some params... --ddl=\"
        CREATE TABLE ....;
        CREATE UNIQUE INDEX...;
        CREATE UNIQUE INDEX...;
        CREATE TABLE ...;
        CREATE TABLE ...;
        CREATE TABLE ...;\"
    "

Using dasel we can see that the block of text in the command is interpreted exactly as seen in the yaml:

$ dasel -f test-2.yaml --format '{{ .command }}'
bash -c "
  ...some statements here...
  gcloud ...some params... --ddl=\
   \"CREATE TABLE ....;
    CREATE UNIQUE INDEX...;
    CREATE UNIQUE INDEX...;
    CREATE TABLE ...;
    CREATE TABLE ...;
    CREATE TABLE ...;\"
"

If we copy the full quoted bash -c block and instead use echo, we can see the commands that bash will interpret:

$ echo "
  ...some statements here...
  gcloud ...some params... --ddl=\"
    CREATE TABLE ....;
    CREATE UNIQUE INDEX...;
    CREATE UNIQUE INDEX...;
    CREATE TABLE ...;
    CREATE TABLE ...;
    CREATE TABLE ...;\"
"

  ...some statements here...
  gcloud ...some params... --ddl="
    CREATE TABLE ....;
    CREATE UNIQUE INDEX...;
    CREATE UNIQUE INDEX...;
    CREATE TABLE ...;
    CREATE TABLE ...;
    CREATE TABLE ...;"

What is produced is valid shell syntax. Carriage returns inside of double-quotes do not need to be escaped, and the quoted --ddl= value will be sent across as a valid set of sql statements.

  • Related