RemoveSyncthingConflicts.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. #
  3. # RemoveSyncthingConflicts.sh
  4. #
  5. # Acts as you'd expect, displays syncthing conflict files, with the option to
  6. # delete them all. Dependent on the script syncthing_findconflicts.py by
  7. # sercaxto (Georg Luntz) as published here:-
  8. # https://github.com/sercxanto/small_scripts/blob/master/syncthing_findconflicts.py
  9. #
  10. # TODO: Reimplement this in python as a syncthing_findconflicts.py fork
  11. #
  12. # FUNCTIONS
  13. #
  14. # Remove the temp file, if it exists
  15. function DeleteTemp {
  16. if [ -e /tmp/SynConList ]
  17. then
  18. echo Cleaning up
  19. rm /tmp/SynConList
  20. fi
  21. }
  22. # Check and fix dependency
  23. function DependencyFix {
  24. if [ $(which syncthing_findconflicts.py) ]
  25. then
  26. # syncthing_findconflicts.py found
  27. return
  28. else
  29. echo Missing dependency, attempting to fix
  30. # Download dependncy to /usr/bin
  31. curl https://raw.githubusercontent.com/sercxanto/small_scripts/master/syncthing_findconflicts.py > /usr/bin/syncthing_findconflicts.py
  32. # Check that file was created succesfully
  33. if [ -e /usr/bin/syncthing_findconflicts.py ]
  34. then
  35. echo Downloaded, making executable
  36. chmod +x /usr/bin/syncthing_findconflicts.py
  37. else
  38. echo "Failed to repair. Either run this script with sudo or install https://github.com/sercxanto/small_scripts/blob/master/syncthing_findconflicts.py to somewhere in your path manually"
  39. exit
  40. fi
  41. fi
  42. }
  43. #
  44. # MAIN SCRIPT
  45. #
  46. # Check/fix dependencies
  47. DependencyFix
  48. # Run syncthing_findconflicts.py for user output
  49. syncthing_findconflicts.py
  50. # Blank out a temporary list file
  51. cat /dev/null > /tmp/SynConList
  52. # Output list of all syncthing conflict files
  53. # 1. grep to find all the lines that start with some whitespace followed by a forward slash
  54. # 2. sed to remove any of the trailing blank space, ready to delete
  55. syncthing_findconflicts.py | grep '^[ ]*/' | sed s/\ *// > /tmp/SynConList
  56. # Count found conflicts
  57. line_count=$(cat /tmp/SynConList | wc -l )
  58. # Wipe temp file and quit if nothing found
  59. if [ $line_count = 0 ]
  60. then
  61. DeleteTemp
  62. exit
  63. fi
  64. # Ask user if they want files removed
  65. echo
  66. echo "Do you want to remove all conflicts? (y/N)"
  67. read userchoice
  68. # Checl if user response starts with a "Y" (case insensitive)
  69. if [ $(echo $userchoice | grep -i ^y | wc -l) -gt 0 ]
  70. then
  71. echo Deleting...
  72. # Counter is zero, compared to ensure it is below the total number of lines in the list
  73. counter=0
  74. while [ $counter -lt $line_count ]; do
  75. # Immediatly incremented so it matches first line/last line
  76. let counter=$counter+1
  77. # Set current line - only output counter amount of lines, and take the bottom one
  78. # As counter increases, each line in turn is captured
  79. current=$(cat /tmp/SynConList | head -n$counter | tail -n1)
  80. echo Removing $current
  81. rm "$current"
  82. done
  83. # If user responds "N"
  84. elif [ $(echo $userchoice | grep -i ^n | wc -l) -gt 0 ];
  85. then
  86. echo "Files will remain on disk"
  87. else
  88. # User responds neither yes nor no
  89. echo "Please answer yes or no - exiting"
  90. fi
  91. # Wipe temp file
  92. DeleteTemp