Guest User

Untitled

a guest
Feb 27th, 2018
141
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. clear
  3. keep_clip_log=false
  4. log_size=15000  #in lines, content will be truncated when it goes above this. each entry is added on top.
  5.         #an average page of text editor is 40-50 lines. 15000 is 300 pages (about 300 lage clipboard entries, 900 small ones)
  6. poll_lague=0.5 #interval of time in seconds between each check
  7. clipfile="/home/$(whoami)/clipcheck"
  8. clipback="/home/$(whoami)/clipback"
  9. cliplog="/home/$(whoami)/cliplog"
  10. scratch="/tmp"
  11. del=srm
  12. d_arg0="-llz"
  13.  
  14. error_string="Error: target STRING not available"
  15. dbg=true #enable debug
  16.  
  17. #prep work
  18.  
  19.     #if files don't exist, create them
  20.     if [ ! -f "$clipback" ]; then echo -n "">"$clipback";fi
  21.     if [ ! -f "$cliplog" ]; then echo -n "">"$cliplog";fi
  22.     if [ ! -f "$clipfile" ]; then echo -n "">"$clipfile";fi
  23.  
  24. #main function
  25.  
  26. while true; do
  27.                             if [ $dbg = true ]; then echo -e "$(date) : CHECKING clipboard..."; fi
  28.     nohup xclip -selection clipboard -o>"$clipfile"  2> /dev/null &
  29.     sleep $poll_lague
  30.                             if [ $dbg = true ]; then echo "$(date) : finished. CONTENT: $(cat $clipfile|head -n 1)";fi
  31.     if [ -z "$(cat $clipfile)" ]; then
  32.                             if [ $dbg = true ]; then echo "$(date) : WAYLAND DROPPED THE CLIPBOARD, Restoring..."; fi
  33.         cat "$clipback"|xclip -selection clipboard 2> /dev/null    
  34.                             if [ $dbg = true -a "$?" = 0 ]; then echo "$(date) : restored....";fi
  35.     else
  36.                             if [ $dbg = true ]; then echo "$(date) : CLIPBOARD INTACT"; fi
  37.         clip_hash=$(sha256sum "$clipfile"|awk "{print \$1}")
  38.         back_hash=$(sha256sum "$clipback"|awk "{print \$1}")
  39.         if [ "$clip_hash" = "$back_hash" ];then
  40.             echo -n ""
  41.                             if [ $dbg = true -a "$?" = 0 ]; then echo "$(date) : still the SAME...";fi
  42.  
  43.         else
  44.             if [ $keep_clip_log = true ]; then
  45.                 temp0="$scratch/$(date +%s%N)"
  46.                 cliplines=$(cat "$clipback"|wc -l)
  47.                     cliplines=$(expr $cliplines + 8)
  48.                     sir_imp=$(cat "$cliplog"|wc -l)
  49.                     cliplines=$(expr $cliplines + $sir_imp)
  50.                     delta=$(expr $log_size - $cliplines)           
  51.                 mv "$cliplog" "$temp0"
  52.                 echo -e "\n\n\
  53.                     \n||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\
  54.                     \n|||||||||||||||||||||||||||||||||||||||||||||||||||CLIP|||||||||||||||||||||||||||||||||||||||||||||||||||\
  55.                     \n||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\
  56.                     \n\n">"$cliplog"
  57.                 cat "$clipback">>"$cliplog"
  58.                
  59.                 if [ $delta -ge 0 ]; then
  60.                     cat "$temp0">>"$cliplog"
  61.                 else
  62.                     cat "$temp0"|head -n $delta >> "$cliplog"
  63.                 fi
  64.                 $del $d_arg0 "$temp0"
  65.             fi           
  66.             cp "$clipfile" "$clipback"
  67.                             if [ $dbg = true -a "$?" = 0 ]; then echo "$(date) : it has CHANGED and was backed up...";fi
  68.         fi         
  69.     fi 
  70.    
  71.     if [ $dbg = true ]; then echo "----------------------------------------------";fi
  72. done
  73.  
  74.  
  75. exit
  76. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  77. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  78. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  79. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  80. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  81. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  82. #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
RAW Paste Data