Guest User

Untitled

a guest
Apr 2nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.00 KB | None | 0 0
  1. #!/bin/bash
  2. #Csandriel
  3. #08.03.2018
  4.  
  5. #wallshow
  6.  
  7. #The script is intended to periodicaly change wallpapers. It sets random
  8. #images from certain directory. It has the ability to change chedule on the fly
  9.  
  10. #time interval can been set with '-p' key, in such the notations:
  11. #wallshow -p 5min
  12. #wallshow -p 2hours
  13. #wallshow -p 5days
  14. #Called without  any key, the script just does randomly wallpaper change,
  15. #not affecting chedule if it is set.
  16. #Every new invocation of -p key erases the chedule previosly set
  17. #Key -s stops wallshow, leaving current wallpaper remained, and removing chedule.
  18. #Key -d deletes the source file of current wallpaper
  19. #Key -u refreshes the images-list of script
  20.  
  21. #The command line that performs setting of wallpaper
  22. #on the particular system. I use my own scrpipt
  23. #called "setwallpaper"
  24. #that looks like this:
  25. #---------------------------------------
  26. # ! /bin/bash
  27. # cp -f "$1"  /home/user/wallpaper.img
  28. # feh --bg-fill /home/user/wallpaper.img
  29. #---------------------------------------
  30. #Remember QUOTES here:
  31. PERFORM="/home/bin/setwallpaper"
  32.  
  33. #####  TODO:пока что отказывается корректно работать сброс рут-прав, если запущено от рута.
  34. #####  Поэтому в таком виде нужно следить чтобы запускался скрипт из под одного пользователя
  35. #####  Иначе будет существовать столько расписаний смены обоев, сколько пользователей
  36. #####  запускали скрипт
  37. #####
  38. #USER=user
  39. #if launched as root then restart script as user
  40. #if [ $UID -eq 0 ] ; then echo "DROP ROOT & recurse"; echo "$0 $@"; su "$USER" -c "$0 $@"; exit; fi
  41.  
  42.  
  43. #the directory where wallpapers are stored:
  44. WS_DIR=/data0/graph/wallpaper
  45.  
  46. #list of graphic files found in WS_DIR:
  47. WS_FILE=/home/bin/.wallshow.lst
  48. #Generating/Updating of the list
  49. #may be done with '-u' key
  50.  
  51. #count of lines in wallpapers-list:
  52. WS_FILE_LINES=$(echo $(wc -l $WS_FILE)|sed 's_\([^ ]*\).*_\1_')
  53.  
  54. #id of the display wich wallshow must be performed for:
  55. DISPLAY_NUM=:0
  56.  
  57. gfromrange(){
  58.     Max_RAND=32762
  59.             MIN=$1
  60.             MAX=$2
  61.             RANGE=$(($MAX-$MIN))
  62.             r=$(bc -l <<<"($RANGE/$Max_RAND)*$RANDOM+$MIN")
  63.             x=$(echo $r|sed 's_^.*[\.]\(.\).*_\1_')
  64.             r=$(echo $r|sed -e 's/[\.].*//')      
  65.             if  [ $x -gt 4 ] ; then  ((r++)) ; fi  
  66.                                                    
  67.             echo $r        
  68.     }
  69.      
  70. getlist(){
  71. echo "image list have been updating..........."
  72.     find "$WS_DIR" \
  73.     -iname "*png" -o \
  74.     -iname "*jpg" -o \
  75.     -iname "*gif" -o \
  76.     -iname "*bmp" -o \
  77.     -iname "*jpeg" -o \
  78.     -iname "*tiff" -o \
  79.     -iname "*tif"  \
  80.     >"$WS_FILE"
  81.     echo "done."
  82.     echo "$(wc -l $WS_FILE|sed 's_\([0-9]*\).*_\1_')" files was added
  83. }
  84.  
  85. selectfile(){
  86.     num=`gfromrange 1 $WS_FILE_LINES`
  87.     echo $(sed -n "$num"p $WS_FILE)
  88. }
  89.  
  90.  
  91. settime(){
  92.     #a tricky passage: list at-jobs related to the script
  93.     #and removing it in the same time by pricessing 'at -l' report
  94.     #directly to removing command
  95.     at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh
  96.     #set new shedule
  97.     at <<EOF -qw now+"$PAUSE"
  98. DISPLAY="$DISPLAY_NUM" "$0" -p "$PAUSE"
  99. EOF
  100. }
  101.  
  102. usage(){
  103. echo "RTFM in the script itself"
  104. }
  105.  
  106. #main()
  107.  
  108. while getopts usdp: OPTION
  109.     do  case "$OPTION" in
  110.         u) getlist; exit 0;;
  111.         p) PAUSE="$OPTARG"
  112.             settime
  113.             echo "interval has set to $PAUSE";;
  114.         s)  at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh; exit 0;;
  115.         d)#now we have became severe enough  
  116.         #to store our variables in system files:
  117.         WSW=$(sed -n 's/WS_WALLPAPER=\(.*\)/\1/p' ~/.Xresources)
  118.             rm "$WSW"
  119.             echo -e " $WSW\n was deleted";
  120.             exit 0;;
  121.  
  122.         ?) usage; exit 1;;
  123.     esac
  124.     done
  125.  
  126. WS_WALLPAPER=$(selectfile)
  127.  
  128. sed -i '/WS_WALLPAPER/d' ~/.Xresources
  129. echo "WS_WALLPAPER=$WS_WALLPAPER" >> ~/.Xresources
  130.  
  131. DISPLAY=$DISPLAY_NUM $($PERFORM "$WS_WALLPAPER")
  132. echo -e " WS_WALLPAPER="$WS_WALLPAPER"\n was set"
Advertisement
Add Comment
Please, Sign In to add comment