Home > Software engineering >  How to split a string that is separated by comma, into multiple strings using Bash
How to split a string that is separated by comma, into multiple strings using Bash

Time:08-24

I am creating a git-hub workflow using workflow_dispatch for my inputs data

workflow_dispatch:
     inputs:
      Browser:
          description: 'Select what browsers to use'     
          required: true
          default: 'Chrome'

And I got my setup job, where I take my workflow_dispatch data and trasfer them to json, so I can use it in my matrix

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      matrix-browser: ${{ steps.set-matrix-browser.outputs.matrix-browser }}
    steps:
    - uses: actions/checkout@v2
    - id: set-matrix-browser
      run: |
        testBro="${{ github.event.inputs.Browser }}"
        echo "::set-output name=matrix-browser::[\"${testBro}\"]"
        echo "[\"${testBro}\"]"

So, my question is: If I got two or more browsers in my github.event.inputs.Browser = "Chrome, Safari, Edge", who I can to split them, for each browser to be a separate string.

I want my output to look like this,

["Chrome", "Safari, "Edge"]

but instead I got this

["Chrome, Safari, Edge"]

Can you please suggest how I need to change this line of code?

echo "::set-output name=matrix-browser::[\"${testBro}\"]"

I've tried something like this:

echo "::set-output name=matrix-browser::[\"${testBro | tr "," "\n"}\"]"

CodePudding user response:

Assuming testBro="Chrome,Safari,Edge"
echo [\"$testBro\"] |sed 's/,/\", \"/g'
or for the full line
echo "::set-output name=matrix-browser:: $(echo [\"$testBro\"] |sed 's/,/\", \"/g')"
gives the output
::set-output name=matrix-browser:: ["Chrome", "Safari", "Edge"]

CodePudding user response:

I figured it

echo "::set-output name=matrix-browser::[\"${testBro//', '/\",\"}\"]"

Thanks everybody

  • Related