2017-09-04 53 views
0

curl命令來傳遞參數的ICINGA API中:CURL API來傳遞參數在bash腳本

我有一個curl命令,並通過它在bash腳本,我需要有POST方法兩個變量此URL,如何將參數傳遞到curl命令

curl -k -s -u 'root:icinga' -H 'Accept: application/json' \ 
    -X POST 'https://sample.com:5665/v1/actions/acknowledge-problem?type=Service' \ 
    -d '{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {\"$1\} && service.name == {\"$2\}"" }''' \ 
    | python -m json.tool 

的$ 1和$ 2應該有分別的主機名和servicenames

請幫

感謝 Aravind

回答

1

如果在bash中使用單引號('like this'),則會得到一個不帶可變擴展的字符串。也就是說,比較:

$ echo '$DISPLAY' 
$DISPLAY 

有了:

$ echo "$DISPLAY" 
:0 

這也正是同樣的情況在你curl命令行,那就是你有:

'{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {\"$1\} && service.name == {\"$2\}"" }''' 

有實際工作中的一些在最後引用'''開頭的問題,並在最後的}之前包括""。如果你想讓這些變量擴大,你需要把它們移到你的單引號之外。你可以這樣做:

'"host.name == {"'"$1"'"} && ...' 

在這種情況下,"$1"外單引號。或者,你可以這樣做:

"\"host.name == {\"$1\"} ** ..." 

這裏,我們只是用在外面的雙引號,所以變量擴展工作正常,但我們必須轉義字符串內的每個文字"

使用第一個選項,最後一個參數-d看起來像這樣的事情(「東西」,因爲我不熟悉icinga):

'{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {"'"$1"'"} && service.name == {"'"$2"'"}}' 

如果$1foo$2bar,這給你:

{ "author": "icingaadmin", "comment": " Working on it.", "notify": true, "filter": "host.name == {"foo"} && service.name == {"bar"}} 
+0

我無法執行相同的,看起來像轉義序列仍然無法正常工作。 – masteraravind

+0

它以什麼方式失敗? – larsks