1

I want to run command:

curl -H "X-Auth-Token: $OS_TOKEN" "http://192.168.0.13:8774/v2.1/servers"

And, I made the script for it.

URL="http://192.168.0.13:8774/v2.1/servers"
HEADER="X-Auth-Token: 12345678"
METHOD="GET"

CMD="curl -H $HEADER $URL"

eval "$CMD"

But, as it doesn't include any double quotes in $CMD and separates parameters by space, it runs wrong command

$ bash request.sh
curl: (6) Could not resolve host: 12345678

How can I wrap it? In command line, we can give double quotes to separate parameters.

But how can I put a variable with spaces to a parameter using scripts as same as command line.

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
osflw
  • 79
  • 1
  • 8
  • 2
    Don't try to put your command in a variable (see [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050)); they're for data, not executable code. Just execute it directly, like you would at the command line. And don't use `eval`, it's a massive bug magnet. Oh, and use lower- or mixed-case variable names, to avoid conflicts with the various all-caps names that have special meanings. – Gordon Davisson Jun 08 '21 at 09:42
  • 1
    @osflw : If you **must** have the pieces of your command in variables (for whatever reason), try to put them into an array, one array element for each parameter. For how to do this, see my answer to [this](https://stackoverflow.com/questions/67862182/command-runs-from-terminal-without-error-but-fails-from-bash-script/67866758?noredirect=1#comment119958589_67866758) question. Slightly different question, but same approach advisable. – user1934428 Jun 08 '21 at 13:09
  • @GordonDavisson, @user1934428 Thanks for both advice. I put my command in the variable because I want to put some parameter depending on the result of if-statement. For example, I add `-d $DATA` parameter if `$METHOD == "POST"`, which was omitted here. Then, how do people make for that? – osflw Jun 09 '21 at 09:42
  • Use an array; see [this section of BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050#I.27m_constructing_a_command_based_on_information_that_is_only_known_at_run_time), the answer user1934428 linked, and [this answer of mine](https://stackoverflow.com/questions/50710476/bash-need-help-passing-a-a-variable-to-rsync/50710691#50710691). BTW, this approach doesn't work well with commands to be run over `ssh` -- that's a different problem, with different solutions. – Gordon Davisson Jun 09 '21 at 10:27
  • I got it. Thanks! Then, I'll ask in separate question about the situation, where `ssh user@hostname -t "sudo -c "grep "things to grep"""`. – osflw Jun 10 '21 at 08:48
  • @GordonDavisson, thanks to your answer, I'm using it well. But, I saw something weird expansion. I added `HEADERS+=("Content-Type: application/json")`, but in `${HEADERS[@]/#/-H }`, it expands to `-H Content-Type: application/json` so it recognizes as separated parameters. How can I solve this issue? I tried adding `\"`, but it doesn't help. (It expands to `-H '"Content-Type:' 'application/json"'`) – osflw Jun 28 '21 at 08:35
  • You need double-quotes around it to prevent word-splitting. But with double-quotes, it won't split `-H` from the value. Solution: `curl` (like many utilities) will let you omit the space between `-H` and the header, so using `"${HEADERS[@]/#/-H}"` (note the double-quotes and lack of space after `-H`) should work. – Gordon Davisson Jun 28 '21 at 08:42
  • @GordonDavisson. Wow. I'm surprised how deep you understand the bash. Thanks. – osflw Jun 28 '21 at 11:16

2 Answers2

0

You should change CMD="curl -H $HEADER $URL" in CMD="curl -H \"$HEADER\" \"$URL\"".

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
  • Thanks. It helps for this situation. I wonder something more. If I want to wrap command with escaped double quote, sometimes it prints error. `ssh user@hostname -t "sudo -c "grep "things to grep"""`. I want run grep with sudo while finding something with space. Then, how can I give it? If I escape double quotes, it gives an error. There's two type of quotes and I have to wrap three depth. Please assume I have to use three depth to run that command. – osflw Jun 08 '21 at 09:55
  • Are you addressing a different issue than the above described? – Antonio Petricca Jun 08 '21 at 10:00
  • Write `ssh user@hostname -t "sudo -c \"grep 'things to grep'\""`. – Antonio Petricca Jun 08 '21 at 10:04
  • Yes, it is a different issue. I thought it was similar, but the issue I uploaded was solved by escaped quotes. And, when it comes to another issue, it shows `Unmatched ".` if I run command as you said. – osflw Jun 08 '21 at 12:27
0

I would try:

URL="http://192.168.0.13:8774/v2.1/servers"
HEADER="X-Auth-Token: 12345678"
CMD='curl -H \"${HEADER}\" ${URL}'
eval "${CMD}"
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70