Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 我在eval with}中遇到一个解析错误_Bash - Fatal编程技术网

Bash 我在eval with}中遇到一个解析错误

Bash 我在eval with}中遇到一个解析错误,bash,Bash,下面是我的简单Bash脚本: curljson() { eval "curl $* | python -m json.tool" } 它只是以可读的方式输出JSON 但当我这样做的时候: curljson -X PUT -d '{"settings": {"number_of_shards": 3, "number_of_replicas": 1}}' http://192.168.1.111:9200/blogs 我得到这个错误: (eval):1: parse error

下面是我的简单Bash脚本:

curljson() {
        eval "curl $* | python -m json.tool"
}
它只是以可读的方式输出JSON

但当我这样做的时候:

curljson -X PUT -d '{"settings": {"number_of_shards": 3, "number_of_replicas": 1}}' http://192.168.1.111:9200/blogs
我得到这个错误:

(eval):1: parse error near `}'
但当我卷曲的时候,它就起作用了,所以它似乎是我的剧本


那么,我如何让这个Bash脚本接受
}

您根本不需要
评估

curljson() {
    curl "$@" | python -m json.tool
}

curljson
真正做的就是将其所有参数原样传递给
curl
,然后将输出通过管道传递给Python脚本
“$@”
扩展到函数作为参数接收的相同字串。

此处不要使用
eval
。你不需要它,这是一个巨大的安全问题。在这里使用
$*
也是“错误的”。您应该使用
curl“$@”| python-m json.tool
(并确保像在示例中那样单独传递参数)。如果将
set-x
添加到函数顶部,并将
set+x
添加到底部,您可能会更好地看到解析错误的来源。(您对
$*
eval
的使用共同导致了问题。尽管在这里使用
“$@”
也会出现
eval
的问题。您对行的评估过高。)