最近在写一个模仿dropbox_uploader的百度网盘的shell脚本。通过调用pcs的api返回一个目录下的所有文件。返回的json数据如下:
{"list":[{"fs_id":1878243434,"path":"\/apps\/zjhui\/test","ctime":1392866001,"mtime":1392866001,"md5":""
利用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
$ 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 命令。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。