1

我可以将注释(注释掉的代码或不影响脚本的注释)放在 shell 脚本的单引号内吗?

我尝试了以下方法来使这些工作:

# comment
\# comment
\/\* comment \*\/

下面是我正在尝试做的一个例子。你会看到我的评论以散列开头,并且在 curl 请求的数据包内的单引号内。

curl -XPOST 'http://ec2-54-234-64-66.compute-1.amazonaws.com:9200/nsns/nsn/_mapping?pretty=true' -d '{
    "nsn" : {

        "type" : "object",
        "include_in_all" : "false",
        "index" : "no",
        "properties" : {

        # this data is used for facetting END

            "id" : { "type" : "long", "include_in_all" : "true" },
            "BDC_CODE" : { "type" : "byte" },
            "KID" : { "type" : "byte" },
            "ITEM_STANDARDIZATION_CODE" : { "type" : "string" },

        # we don't know what these field's data look like yet

            "stock_on_hand" : { "type" : "integer" },
            "non_stocked_items" : { "type" : "integer" },
            "stocked_restrictions" : { "type" : "string" },
            "consistency_of_spend" : { "type" : "string" },
            "WSDC" : { "type" : "string" }

        }
    }
}'
4

2 回答 2

2

问题不是单引号,而是使用JSON. AFAIK,您不能在 中添加评论JSON,请参阅Can comments be used in JSON?

笔记 :

你应该添加-H "Content-Type: text/json"你的cURL

于 2013-01-09T23:19:55.957 回答
1

您不能将它们放在引号内,但您可以将字符串分解,以便保留注释:

json='{
    "nsn" : {

        "type" : "object",
        "include_in_all" : "false",
        "index" : "no",
        "properties" : {
'

# this data is used for facetting END
json+='
            "id" : { "type" : "long", "include_in_all" : "true" },
            "BDC_CODE" : { "type" : "byte" },
            "KID" : { "type" : "byte" },
            "ITEM_STANDARDIZATION_CODE" : { "type" : "string" },
'

# we don't know what these field's data look like yet
json+='
            "stock_on_hand" : { "type" : "integer" },
            "non_stocked_items" : { "type" : "integer" },
            "stocked_restrictions" : { "type" : "string" },
            "consistency_of_spend" : { "type" : "string" },
            "WSDC" : { "type" : "string" }
        }
    }
}'

url='http://ec2-54-234-64-66.compute-1.amazonaws.com:9200/nsns/nsn/_mapping?pretty=true'

curl -XPOST "$url" -d "$json"

请注意,这+=是一个特定于 bash 的字符串连接赋值运算符。

于 2013-01-10T01:25:17.163 回答