在shell脚本中的单引号内添加注释?

时间:2013-01-09 23:16:58

标签: shell hash escaping comments

我可以在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" }

        }
    }
}'

2 个答案:

答案 0 :(得分:2)

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

注意:

您应在-H "Content-Type: text/json"

中添加cURL

答案 1 :(得分: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的字符串连接赋值运算符。

相关问题