我有一个带有以下功能的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
}
传递参数:
## 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'
我把这个传递给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`,卷曲“$ 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 $1)
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 ${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 $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[@]}"
}
答案 0 :(得分:3)
shell在替换变量引用之前解析引号(例如$1
),所以如果$1
的值中有引号,那么它们到位时他们做任何有用的事都为时已晚。不是将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[@]}"
答案 1 :(得分:0)
您可以先使用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\" $1 $2"
之后的命令字符串
eval ${command}
eval
将首先读取并解释所有引用,转义和变量,然后运行已解释的命令。希望这可以帮助。迎接。