Bash,curl,eval和whitespace [英] Bash, curl, eval and whitespace

查看:222
本文介绍了Bash,curl,eval和whitespace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个脚本来为给定的用户输入执行CURL命令。该脚本有多个辅助函数,用于创建最终传递给CURL的参数列表(参数)。



一个剥离的例子如下:

 #!/ bin / bash 
function create_arg_list
{
#这个HTTP HEADER VALUE COMES FROM THE USER和MAY CONTAIN SPACES
local __hdr =x-madhurt-test:madh urt
local __params =http://google.co.in -X GET -H'$ __ hdr'

local __resultvar =$ 1
eval $ __ resultvar =\$ __ params\
echoparams after eval:$ __ resultvar
}


create_arg_list arg_list
echoparams after eval in main code:$ arg_list

echo运行命令:curl -v $ arg_list
curl -v $ arg_list

脚本在输入参数(文件路径, ..)有(引用)其中的空格。但是,当应该作为HTTP头传递给CURL的参数包含空格时,脚本会失败。



这是我试过的:


  1. 在标题值周围使用单引号(例如'$ __ hdr')。但是,传递给CURL的值为:

     curl -v  http:// google。 co.in  -X GET -H'x-madhurt-test:madh urt'

    ,它被当作CURL处理,发送的实际头像这样: pre>'x-madhurt-test:madh


  2. 双重转义标头值(例如\\$ __ hdr \\ ),但这似乎也工作得很好。在这种情况下,CURL获取urt作为单独的参数,并将其视为一个URL

     curl:(6)无法解析主机'urt''


  3. 转换标题值中的空格(即使用madh\ urt而不是madh urt),但结果与选项2相同。

有人对这里发生的错误有什么见解吗?

解决方案

p>这段代码工作,但它不打算使用as-is。我发布它给你一些想法如何继续。他们的关键,使你想做的工作是使用一个数组。不幸的是,Bash不能从函数返回数组你可能应该做的是使用一个全局数组下面的代码,但是,创建一个声明语句的字符串,并传递

 #!/ bin / bash 
function create_arg_list
{
#这个HTTP HEADER值来自用户,可以包含空格
local __hdr =x-madhurt-test:madh urt
local __params =http: co.in -X GET -H
__params =($ __ params$ __ hdr)

local __resultvar =$ 1
eval $ __ resultvar = $(printf% q$(declare -p __params))
echoparams after eval:$ __ resultvar
}

create_arg_list arg_list
echoparams after eval主代码:$ arg_list

echo运行命令:curl -v $ arg_list

eval $ arg_list

curl -v$ { __params [@]}


I'm writing a script to execute CURL commands for a given user input. The script has multiple helper function to create the list of parameters (arguments) that will eventually be passed to CURL.

A stripped out example, is as follows :

#!/bin/bash
function create_arg_list
{
    # THIS HTTP HEADER VALUE COMES FROM THE USER and MAY CONTAIN SPACES
 local __hdr="x-madhurt-test:madh urt"
 local __params="http://google.co.in -X GET -H '$__hdr'"

 local __resultvar="$1"
 eval $__resultvar="\"$__params\""
 echo "params after eval : $__resultvar"
}


create_arg_list arg_list
echo "params after eval in main code : $arg_list"

echo "Running command : curl -v $arg_list"
curl -v $arg_list

The script works great when the input parameters (file path, url etc..) have (quoted) white space in them. However, when the arguments that are supposed to be passed as HTTP Headers to CURL contain spaces, the script fails miserably.

Here is what I've tried :

  1. Use single quotes around the header value (e.g. '$__hdr'). However, with this the value that is passed to CURL is :

    curl -v http://google.co.in -X GET -H 'x-madhurt-test:madh urt'

    , which is treated as-as by CURL and the actual header that is sent is like this :

    'x-madhurt-test:madh

  2. Double escape the header value (e.g. \\"$__hdr\\"), but this does seem to work as well. In this case CURL gets "urt" as a separate parameter and treats it as a URL

    curl: (6) Couldn't resolve host 'urt"'

  3. Escape the space in the header value (i.e. use "madh\ urt" instead of "madh urt"), but this turns out to be the same as option 2.

Does someone have insights as to what is happening wrong here?

解决方案

This code works but it's not intended to use as-is. I'm posting it to give you some ideas for how you might proceed. They key to making what you want to do work is to use an array. Unfortunately, Bash can't return arrays from functions. What you probably ought to do is use a global array. The code below, however, creates a string out of a declare statement and passes that through your indirect variable. It's a seriously bad kludge.

#!/bin/bash
function create_arg_list
{
    # THIS HTTP HEADER VALUE COMES FROM THE USER and MAY CONTAIN SPACES
 local __hdr="x-madhurt-test:madh urt"
 local __params="http://google.co.in -X GET -H"
 __params=($__params "$__hdr")

 local __resultvar="$1"
 eval $__resultvar=$(printf "%q" "$(declare -p __params)")
 echo "params after eval : $__resultvar"
}

create_arg_list arg_list
echo "params after eval in main code : $arg_list"

echo "Running command : curl -v $arg_list"

eval $arg_list

curl -v "${__params[@]}"

这篇关于Bash,curl,eval和whitespace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆