Erik
Erik

Reputation: 3198

invalid bash line in gitlab, but unclear why

I am attempting to parse the nodejs package.json for the version number.. this line usually works but somehow I am now getting an error of invalid block

PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | xargs)

specifically this area is highlighted in VSCode sed 's/......

enter image description here

How can I repair this line? is the " needing escaping or ?

Upvotes: 0

Views: 80

Answers (1)

Biffen
Biffen

Reputation: 6357

The problem is YAML-related (it seems the combination of : and | is the culprit).

The easy fix is to use a YAML block:

script:
  # …
  - |-
    PACKAGE_VERSION=$(cat package.json | grep version | head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g' | xargs)

And while you’re at it you can use a folding block to make it more readable:

script:
  # …
  - >-
    PACKAGE_VERSION=$(
      cat package.json |
      grep version |
      head -1 |
      awk -F: '{ print $2 }' |
      sed 's/[",]//g' |
      xargs
    )

Chaining grep, head, awk and sed can usually be replaced by a single awk. And there’s a useless use of cat. Not to mention parsing JSON without a proper JSON parser.

Upvotes: 3

Related Questions