Advertisement
Guest User

Untitled

a guest
Aug 14th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. #!bin/bash
  2.  
  3. # 读取头文件
  4. parse_yaml(){
  5. local prefix=$2
  6. local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
  7. # 1,10s 表示只看前10行。
  8. sed -ne "1,10s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
  9. -e "1,10s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
  10. awk -F$fs '{
  11. indent = length($1)/2;
  12. vname[indent] = $2;
  13. for (i in vname) {if (i > indent) {delete vname[i]}}
  14. if (length($3) > 0) {
  15. vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
  16. printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
  17. }
  18. }'
  19. }
  20.  
  21. showFileInfo(){
  22. oldIFS=$IFS
  23. IFS="#"
  24. #parse_yaml $1 "config_"
  25. eval $(parse_yaml $1 "config_")
  26.  
  27. tags=${config_tags//[$'\t\r\n']/}
  28. cates=${config_categories//[$'\t\r\n']/}
  29.  
  30. checkTags $1 $tags
  31. checkCategories $1 $cates
  32. IFS=$oldIFS
  33. }
  34.  
  35. checkTags(){
  36. tags=$(tag --list --no-name $1)
  37. if [ -z $tags ]
  38. then
  39. realtags='[无标签]'
  40. else
  41. # 两个斜杠表示替换所有
  42. realtags="[${tags//,/, }]"
  43. fi
  44.  
  45. if [ "$2" = "$realtags" ];
  46. then
  47. if [ $showInfo = true ]
  48. then
  49. echo $1 ' ' tags ✓ ' '
  50. fi
  51. else
  52. echo "$1 tags: \"$2\" --> \"$realtags\""
  53. if [ $fixit = true ]
  54. then
  55. # 整行替换以 “tags:” 开头的那一行
  56. sed -i '' "s/^[[:blank:]]*tags:.*/tags: $realtags/" $1
  57. fi
  58. fi
  59. }
  60.  
  61. checkCategories(){
  62.  
  63. # 截取子路径
  64. subpath=${1/$dirPath/}
  65. # 截取文件名
  66. filename=${1##*/}
  67. subpath=${subpath/$filename/}
  68.  
  69. cates=${2//[\[\] ]/}
  70. cates=/${cates//,/\/}/
  71.  
  72. if [ $subpath = $cates ]
  73. then
  74. if [ $showInfo = true ]
  75. then
  76. echo $1 ' ' categories ✓
  77. fi
  78. else
  79. #删除前面的 “/”
  80. catesname=${subpath#*\/}
  81. #删除前面的 “后面的 /”
  82. catesname=${catesname%/*}
  83. catesname="[${catesname/\//, }]"
  84.  
  85. echo "$1 categories: \"$2\" --> \"${catesname}\""
  86.  
  87. if [ $fixit = true ]
  88. then
  89. # 整行替换以 “categories:” 开头的那一行
  90. sed -i '' "s/^[[:blank:]]*categories:.*/categories: ${catesname}/" $1
  91. fi
  92. fi
  93. }
  94.  
  95. export -f showFileInfo
  96. export -f parse_yaml
  97. export -f checkTags
  98. export -f checkCategories
  99.  
  100. #export dirPath='.'
  101. export dirPath='../source/_posts'
  102.  
  103. export timelimit=3
  104. export fixit=false
  105. export showInfo=false
  106.  
  107. # 读取用户设定
  108. readArgs(){
  109.  
  110. echo '默认目录:' $dirPath
  111. read -p '更改目录请输入(不更改直接回车):' arg
  112. if [ -n "$arg" ]
  113. then
  114. dirPath=$arg
  115. fi
  116.  
  117.  
  118. read -p "时间限定(天数 ${timelimit}天,不更改直接回车):" arg
  119. if [ -n "$arg" ]
  120. then
  121. timelimit="-${arg}d" # 时间限制
  122. else
  123. timelimit="-${timelimit}d"
  124. fi
  125.  
  126.  
  127. read -p '自动修改请输入“fix”,否则只作检查:' arg
  128. if [ "$arg" = "fix" ]
  129. then
  130. fixit=true
  131. fi
  132.  
  133. read -p '是否显示所有信息?(输入 y 确认):' arg
  134. if [ "$arg" = "y" ]
  135. then
  136. showInfo=true
  137. fi
  138. }
  139.  
  140. readArgs
  141.  
  142. find $dirPath -type f -name "*.md" | xargs -n 1 -P 10 -I {} bash -c 'showFileInfo "$@"' _ {} \;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement