2015-11-05 122 views
1

我有一個json文件名爲test.json,下面的內容。閱讀JSON文件並解析以獲得shell腳本中的元素值

{ 
     "run_list": ["recipe[cookbook-ics-op::setup_server]"], 
    "props": { 
     "install_home": "/test/inst1", 
      "tmp_dir": "/test/inst1/tmp", 
     "user": "tuser 
       } 
} 

我想讀這個文件到shell腳本&一個變量然後提取install_home,用戶&值TMP_DIR使用EXPR。有人可以幫忙嗎?

道具= cat test.json

工作得到JSON文件到一個變量。現在我怎樣才能使用expr提取這些值。任何幫助將不勝感激。

+0

請勿使用'expr'或正則表達式(anticipatin g一些討厭的基於正則表達式的答案)。使用適當的JSON解析器,如'jq'。 –

回答

0

對於純bash的解決辦法,我建議這樣的:
github.com/dominictarr/JSON.sh
它可以這樣來使用:

./json.sh -l -p < example.json 

打印輸出,如:

["name"]  "JSON.sh" 
["version"]  "0.2.1" 
["description"] "JSON parser written in bash" 
["homepage"] "http://github.com/dominictarr/JSON.sh" 
["repository","type"] "git" 
["repository","url"] "https://github.com/dominictarr/JSON.sh.git" 
["bin","JSON.sh"]  "./JSON.sh" 
["author"]  "Dominic Tarr <[email protected]> (http://bit.ly/dominictarr)" 
["scripts","test"]  "./all-tests.sh" 

從這裏achive什麼很瑣碎您正在尋找