Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.11 KB | None | 0 0
  1. #!/bin/bash
  2. #Csandriel
  3. #08.03.2018
  4.  
  5. #wallshow
  6.  
  7. #The script  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. #gets a random number from  a given range
  58. gfromrange(){
  59.     Max_RAND=32762
  60.             MIN=$1
  61.             MAX=$2
  62.             RANGE=$(($MAX-$MIN))
  63.             r=$(bc -l <<<"($RANGE/$Max_RAND)*$RANDOM+$MIN")
  64.             x=$(echo $r|sed 's_^.*[\.]\(.\).*_\1_')
  65.             r=$(echo $r|sed -e 's/[\.].*//')      
  66.             if  [ $x -gt 4 ] ; then  ((r++)) ; fi  
  67.                                                    
  68.             echo $r        
  69.     }
  70.     #enumerates graphical files in $WS_DIR, stores the list in $WS_FILE
  71. getlist(){
  72. echo "image list have been updating..........."
  73.     find "$WS_DIR" \
  74.     -iname "*png" -o \
  75.     -iname "*jpg" -o \
  76.     -iname "*gif" -o \
  77.     -iname "*bmp" -o \
  78.     -iname "*jpeg" -o \
  79.     -iname "*tiff" -o \
  80.     -iname "*tif"  \
  81.     >"$WS_FILE"
  82.     echo "done."
  83.     echo "$(wc -l $WS_FILE|sed 's_\([0-9]*\).*_\1_')" files was added
  84. }
  85.  
  86. selectfile(){
  87.     num=`gfromrange 1 $WS_FILE_LINES`
  88.     echo $(sed -n "$num"p $WS_FILE)
  89. }
  90.  
  91.  
  92. settime(){
  93.     #a tricky passage: list at-jobs related to the script
  94.     #and removing it in the same time by pricessing 'at -l' report
  95.     #directly to removing command
  96.     at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh
  97.     #set new shedule
  98.     at <<EOF -qw now+"$PAUSE"
  99. DISPLAY="$DISPLAY_NUM" "$0" -p "$PAUSE"
  100. EOF
  101. }
  102.  
  103. usage(){
  104. echo "RTFM in the script itself"
  105. }
  106.  
  107. #main()
  108.  
  109. while getopts usdp: OPTION
  110.     do  case "$OPTION" in
  111.         u) getlist; exit 0;;
  112.         p) PAUSE="$OPTARG"
  113.             settime
  114.             echo "interval has set to $PAUSE";;
  115.         s)  at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh; exit 0;;
  116.         d)#now we have became severe enough  
  117.         #to store our variables in system files:
  118.         WSW=$(sed -n 's/WS_WALLPAPER:\(.*\)/\1/p' ~/.Xresources)
  119.             rm "$WSW"
  120.             echo -e " $WSW\n was deleted";
  121.             exit 0;;
  122.  
  123.         ?) usage; exit 1;;
  124.     esac
  125.     done
  126.  
  127. WS_WALLPAPER=$(selectfile)
  128.  
  129. sed -i '/WS_WALLPAPER/d' ~/.Xresources
  130. echo "WS_WALLPAPER:$WS_WALLPAPER" >> ~/.Xresources
  131.  
  132. DISPLAY=$DISPLAY_NUM $($PERFORM "$WS_WALLPAPER")
  133. echo -e " WS_WALLPAPER="$WS_WALLPAPER"\n was set"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement