0

I have following scripts

#!/bin/bash
set -o xtrace
gluster_volume="a.example.com:/data/brick1/gv0 b.example.com:/data/brick1/gv0"
gluster volume create gv0 replica 2 ${gluster_volume} force

While executing second line this script throwing error related to gluster_volume that its unable read complete line and just considering a.example.com:/data/brick1/gv0 b.example.com(so look like gluster command not able parse it).

Then I run this script with trace and found that command is like

gluster volume create gv0 replica 2 'a.example.com:/data/brick1/gv0 b.example.com:/data/brick1/gv0' force

so script is adding single quotes which are creating problem. I was under the impression that it's due to set -o xtrace the single quotes are not part of the value, just part of the displayed command line. But its not true. if i run above command on command prompt it gives same error and on removing quotes it works.

How i can change my script so it don't add quotes? (I tried to remove quote with sed but not working, something happening at run time).

I need gluster_volume with dynamic values.

Update1:

workers=`echo "${WORKER_HOST_IP}"|sed "s/,$//"`
IFS=','
for worker in ${workers}; do
    IFS='-' read -r -a array <<< "$worker"

    gluster_volume+=${array[0]}':/data/brick1/gv0 '

done
13
  • Have you tried looking at this? stackoverflow.com/questions/9733338/… Commented Aug 6, 2020 at 16:11
  • 2
    Another option might be using the eval command... just concat the string to form the entire command, first. linuxhint.com/bash_eval_command Commented Aug 6, 2020 at 16:13
  • I suspect that you are not using bash but zsh. Add this to your script and its output to your question: echo "$BASH_VERSION". How do you start your script? Commented Aug 6, 2020 at 16:14
  • @TinkerTenorSoftwareGuy Thanks eval works for me. Commented Aug 6, 2020 at 16:23
  • 1
    That IFS=',' explains it. You could reset IFS back to normal after the loop (there are several ways to do this, none perfect), but I'd look at using a better method to split workers. The best way to do that depends on the exact format of the WORKER_HOST_IP variable. Commented Aug 7, 2020 at 19:49

1 Answer 1

1

You can try eval(Be careful if the list of bricks or the string comes from an external untrusted source)

#!/bin/bash
set -o xtrace
gluster_volume="a.example.com:/data/brick1/gv0 b.example.com:/data/brick1/gv0"
eval "gluster volume create gv0 replica 2 ${gluster_volume} force"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.