Advertisement
Guest User

Untitled

a guest
Jul 7th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.86 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. #supposed dependendency: feh
  23. #Remember QUOTES here:
  24. PERFORM="feh --bg-fill "
  25.  
  26. #####  TODO:пока что отказывается корректно работать сброс рут-прав, если запущено от рута.
  27. #####  Поэтому в таком виде нужно следить чтобы запускался скрипт из под одного пользователя
  28. #####  Иначе будет существовать столько расписаний смены обоев, сколько пользователей
  29. #####  запускали скрипт
  30. #####
  31. #USER=user
  32. #if launched as root then restart script as user
  33. #if [ $UID -eq 0 ] ; then echo "DROP ROOT & recurse"; echo "$0 $@"; su "$USER" -c "$0 $@"; exit; fi
  34.  
  35.  
  36. #the directory where wallpapers are stored:
  37. WS_DIR=/data0/graph/wallpaper
  38.  
  39. #list of graphic files found in WS_DIR:
  40. WS_FILE=/home/bin/.wallshow.lst
  41. #Generating/Updating of the list
  42. #may be done with '-u' key
  43.  
  44. #count of lines in wallpapers-list:
  45. WS_FILE_LINES=$(echo $(wc -l $WS_FILE)|sed 's_\([^ ]*\).*_\1_')
  46.  
  47. #id of the display wich wallshow must be performed for:
  48. DISPLAY_NUM=:0
  49.  
  50. #gets a random number from  a given range
  51. gfromrange(){
  52.     Max_RAND=32762
  53.             MIN=$1
  54.             MAX=$2
  55.             RANGE=$(($MAX-$MIN))
  56.             r=$(bc -l <<<"($RANGE/$Max_RAND)*$RANDOM+$MIN")
  57.             x=$(echo $r|sed 's_^.*[\.]\(.\).*_\1_')
  58.             r=$(echo $r|sed -e 's/[\.].*//')      
  59.             if  [ $x -gt 4 ] ; then  ((r++)) ; fi  
  60.                                                    
  61.             echo $r        
  62.     }
  63.     #enumerates graphical files in $WS_DIR, stores the list in $WS_FILE
  64. getlist(){
  65. echo "image list have been updating..........."
  66.     find "$WS_DIR" \
  67.     -iname "*png" -o \
  68.     -iname "*jpg" -o \
  69.     -iname "*gif" -o \
  70.     -iname "*bmp" -o \
  71.     -iname "*jpeg" -o \
  72.     -iname "*tiff" -o \
  73.     -iname "*tif"  \
  74.     >"$WS_FILE"
  75.     echo "done."
  76.     echo "$(wc -l $WS_FILE|sed 's_\([0-9]*\).*_\1_')" files was added
  77. }
  78.  
  79. selectfile(){
  80.     num=`gfromrange 1 $WS_FILE_LINES`
  81.     echo $(sed -n "$num"p $WS_FILE)
  82. }
  83.  
  84.  
  85. settime(){
  86.     #a tricky passage: list at-jobs related to the script
  87.     #and removing it in the same time by pricessing 'at -l' report
  88.     #directly to removing command
  89.     at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh
  90.     #set new shedule
  91.     at <<EOF -qw now+"$PAUSE"
  92. DISPLAY="$DISPLAY_NUM" "$0" -p "$PAUSE"
  93. EOF
  94. }
  95.  
  96. usage(){
  97. echo "RTFM in the script itself"
  98. }
  99.  
  100. #main()
  101.  
  102. while getopts usdp: OPTION
  103.     do  case "$OPTION" in
  104.         u) getlist; exit 0;;
  105.         p) PAUSE="$OPTARG"
  106.             settime
  107.             echo "interval has set to $PAUSE";;
  108.         s)  at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh; exit 0;;
  109.         d)#now we have became severe enough  
  110.         #to store our variables in system files:
  111.         WSW=$(sed -n 's/WS_WALLPAPER:\(.*\)/\1/p' ~/.Xresources)
  112.             rm "$WSW"
  113.             echo -e " $WSW\n was deleted";
  114.             exit 0;;
  115.  
  116.         ?) usage; exit 1;;
  117.     esac
  118.     done
  119.  
  120. WS_WALLPAPER=$(selectfile)
  121.  
  122. sed -i '/WS_WALLPAPER/d' ~/.Xresources
  123. echo "WS_WALLPAPER:$WS_WALLPAPER" >> ~/.Xresources
  124.  
  125. DISPLAY=$DISPLAY_NUM $($PERFORM "$WS_WALLPAPER")
  126. echo -e " WS_WALLPAPER="$WS_WALLPAPER"\n was set"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement