简体   繁体   English

BASH:将curl参数作为$ 1变量传递

[英]BASH: passing curl parameters as $1 variable

I have a bash script with the following function: 我有一个带有以下功能的bash脚本:

function curl_the_URL_and_check_status(){

   status=$(curl $1 | grep "X-Cache-Status:" | cut -d " " -f 2)

   if [[ "$status" != *"MISS"* ]]; then
       echo "
   cURL returned non MISS status. Something is not correct. Exiting with status 1
   check the status by entering curl $1"
       exit 1
   fi
}

Passing the parameters: 传递参数:

## My comment :: .... some more output ....
+++ grep -o -P '(?<=\/\/).*?(?=\/)'
++ host=php-mindaugasb.c9.io
+++ echo http://php-mindaugasb.c9.io/Testing/JS/displayName.js
+++ perl -pe 's|(?<=://).+?(?=/)|localhost:805|'
++ modified_URL=http://localhost:805/Testing/JS/displayName.js

## My comment ::  below is the parameter passed to the function as $1
++ cURL_string='-H "Host: php-mindaugasb.c9.io" -I -s http://localhost:805/Testing/JS/displayName.js'

I get this passed to curl: 我把它传递给curl:

++ echo -H '"Host:' 'php-mindaugasb.c9.io"' -I -s http://localhost:805/Testing/JS/displayName.js

Which does not work trying from console (gateway timeout error is thrown). 从控制台尝试不起作用(抛出网关超时错误)。

So my curl looks like this: 所以我的卷发看起来像这样:

curl -H '"Host:' 'php-mindaugasb.c9.io"' -I -s http://localhost:805/Testing/JS/displayName.js

I need it to look like this (which works when tested from console): 我需要它看起来像这样(从控制台测试时有效):

curl -H "Host: php-mindaugasb.c9.io" -I -s http://localhost:805/Testing/JS/displayName.js

How do I make this happen? 我该如何实现这一目标?

Tried with `curl $1`, curl "$1" ... 尝试`curl $ 1`,卷曲“$ 1”......

Thanks 谢谢

ADDENDUM 附录

I call the function like this: 我把这个函数称为:

 # another function that constructs correct CURL string
 cURL_string="\"-H Host: $host\" -I -s $modified_URL"

 # global scope - this is where the curl is called
 curl_params=$(get_prepared_string_for_cURL $1)
 curl_the_URL_and_check_status $curl_params

(UPDATE: 14 01 2015) (更新日期:2015年1月14日)

Here is what I get using array approach: 这是我使用数组方法得到的:

cURL_string=(-H \\"Host: $host\\" -I -s $modified_URL) cURL_string =( - H \\“Host:$ host \\” - I -s $ modified_URL)

CASES: 案例:

curl "${curl_params[@]}" ==> curl '-H "Host: php-mindaugasb.c9.io" -I -s http://localhost:805/Testing/JS/displayName.js'

curl: no URL specified! curl:没有指定URL!

curl ${curl_params[@]} ==> curl -H '"Host:' 'php-mindaugasb.c9.io"' -I -s http://localhost:805/Testing/JS/displayName.js

I need 我需要

curl -H "Host: php-mindaugasb.c9.io" -I -s http://localhost:805/Testing/JS/displayName.js

get_prepared_string_for_cURL get_prepared_string_for_cURL

function get_prepared_string_for_cURL(){

    # get the host from URL, to use with in curl with the --Host flag
    host=$(echo $1 | grep -o -P '(?<=\/\/).*?(?=\/)')

    # replace the host part with the "localhost:805" to request the resource
    # from the nginx virtual host (server block) dedicated for proxy cache
    modified_URL=$(echo $1 | perl -pe 's|(?<=://).+?(?=/)|localhost:805|')

    # construct cURL string
    cURL_string=(-H Host: $host -I -s $modified_URL)

    # echo "$cURL_string"

    echo "${cURL_string[@]}"
}

The shell parses quotes before substituting variable references (eg $1 ), so if there are quotes in the value of $1 , by the time they're in place it's too late for them to do anything useful. 外壳替代变量引用(如分析报价$1 ),因此,如果有在价值的报价$1 ,通过他们的及时到位为时已晚为他们做任何有用的东西。 Rather than passing the curl arguments as a single argument with quotes embedded, pass it as a series of arguments and use "$@" to expand it: 不是将curl参数作为嵌入引号的单个参数传递,而是将其作为一系列参数传递,并使用"$@"来扩展它:

function curl_the_URL_and_check_status(){

   status=$(curl "$@" | grep "X-Cache-Status:" | cut -d " " -f 2)
[...]

...and then call it with something like: ...然后用以下内容调用它:

curl_the_URL_and_check_status -H "Host: php-mindaugasb.c9.io" -I -s http://localhost:805/Testing/JS/displayName.js

instead of: 代替:

curl_the_URL_and_check_status '-H "Host: php-mindaugasb.c9.io" -I -s http://localhost:805/Testing/JS/displayName.js'

But it looks like you're also building the parameter list in a variable, which causes exactly the same problem -- there's no good way to take a plain variable and split it into arguments based on embedded quotes. 但看起来你也在变量中构建参数列表,这导致完全相同的问题 - 没有好的方法来获取普通变量并将其拆分为基于嵌入式引号的参数。 Again, there's a solution: use an array, with each argument being an element of the array. 同样,有一个解决方案:使用一个数组,每个参数都是数组的一个元素。 Then, reference the array as "${arrayname[@]}" so each element gets treated as a separate argument. 然后,将数组引用为"${arrayname[@]}"以便将每个元素视为单独的参数。

cURL_args=(-H "Host: php-mindaugasb.c9.io" -I -s "$modified_URL")
curl_the_URL_and_check_status "${cURL_args[@]}"

you could use eval to interpret variables in a command string first and then execute it 您可以先使用eval来解释命令字符串中的变量,然后再执行它

UPDATE: 更新:

eval [arg ...]
          The args are read and concatenated together into a single command.  This command is then read and  exe‐
          cuted  by  the  shell,  and its exit status is returned as the value of eval.  If there are no args, or
          only null arguments, eval returns 0.

so you could build a command string like command="curl -H \\"Content-Type: whatever\\" $1 $2" afterwards run 所以你可以构建一个命令字符串,如command="curl -H \\"Content-Type: whatever\\" $1 $2"之后运行

eval ${command}

eval will read and interpret all your quotations, escapes and variables first and then run the interpreted command. eval将首先读取并解释所有引用,转义和变量,然后运行已解释的命令。 hope this helps. 希望这可以帮助。 greets. 映入眼帘。

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

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