Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Csandriel
- #08.03.2018
- #wallshow
- #The script intended to periodicaly change wallpapers. It sets random
- #images from certain directory. It has the ability to change chedule on the fly
- #time interval can been set with '-p' key, in such the notations:
- #wallshow -p 5min
- #wallshow -p 2hours
- #wallshow -p 5days
- #Called without any key, the script just does randomly wallpaper change,
- #not affecting chedule if it is set.
- #Every new invocation of -p key erases the chedule previosly set
- #Key -s stops wallshow, leaving current wallpaper remained, and removing chedule.
- #Key -d deletes the source file of current wallpaper
- #Key -u refreshes the images-list of script
- #The command line that performs setting of wallpaper
- #on the particular system. I use my own scrpipt
- #called "setwallpaper"
- #that looks like this:
- #---------------------------------------
- # ! /bin/bash
- # cp -f "$1" /home/user/wallpaper.img
- # feh --bg-fill /home/user/wallpaper.img
- #---------------------------------------
- #Remember QUOTES here:
- PERFORM="/home/bin/setwallpaper"
- ##### TODO:пока что отказывается корректно работать сброс рут-прав, если запущено от рута.
- ##### Поэтому в таком виде нужно следить чтобы запускался скрипт из под одного пользователя
- ##### Иначе будет существовать столько расписаний смены обоев, сколько пользователей
- ##### запускали скрипт
- #####
- #USER=user
- #if launched as root then restart script as user
- #if [ $UID -eq 0 ] ; then echo "DROP ROOT & recurse"; echo "$0 $@"; su "$USER" -c "$0 $@"; exit; fi
- #the directory where wallpapers are stored:
- WS_DIR=/data0/graph/wallpaper
- #list of graphic files found in WS_DIR:
- WS_FILE=/home/bin/.wallshow.lst
- #Generating/Updating of the list
- #may be done with '-u' key
- #count of lines in wallpapers-list:
- WS_FILE_LINES=$(echo $(wc -l $WS_FILE)|sed 's_\([^ ]*\).*_\1_')
- #id of the display wich wallshow must be performed for:
- DISPLAY_NUM=:0
- #gets a random number from a given range
- gfromrange(){
- Max_RAND=32762
- MIN=$1
- MAX=$2
- RANGE=$(($MAX-$MIN))
- r=$(bc -l <<<"($RANGE/$Max_RAND)*$RANDOM+$MIN")
- x=$(echo $r|sed 's_^.*[\.]\(.\).*_\1_')
- r=$(echo $r|sed -e 's/[\.].*//')
- if [ $x -gt 4 ] ; then ((r++)) ; fi
- echo $r
- }
- #enumerates graphical files in $WS_DIR, stores the list in $WS_FILE
- getlist(){
- echo "image list have been updating..........."
- find "$WS_DIR" \
- -iname "*png" -o \
- -iname "*jpg" -o \
- -iname "*gif" -o \
- -iname "*bmp" -o \
- -iname "*jpeg" -o \
- -iname "*tiff" -o \
- -iname "*tif" \
- >"$WS_FILE"
- echo "done."
- echo "$(wc -l $WS_FILE|sed 's_\([0-9]*\).*_\1_')" files was added
- }
- selectfile(){
- num=`gfromrange 1 $WS_FILE_LINES`
- echo $(sed -n "$num"p $WS_FILE)
- }
- settime(){
- #a tricky passage: list at-jobs related to the script
- #and removing it in the same time by pricessing 'at -l' report
- #directly to removing command
- at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh
- #set new shedule
- at <<EOF -qw now+"$PAUSE"
- DISPLAY="$DISPLAY_NUM" "$0" -p "$PAUSE"
- EOF
- }
- usage(){
- echo "RTFM in the script itself"
- }
- #main()
- while getopts usdp: OPTION
- do case "$OPTION" in
- u) getlist; exit 0;;
- p) PAUSE="$OPTARG"
- settime
- echo "interval has set to $PAUSE";;
- s) at -qw -l|sed 's_\([0-9]*\).*_atrm \1_'|sh; exit 0;;
- d)#now we have became severe enough
- #to store our variables in system files:
- WSW=$(sed -n 's/WS_WALLPAPER:\(.*\)/\1/p' ~/.Xresources)
- rm "$WSW"
- echo -e " $WSW\n was deleted";
- exit 0;;
- ?) usage; exit 1;;
- esac
- done
- WS_WALLPAPER=$(selectfile)
- sed -i '/WS_WALLPAPER/d' ~/.Xresources
- echo "WS_WALLPAPER:$WS_WALLPAPER" >> ~/.Xresources
- DISPLAY=$DISPLAY_NUM $($PERFORM "$WS_WALLPAPER")
- echo -e " WS_WALLPAPER="$WS_WALLPAPER"\n was set"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement