skip to Main Content

I’m writing a BASH script on ubuntu 18 OS.
I’m doing manipulation on a string (append, cut)
and after that, I want to use it as a parameter for a docker command.
the problem is that it returns with apostrophes and I need only the value without those.

if [[ "${TESTIM_LABEL}" =~ "," ]]; then
    IFS=','
    read -a strarr <<< "$TESTIM_LABEL"
    LABELS=""
    prefix=" --label "
    for val in "${strarr[@]}";
    do
      LABELS+=${prefix}${val}
      echo "$LABELS"

    done
        printf "$LABELS"
  else
      printf "im outside the IF"
      LABELS="--label ${TESTIM_LABEL}"
fi

on this IF statement when the condition is true and I’m inside the IF then the value of LABELS variable printed without apostrophes
but
when I use this param later as part of a longer command it being inserted with apostrophes

example:

    RESULT=$(docker run --rm -e rpLaunch="${RP_LAUNCH_NAME}" -e rpTeam="${RP_TEAM}" -e rpUuid="${rp_uuid}" -e rpBranchNameTag="${BRANCH_NAME}" -e rpDescription="${RP_DESCRIPTION}" -v $2:/opt/testim-runner ${TESTIM_DOCKER} 
  --token ${TESTIM_TOKEN} 
  --project "${TESTIM_PROJECT}" 
  ${LABELS} 

output will be (after inserting to the IF "xxx,yyy"):

docker run --rm -e rpLaunch=master/testim/@arion_ab_testing -e rpTeam=SocialArion -e rpUuid= -e rpBranchNameTag=master -e rpDescription=http://jenkins-prod-search.internalk.com/job/ui-pull-request/3127/ -v /home/centos/jenkins/workspace/ui-pull-request:/opt/testim-runner testim/docker-cli --token Dt9kFOtOhNcMum2gZjvnapOpGyq8vgreEnZOJF2nR9SeCJaRGE --project bJFghGy6Jo9yvtOO3ZiO ' --label xxx --label yyy'

and those apostrophes around ‘ –label xxx –label yyy’ need to be removed.

How do I do that?

2

Answers


  1. [too long for a comment] … I’m thinking this may come down to a (simple) issue of how the TESTIM_LABEL variable is being populated. [NOTE: OP has yet to show us how said variable is being populated.]

    One simple example, using OP’s current code for demonstration purposes:

    #!/usr/bin/bash
    
    read -p "enter TESTIM_LABEL: " TESTIM_LABEL      # added to OP's code; have user enter value @ prompt;
                                                     # the rest is cut-n-pasted from the question ...
    
    if [[ "${TESTIM_LABEL}" =~ "," ]]; then 
        IFS=','
        read -a strarr <<< "$TESTIM_LABEL"
        LABELS=""
        prefix=" --label "
        for val in "${strarr[@]}";
        do
          LABELS+=${prefix}${val}
          echo "$LABELS"
    
        done
            printf "$LABELS"
      else
          printf "im outside the IF"
          LABELS="--label ${TESTIM_LABEL}"
    fi
    

    A couple sample runs where we do (not) quote the input:

    $ testim.bash
    enter TESTIM_LABEL: 'xxx,yyy'
     --label 'xxx
     --label 'xxx --label yyy'
     --label 'xxx --label yyy'              # unwanted quotes
    
    $ testim.bash
    enter TESTIM_LABEL: xxx,yyy
     --label xxx
     --label xxx --label yyy
     --label xxx --label yyy                # no quotes
    

    Granted, there may be another explanation but it would help if OP provides more details on how the TESTIM_LABEL variable is being set.

    Login or Signup to reply.
  2. Just remove them like so:

    test="'test'"
    
    #with
    $ echo "$test"
    'test'
    
    #without
    $ echo "${test//'}"
    test
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search