简体   繁体   English

使用Bash将多字串作为卷曲选项

[英]Multiword string as a curl option using Bash

I want to get some data from a HTTP server. 我想从HTTP服务器获取一些数据。 What it sends me depends on what I put in a POST request. 它发送给我的内容取决于我在POST请求中输入的内容。

What I put in the INPUT_TEXT field is a sequence of words. 我在INPUT_TEXT字段中输入的是一个单词序列。 When I run the following command, I get good looking output. 当我运行以下命令时,得到的输出看起来不错。

$ curl http://localhost:59125/process -d INPUT_TEXT="here are some words"

I want a bash script to take some string as a command line argument, and pass it appropriately to curl. 我希望bash脚本将一些字符串作为命令行参数,并将其适当地传递给curl。 The first thing I tried was to put the following in a script: 我尝试的第一件事是将以下内容放入脚本中:

sentence=$1
command="curl http://localhost:59125/process -d INPUT_TEXT=\"${sentence}\""
$command

I then run the script like so: 然后,我像这样运行脚本:

$ ./script "here are some words"

But then I get a curl Couldn't resolve host error for each of "are", "some", and "words". 但是,然后我开始卷曲Couldn't resolve host “ are”,“ some”和“ words”中的每一个的Couldn't resolve host错误。 It would seem that "here" got correctly treated as the INPUT_TEXT, but the rest of the words were then considered to be hosts, and not part of the option. 似乎“这里”被正确地视为INPUT_TEXT,但其余单词则被视为主机,而不是该选项的一部分。

So I tried: 所以我尝试了:

command=("curl" "http://localhost:59125/process" "-d" "INPUT_TEXT='$sentence'")
${command[@]}

I got the same output as the first script. 我得到的输出与第一个脚本相同。 I finally got what I wanted with: 我终于有了想要的东西:

result=$(curl http://localhost:59125/process -d INPUT_TEXT="${sentence}")
echo $result

I'm still unsure as to what the distinction is. 我仍然不确定区别是什么。 In the first two cases, when I echoed out the contents of command, I get exactly what I input from the interactive Bash prompt, which had worked fine. 在前两种情况下,当我回显命令的内容时,我可以从交互式Bash提示符中完全得到我输入的内容,效果很好。 What caused the difference? 是什么引起了差异?

The following will work: 以下将起作用:

command=("curl" "http://localhost:59125/process"
         "-d"  "INPUT_TEXT=$sentence")
"${command[@]}"

That has two changes from yours: 与您相比有两个变化:

  • I removed the incorrect quotes around $sentence since you don't want to send quotes to the server (as far as I can see). 我删除了$sentence周围的不正确的引号,因为您不想将引号发送到服务器(据我所知)。

  • I put double-quotes around the use of "${command[@]}" . 我在"${command[@]}"的使用前后加上双引号。 Without the double quotes, the array's elements are concatenated with spaces between them and then the result is word-split. 如果没有双引号,则将数组的元素之间用空格连接起来,然后将结果进行字分割。 With double quotes, the individual array elements are used as individual words. 用双引号将单个数组元素用作单个单词。

The second point is well-explained in the bash FAQ and a bunch of SO answers dealing with quotes. 第二点在bash FAQ和一堆有关报价的SO答案中得到了很好的解释。

The important thing to understand is that quotes only quote when a command is parsed. 要了解的重要一点是,引号仅在解析命令时才引号。 A quote which is a character in a variable is just a character ; 引号是变量中的字符只是一个字符 ; it is not reinterpreted when the value of the variable expanded. 当变量的值扩展时,不会重新解释它。 Whitespace in the variable is used for word-splitting if the variable expansion is unquoted; 如果未引用变量扩展名,则将变量中的空格用于单词拆分; the fact that the whitespace was quoted in the the command which defined the variable is completely irrelevant. 在定义变量的命令中用空格引起的事实是完全不相关的。 In this sense, bash is just the same as any other programming language . 从这个意义上讲,bash与任何其他编程语言都一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM