bash 使用定义的 Content-Type 从 .sh 脚本运行 curl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12051329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:34:47  来源:igfitidea点击:

Run curl from .sh script with defined Content-Type

bashcurlsh

提问by kaspian

When I'm trying to run test.sh script I've always receive error from curl:

当我尝试运行 test.sh 脚本时,我总是收到来自 curl 的错误:

curl: (6) Couldn't resolve host 'application'

test.sh:

测试.sh:

#!/bin/sh
CT="Content-Type:\ application/json"

TEST="curl http://127.0.0.1 -H $CT"
echo $TEST

RESPONSE=`$TEST`
echo $RESPONSE

But if I just run following command from console everything fine:

但是如果我只是从控制台运行以下命令一切正常:

curl http://127.0.0.1 -H Content-Type:\ application/json

Could you please let me know what is wrong in script, as I understand something is wrong with 'space' escape, but have no idea how to fix it.

您能否让我知道脚本中有什么问题,因为我知道“空间”转义出了点问题,但不知道如何解决。

Also I've tried following combination, but result the same:

我也尝试过以下组合,但结果相同:

CT="Content-Type: application/json"
TEST="curl http://127.0.0.1 -H \"$CT\""

UPD:

更新:

bash / dash is only available on server. (/bin/sh --> bash)

bash / dash 仅在服务器上可用。(/bin/sh --> bash)

GNU bash, version 4.2.10(1)-release (x86_64-pc-linux-gnu)

回答by Kuf

Run the following: (delete the space after Content-Type)

运行以下命令:(删除Content-Type后的空格)

#!/bin/bash
CT="Content-Type:application/json"

TEST="curl http://127.0.0.1 -H $CT"
echo $TEST

RESPONSE=`$TEST`
echo $RESPONSE

回答by Karirovax Karirax

You can try with: bash -c your_bash_file.shIt worked for me with the same problem

您可以尝试:bash -c your_bash_file.sh它对我有同样的问题