Advertisement
Guest User

schnappi.sh

a guest
Feb 27th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #!/bin/bash
  2. clear
  3.  
  4. OUT="schnappi.qgroup.parsed"
  5. OUT2="schnappi.subvolume.parsed"
  6. OUT3="schnappi.schnapps.parsed"
  7. OUTFULL="schnappi.result"
  8.  
  9. IN="schnappi.qgroup"
  10. IN2="schnappi.subvolume"
  11. IN3="schnappi.schnapps"
  12.  
  13. FULLSIZE=0
  14. COMPACT=0
  15.  
  16. for arg in "$@"
  17. do
  18. if [ $arg == "-f" ]; then
  19. FULLSIZE=1
  20. fi
  21. if [ $arg == "-c" ]; then
  22. COMPACT=1
  23. fi
  24.  
  25. done
  26.  
  27. read HEIGHT WIDTH <<<$(stty size)
  28.  
  29. #exit 1
  30.  
  31.  
  32. trim() {
  33. local var="$*"
  34. echo "$var" | xargs
  35. }
  36.  
  37. >$OUT
  38. >$OUT2
  39. >$OUT3
  40. >$OUTFULL
  41.  
  42. btrfs quota enable /
  43. btrfs qgroup show / >$IN
  44. btrfs subvolume list / >$IN2
  45. schnapps list >$IN3
  46.  
  47. OLDIFS=$IFS
  48. IFS=" "
  49. I=0;
  50. while read ids all size
  51. do
  52. I=$(($I+1))
  53. if (( $I>2 )); then
  54. IDPARTS=(${ids//\// })
  55. id=${IDPARTS[1]}
  56. echo -e "$id;$size" >>$OUT
  57. fi
  58. done < $IN
  59. IFS=$OLDIFS
  60.  
  61. OLDIFS=$IFS
  62. IFS=" "
  63. I=0;
  64. while read non id non non non non non non schid
  65. do
  66. I=$(($I+1))
  67. if (( $I>0 )); then
  68. echo -e "$id;${schid//@/}" >>$OUT2
  69. fi
  70. done < $IN2
  71. IFS=$OLDIFS
  72.  
  73. OLDIFS=$IFS
  74. IFS=" "
  75. I=0;
  76. while IFS='|' read schid type date description
  77. do
  78. I=$(($I+1))
  79. if (( $I>2 )); then
  80. # date=$(trim "$date")
  81. echo -e "$(trim "$schid");$(trim "$type");$(trim "$date");$(trim "$description")" >>$OUT3
  82. fi
  83. done < $IN3
  84. IFS=$OLDIFS
  85.  
  86.  
  87. # ---- show info ----
  88.  
  89. unset qgroupsdata
  90. while IFS= read -r line
  91. do
  92. if [[ $(trim "$line") != "" ]]; then
  93. qgroupdata+=("$line")
  94. fi
  95. done < $OUT
  96.  
  97. #echo -e "${qgroupdata[0]}"
  98. #echo -e "${qgroupdata[1]}"
  99.  
  100.  
  101. unset subvolumedata
  102. while IFS= read -r line
  103. do
  104. if [[ $(trim "$line") != "" ]]; then
  105. subvolumedata+=("$line")
  106. fi
  107. done < $OUT2
  108.  
  109. #echo -e "${subvolumedata[0]}"
  110. #echo -e "${subvolumedata[1]}"
  111.  
  112.  
  113.  
  114. unset schnappsdata
  115. while IFS= read -r line
  116. do
  117. if [[ $(trim "$line") != "" ]]; then
  118. schnappsdata+=("$line")
  119. fi
  120. done < $OUT3
  121.  
  122. #echo -e "${schnappsdata[0]}"
  123. #echo -e "${schnappsdata[1]}"
  124.  
  125.  
  126. # --- print result ---
  127.  
  128. findsch() {
  129. local haystack="$1"
  130. local needle="$2"
  131.  
  132. unset schnappsdata
  133. while IFS= read -r line
  134. do
  135. if [[ $(trim "$line") != "" ]]; then
  136. schnappsdata+=("$line")
  137. fi
  138. done <"$haystack"
  139.  
  140. for itemsch in "${schnappsdata[@]}"
  141. do
  142. IFS=';'
  143. read id type date comment <<<"$itemsch"
  144. IFS=' '
  145. read dd dt dz <<<"$date"
  146. IFS=':'
  147. read th tm ts <<<"$dt"
  148. if [ "$id" == "$needle" ]; then
  149. echo "$id;$type;$dd $th:$tm;$comment"
  150. fi
  151. done
  152. }
  153.  
  154.  
  155. findqg() {
  156. local haystack="$1"
  157. local needle="$2"
  158.  
  159. unset data
  160. while IFS= read -r line
  161. do
  162. if [[ $(trim "$line") != "" ]]; then
  163. data+=("$line")
  164. fi
  165. done <"$haystack"
  166.  
  167. for item in "${data[@]}"
  168. do
  169. IFS=';'
  170. read id size <<<"$item"
  171. if [ "$id" == "$needle" ]; then
  172. echo "$size"
  173. fi
  174. done
  175. }
  176.  
  177.  
  178. for item in "${subvolumedata[@]}"
  179. do
  180. IFS=';'
  181. read btrfs schnapps <<<"$item"
  182.  
  183. result_schnapps="$(findsch "$OUT3" "$schnapps")"
  184.  
  185. if [[ "$result_schnapps" != "" ]]; then
  186. result_qgroup="$(findqg "$OUT" "$btrfs")"
  187. echo "$result_schnapps;$result_qgroup" >>$OUTFULL
  188. fi
  189.  
  190. done
  191.  
  192. sort $OUTFULL >"$OUTFULL.sorted"
  193. mv $OUTFULL.sorted $OUTFULL
  194.  
  195. unset data
  196. while IFS= read -r line
  197. do
  198. if [[ $(trim "$line") != "" ]]; then
  199. data+=("$line")
  200. fi
  201. done <"$OUTFULL"
  202.  
  203.  
  204. Sid=0
  205. Stype=0
  206. Sdate=0
  207. Scomment=0
  208.  
  209.  
  210.  
  211. for item in "${data[@]}"
  212. do
  213. IFS=';'
  214. read id type date comment size <<<"$item"
  215.  
  216. if [[ ${#id} -gt $Sid ]]; then Sid=${#id}; fi
  217. if [[ ${#type} -gt $Stype ]]; then Stype=${#type}; fi
  218. if [[ ${#date} -gt $Sdate ]]; then Sdate=${#date}; fi
  219. if [[ ${#comment} -gt $Scomment ]]; then Scomment=${#comment}; fi
  220. if [[ ${#size} -gt $Ssize ]]; then Ssize=${#size}; fi
  221. done
  222.  
  223.  
  224. Sfull=$(($Sid+$Stype+$Sdate+$Scomment+25))
  225.  
  226. if [[ $WIDTH -gt $Sfull ]]; then
  227. FULLSIZE=1
  228. fi
  229. if [[ $COMPACT == 1 ]]; then
  230. FULLSIZE=0
  231. fi
  232.  
  233.  
  234. if [[ $FULLSIZE == 1 ]]; then
  235.  
  236. printf "%${Sfull}s\n" |tr " " "-"
  237. printf "| %${Sid}s | %${Ssize}s | %-${Stype}s | %-${Sdate}s | %-${Scomment}s |\n" "ID" "Size" "Type" "Date" "Comment"
  238. printf "%${Sfull}s\n" |tr " " "-"
  239.  
  240. for item in "${data[@]}"
  241. do
  242. IFS=';'
  243. read id type date comment size <<<"$item"
  244. printf "| %${Sid}s | %${Ssize}s | %-${Stype}s | %-${Sdate}s | %-${Scomment}s |\n" "$id" "$size" "$type" "$date" "$comment"
  245. done
  246.  
  247. printf "%${Sfull}s\n" |tr " " "-"
  248.  
  249. else
  250.  
  251. Sfull=$(($Sid+$Stype+$Sdate+22))
  252. Sfullcommentcomplete=$(($Scomment+4))
  253. if [[ $Sfullcommentcomplete -gt $Sfull ]]; then
  254. Sfull=$Sfullcommentcomplete
  255. fi
  256. Sfullcomment=$(($Sfull-4))
  257.  
  258. printf "%-${Sfull}s\n" |tr " " "-"
  259. printf "| %-${Sfullcomment}s |\n" $(printf "%${Sid}s | %${Ssize}s | %-${Stype}s | %-${Sdate}s" "ID" "Size" "Type" "Date")
  260. printf "| %-${Sfullcomment}s |\n" ""
  261. printf "| %-${Sfullcomment}s |\n" "Comment"
  262. printf "| %${Sfullcomment}s |\n" |tr " " "-"
  263.  
  264. for item in "${data[@]}"
  265. do
  266. IFS=';'
  267. read id type date comment size <<<"$item"
  268.  
  269. printf "| %-${Sfullcomment}s |\n" $(printf "%${Sid}s | %${Ssize}s | %-${Stype}s | %-${Sdate}s" "$id" "$size" "$type" "$date")
  270. printf "| %-${Sfullcomment}s |\n" ""
  271. printf "| %-${Sfullcomment}s |\n" "$comment"
  272. printf "| %${Sfullcomment}s |\n" |tr " " "-"
  273. done
  274.  
  275.  
  276. fi
  277.  
  278. rm "$IN" "$IN2" "$IN3" "$OUTFULL" "$OUT" "$OUT2" "$OUT3"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement