Advertisement
Guest User

Untitled

a guest
Mar 8th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.83 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. set -e
  4. shopt -s extglob
  5.  
  6. TEMPFILE=image.png TEMPDIR= DIR="$(pwd)"
  7.  
  8. usage () {
  9. cat <<EOF
  10. This script saves image from cliboard into
  11. a png file which name is sha256sum of that
  12. image.
  13. This script uses xclip and therefore is
  14. valid only for X11 sessions.
  15.  
  16. Options:
  17. -1          Nothing will work without
  18.             this option. Don't ask why.
  19. -h (--help):        Show this help.
  20. -d (--directory):   Specify target directory.
  21.             Otherwise current is used.
  22. -t (--tempdir):     Set directory for temp file.
  23. EOF
  24. }
  25.  
  26. create_tempfile () {
  27.     local dirs=("/tmp/$USER" "/tmp/_$USER" "/tmp/__$USER") \
  28.         files=(".image" "._image" ".__image")
  29.  
  30.     for dir in "${dirs[@]}"
  31.     do
  32.         if [[ -d $dir ]]
  33.         then
  34.             for file in "${files[@]}"
  35.             do
  36.                 if [[ ! -a $dir/$file ]]
  37.                 then
  38.                     TEMPDIR="$dir"
  39.                     TEMPFILE="$file"
  40.                     return 0
  41.                 fi
  42.             done
  43.         else
  44.             mkdir "$dir"
  45.             TEMPDIR="$dir"
  46.             TEMPFILE="${files[0]}"
  47.             return 0
  48.         fi
  49.     done
  50.     [[ -z $TEMPDIR ]] && return 1
  51. }
  52.  
  53. if [[ -z $1 || $1 = @(-h|--help) ]]
  54. then
  55.     usage
  56.     exit $(( $# ? 0 : 1 ))
  57. fi
  58.  
  59. while (( $# ))
  60. do
  61.     case $1 in
  62.         -1)
  63.             GO=1
  64.             shift
  65.             ;;
  66.         -d|--directory)
  67.             DIR="$2"
  68.             shift 2
  69.             ;;
  70.         -t|--tempdir)
  71.             TEMPDIR="$2"
  72.             shift 2
  73.             ;;
  74.         *)
  75.             echo "Unknown option '$1'."
  76.             echo
  77.             usage
  78.             exit 1
  79.             ;;
  80.     esac
  81. done
  82.  
  83. if (( ! GO ))
  84. then
  85.     usage
  86.     exit 1
  87. fi
  88.  
  89. [[ -z $TEMPDIR ]] &&
  90. if ! create_tempfile
  91. then
  92.     echo "Could not create tempfile, try -t."
  93.     exit 1
  94. fi
  95.  
  96.  
  97. for dir in "$DIR" "$TEMPDIR"
  98. do
  99.     if [[ ! -d $dir ]]
  100.     then
  101.         echo "'$dir' is not a directory."
  102.         exit 1
  103.     fi
  104. done
  105.  
  106. touch "$TEMPDIR/$TEMPFILE"
  107.  
  108.  
  109.  
  110. xclip -t image/png -selection c -o > "$TEMPDIR/$TEMPFILE"
  111. sha256sum - <"$TEMPDIR/$TEMPFILE" | cut -d' ' -f 1 |
  112.     cp "$TEMPDIR/$TEMPFILE" "$DIR/$( cat - ).png"
  113. rm "${TEMPDIR:?}/$TEMPFILE"
  114. rmdir "$TEMPDIR" 2>/dev/null || true
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement