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

BASH: passing curl parameters as $1 variable

我有一个具有以下功能的 bash 脚本:

function curl_the_URL_and_check_status(){

   status=$(curl  | 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 "
       exit 1
   fi
}

传递参数:

## 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 
++ cURL_string='-H "Host: php-mindaugasb.c9.io" -I -s http://localhost:805/Testing/JS/displayName.js'

我把这个传递给 curl:

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

从控制台尝试无效(抛出网关超时错误)。

所以我的卷曲看起来像这样:

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

我需要它看起来像这样(从控制台测试时有效):

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

我该如何做到这一点?

尝试使用 `curl $1`,curl "$1" ...

谢谢

附录

我这样调用函数:

 # 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 )
 curl_the_URL_and_check_status $curl_params

(更新:2015 年 1 月 14 日)

这是我使用数组方法得到的结果:

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

案例:

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

curl: 未指定 URL!

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

我需要

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

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  | 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  | perl -pe 's|(?<=://).+?(?=/)|localhost:805|')

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

    # echo "$cURL_string"

    echo "${cURL_string[@]}"
}

您可以使用 eval 先解释命令字符串中的变量然后执行它

更新:

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.

所以你可以在之后构建一个像 command="curl -H \"Content-Type: whatever\" " 这样的命令字符串 运行

eval ${command}

eval 将首先读取和解释所有引文、转义符和变量,然后 运行 解释命令。希望这可以帮助。问候。

shell 解析引号 之前 替换变量引用(例如 </code>),所以如果 <code> 的值中有引号,当他们到位时,他们做任何有用的事情都为时已晚。不要将 curl 参数作为嵌入引号的单个参数传递,而是将其作为一系列参数传递并使用 "$@" 展开它:

function curl_the_URL_and_check_status(){

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

...然后用类似的方式调用它:

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

而不是:

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

但看起来您也在变量中构建参数列表,这会导致完全相同的问题——没有很好的方法来获取普通变量并根据嵌入的引号将其拆分为参数。同样,有一个解决方案:使用数组,每个参数都是数组的一个元素。然后,将数组引用为 "${arrayname[@]}",这样每个元素都被视为一个单独的参数。

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