最近在写一个模仿dropbox_uploader的百度网盘的shell脚本。通过调用pcs的api返回一个目录下的所有文件。返回的json数据如下:
{"list":[{"fs_id":1878243434,"path":"\/apps\/zjhui\/test","ctime":1392866001,"mtime":1392866001,"md5":"","size":0,"isdir":1},{"fs_id":3901347206,"path":"\/apps\/zjhui\/upload","ctime":1392860719,"mtime":1392860719,"md5":"","size":0,"isdir":1},{"fs_id":3190602252,"path":"\/apps\/zjhui\/aaaaaaaa","ctime":1392885465,"mtime":1392885465,"md5":"783b7e40cdfb4a1344d15b1f7081af66","size":35121,"isdir":0},{"fs_id":3734744594,"path":"\/apps\/zjhui\/baidu-pcs.txt","ctime":1390898949,"mtime":1390898949,"md5":"5ccce0498cf29996399750789f415bb8","size":450,"isdir":0},{"fs_id":4176732013,"path":"\/apps\/zjhui\/bash","ctime":1389845838,"mtime":1389845838,"md5":"286dc423e71186936c39e3507d2d0cf1","size":2076,"isdir":0},{"fs_id":1122970857,"path":"\/apps\/zjhui\/LICENSE","ctime":1391567032,"mtime":1391567032,"md5":"783b7e40cdfb4a1344d15b1f7081af66","size":35121,"isdir":0},{"fs_id":3086666598,"path":"\/apps\/zjhui\/LICENSE-0205","ctime":1391569592,"mtime":1391569592,"md5":"783b7e40cdfb4a1344d15b1f7081af66","size":35121,"isdir":0},{"fs_id":4120839796,"path":"\/apps\/zjhui\/LICENSE01","ctime":1391569447,"mtime":1391569447,"md5":"783b7e40cdfb4a1344d15b1f7081af66","size":35121,"isdir":0},{"fs_id":2922247596,"path":"\/apps\/zjhui\/Pcs.sh","ctime":1389857060,"mtime":1389857060,"md5":"aaeafb64fc4efcff24de3b4e92dc302f","size":3355,"isdir":0},{"fs_id":1874426057,"path":"\/apps\/zjhui\/Pcs_Uploader.sh","ctime":1389773672,"mtime":1389773672,"md5":"14d6e4f7b9daf7761a7f2aa83efaa64d","size":2653,"isdir":0},{"fs_id":2295037712,"path":"\/apps\/zjhui\/test1","ctime":1392869356,"mtime":1392869356,"md5":"5fceae3ba3777db47ed1cc6a9a96cf27","size":728,"isdir":0}],"request_id":4180656241}
利用JSON.sh解析后如下:
["list",0,"fs_id"] 1878243434
["list",0,"path"] "\/apps\/zjhui\/test"
["list",0,"ctime"] 1392866001
["list",0,"mtime"] 1392866001
["list",0,"md5"] ""
["list",0,"size"] 0
["list",0,"isdir"] 1
["list",1,"fs_id"] 3901347206
["list",1,"path"] "\/apps\/zjhui\/upload"
["list",1,"ctime"] 1392860719
["list",1,"mtime"] 1392860719
["list",1,"md5"] ""
["list",1,"size"] 0
["list",1,"isdir"] 1
["list",2,"fs_id"] 3190602252
["list",2,"path"] "\/apps\/zjhui\/aaaaaaaa"
["list",2,"ctime"] 1392885465
["list",2,"mtime"] 1392885465
["list",2,"md5"] "783b7e40cdfb4a1344d15b1f7081af66"
["list",2,"size"] 35121
["list",2,"isdir"] 0
["list",3,"fs_id"] 3734744594
["list",3,"path"] "\/apps\/zjhui\/baidu-pcs.txt"
["list",3,"ctime"] 1390898949
["list",3,"mtime"] 1390898949
["list",3,"md5"] "5ccce0498cf29996399750789f415bb8"
["list",3,"size"] 450
["list",3,"isdir"] 0
["list",4,"fs_id"] 4176732013
["list",4,"path"] "\/apps\/zjhui\/bash"
["list",4,"ctime"] 1389845838
["list",4,"mtime"] 1389845838
["list",4,"md5"] "286dc423e71186936c39e3507d2d0cf1"
["list",4,"size"] 2076
["list",4,"isdir"] 0
["list",5,"fs_id"] 1122970857
["list",5,"path"] "\/apps\/zjhui\/LICENSE"
["list",5,"ctime"] 1391567032
["list",5,"mtime"] 1391567032
["list",5,"md5"] "783b7e40cdfb4a1344d15b1f7081af66"
["list",5,"size"] 35121
["list",5,"isdir"] 0
["list",6,"fs_id"] 3086666598
["list",6,"path"] "\/apps\/zjhui\/LICENSE-0205"
["list",6,"ctime"] 1391569592
["list",6,"mtime"] 1391569592
["list",6,"md5"] "783b7e40cdfb4a1344d15b1f7081af66"
["list",6,"size"] 35121
["list",6,"isdir"] 0
["list",7,"fs_id"] 4120839796
["list",7,"path"] "\/apps\/zjhui\/LICENSE01"
["list",7,"ctime"] 1391569447
["list",7,"mtime"] 1391569447
["list",7,"md5"] "783b7e40cdfb4a1344d15b1f7081af66"
["list",7,"size"] 35121
["list",7,"isdir"] 0
["list",8,"fs_id"] 2922247596
["list",8,"path"] "\/apps\/zjhui\/Pcs.sh"
["list",8,"ctime"] 1389857060
["list",8,"mtime"] 1389857060
["list",8,"md5"] "aaeafb64fc4efcff24de3b4e92dc302f"
["list",8,"size"] 3355
["list",8,"isdir"] 0
["list",9,"fs_id"] 1874426057
["list",9,"path"] "\/apps\/zjhui\/Pcs_Uploader.sh"
["list",9,"ctime"] 1389773672
["list",9,"mtime"] 1389773672
["list",9,"md5"] "14d6e4f7b9daf7761a7f2aa83efaa64d"
["list",9,"size"] 2653
["list",9,"isdir"] 0
["list",10,"fs_id"] 2295037712
["list",10,"path"] "\/apps\/zjhui\/test1"
["list",10,"ctime"] 1392869356
["list",10,"mtime"] 1392869356
["list",10,"md5"] "5fceae3ba3777db47ed1cc6a9a96cf27"
["list",10,"size"] 728
["list",10,"isdir"] 0
["request_id"] 1194090611
我想通过解析这json数据,然后进行组装,最后得到类似于./dropbox.sh list
的效果:
[F] 35147 aaaaaaa
[F] 9524791 Agile_Web_Development_with_Rails_4th_for_Rails_3.2.pdf
[D] 0 Camera Uploads
[D] 0 Photos
[D] 0 Public
Please install jshon yourself. To sort by yourself, follow the sort command as needed.
Use this: https://github.com/ddopson/underscore-cli, based on node.js.
It’s great to use nodejs, bash script maintenance is not easy
Get it done in minutes, easy to understand and maintain.
Assume that the file is data.json and the js file is parse.js
Just use node parse.js. If you don’t want the parse.js file, you can use the -e parameter of node in the bash script to run the js string.