【发布时间】:2015-03-11 17:37:08
【问题描述】:
我有一个具有以下功能的 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`, 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 $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:没有指定 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 $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[@]}"
}
【问题讨论】:
-
使用 $@ 而不是 $1
-
感谢您的输入,但调试器仅显示: curl "@1" 和 curl @1 用于 qouted 和 unqouted 版本。另外,我现在将添加,我如何将参数传递给调用 curl 的函数...请检查,也许这样会更容易帮助
-
什么是
get_prepared_string_for_cURL? -
一个函数 - 我在上面添加了它的实现