[英]How to pass variable into curl
There are other questions about putting the variable into curl command within the url. 关于将变量放入url中的curl命令还有其他问题。
I want to have a variable defined at the top of script to swap values like so: 我想在脚本顶部定义一个变量来交换值,如下所示:
# MODE=-v
MODE='-sS -w "\nEffective URL: %{url_effective} \nSize: %{size_download} \nTotal time: %{time_total} \nRedirect URL: %{redirect_url}"'
and use it among several curl requests like so: 并在几个curl请求中使用它,如下所示:
PAGE=$(curl $MODE --include --location --config curl.config $TARGET1)
Unfortunately, none of the variations of quoting ($MODE or $TARGET1) or ${MODE}
I've tried causes the -w option to be accepted and appear at the bottom of $PAGE. 不幸的是,我尝试过的引用($ MODE或$ TARGET1)或${MODE}
的变化都没有导致-w选项被接受并出现在$ PAGE的底部。 It works fine when replacing $MODE
with the long version. 用长版本替换$MODE
时,它工作正常。
How can this be made to work? 怎么能这样做?
One way to do it 一种方法
w=(
'\nEffective URL: %{url_effective}'
'\nSize: %{size_download}'
'\nTotal time: %{time_total}'
'\nRedirect URL: %{redirect_url}'
)
curl -Ss --include --location --config curl.config -w "${w[*]}" icanhazip.com
When you do it your way, word splitting is happening, so the -w
string is split up on each space instead of being passed as a single string. 当你按照自己的方式进行时,会发生分词,因此-w
字符串会在每个空格中分开,而不是作为单个字符串传递。
$ set -x
$ : curl $MODE --include --location --config curl.config icanhazip.com
+ : curl -sS -w '"\nEffective' URL: '%{url_effective}' '\nSize:' '%{size_download}' '\nTotal' time: '%{time_total}' '\nRedirect' URL: '%{redirect_url}"' --include --location --config curl.config icanhazip.com
Another (similar) way. 另一种(类似的)方式。 Would also suggest you quote variables like URI . 还建议你引用像URI这样的变量。 A second point is to be careful with upper-case variable names; 第二点是要注意大写变量名称; as they can easilly crash with environment variables etc. 因为他们可以轻松地与环境变量等崩溃
#!/bin/bash
url="$1"
w=("-sS"
"-w
Effective URL: %{url_effective}
Size : %{size_download}
Total time : %{time_total}
Redirect URL : %{redirect_url}"
)
page="$(curl "${w[@]}" --include --location --config curl.config "$url")"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.