Maison > Questions et réponses > le corps du texte
最近在写一个模仿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
黄舟2017-04-17 11:36:37
$ cat data | jshon -e list -a -e isdir -u -p -e size -u -p -e path -u | sed 'N;N;s/^1/[D]/;s/^0/[F]/;s/\n/\t/g'
[D] 0 /apps/zjhui/test
[D] 0 /apps/zjhui/upload
[F] 35121 /apps/zjhui/aaaaaaaa
[F] 450 /apps/zjhui/baidu-pcs.txt
[F] 2076 /apps/zjhui/bash
[F] 35121 /apps/zjhui/LICENSE
[F] 35121 /apps/zjhui/LICENSE-0205
[F] 35121 /apps/zjhui/LICENSE01
[F] 3355 /apps/zjhui/Pcs.sh
[F] 2653 /apps/zjhui/Pcs_Uploader.sh
[F] 728 /apps/zjhui/test1
jshon 请自己安装。要排序自己按需求接 sort 命令。
天蓬老师2017-04-17 11:36:37
用nodejs多好,bash脚本维护可不容易啊
var data = require('./data').list;
function pad(s, l) {
return s + (new Array(l - s.toString().length)).join(' ');
}
data.forEach(function (d) {
var type = d.isdir ? 'D':'F';
process.stdout.write('[' + type + '] ');
process.stdout.write(pad(d.size, 10));
process.stdout.write(d.path + '\n');
})
分分钟搞定,简单易懂可维护。
假设文件是data.json, js文件是parse.js
直接 node parse.js就行了,如果不想要parse.js文件可以在bash脚本里使用node的-e参数运行js字符串。