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
ImranRazaKhan
  • 1,955
  • 5
  • 33
  • 74
  • Have you tried looking at this? https://stackoverflow.com/questions/9733338/shell-script-remove-first-and-last-quote-from-a-variable – TinkerTenorSoftwareGuy Aug 06 '20 at 16:11
  • 2
    Another option might be using the eval command... just concat the string to form the entire command, first. https://linuxhint.com/bash_eval_command/ – TinkerTenorSoftwareGuy Aug 06 '20 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? – Cyrus Aug 06 '20 at 16:14
  • @TinkerTenorSoftwareGuy Thanks eval works for me. – ImranRazaKhan Aug 06 '20 at 16:23
  • @Cyrus I am calling this script from other like `sh myscript.sh` – ImranRazaKhan Aug 06 '20 at 16:24
  • `sh` ([Bourne-shell](https://en.wikipedia.org/wiki/Bourne_shell)) is usally not `bash` ([Bourne-again shell](https://en.wikipedia.org/wiki/Bash_(Unix_shell))). – Cyrus Aug 06 '20 at 16:43
  • Replace `sh` with `bash`. – Cyrus Aug 06 '20 at 16:44
  • @Cyrus bash doesn't helped so i will use eval it works for me thanks. – ImranRazaKhan Aug 06 '20 at 16:49
  • I'd avoid `eval` if at all possible -- it has a well-deserved reputation as a bug magnet. The single-quotes are just the shell's way of indicating that that string isn't being split up into separate arguments ("words"). I'd concentrate on trying to figure out *why* it isn't being split. – Gordon Davisson Aug 06 '20 at 16:53
  • @GordonDavisson Thanks for highlighting it look like gluster bug as its not able to parse, i will raise accordingly. – ImranRazaKhan Aug 06 '20 at 18:18
  • @ImranRazaKhan I don't think it can be `gluster` -- the shell should perform word splitting before passing the arguments to `gluster`, so it has to be happening at the shell level. The only things I can think of that'd cause this are that you're running `zsh` as the shell, or that the `IFS` variable has been set to some unusual value. It sounds like you've eliminated `zsh`; is it possible something has messed with `IFS`? – Gordon Davisson Aug 07 '20 at 00:56
  • @GordonDavisson For sake of simplicity i provided trimmed code previously, Now i shared the code which is using IFS to make gluster_volume variable, you can see updated code under update1 – ImranRazaKhan Aug 07 '20 at 11:20
  • 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. – Gordon Davisson Aug 07 '20 at 19:49

1 Answers1

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"