闲话不多说。上代码。
- #!/bin/bash
- # randombg.sh
- # random background .
-
- cache=$HOME/.random_bg_cache/
- bgcommand="gconftool --type string -s /desktop/gnome/background/picture_filename"
-
- # check whether the cache directory is exist.
- if [[ -e "$cache" ]]
- then
- :
- else
- echo "The cache directory is not exist."
- if mkdir -p "$cache" >/dev/null 2>&1
- then
- echo "Creat the directory."
- else
- echo "Can't creat the directory."
- exit 3
- fi
- fi
-
- # get the picture name
- Get_wallimag(){
- local situ
- situ=$(ls -l $cache*.jpg 2>/dev/null| wc -l)
- if [ $situ -ne 0 ]
- then
- array=(`ls $cache*.jpg`)
- local sum=`ls -l $cache*.jpg 2>/dev/null | wc -l`
- local flag=$(($RANDOM % $sum))
- wallpg="${array[$flag]}"
- else
- echo "The cache directory is empty."
- echo "Please check it out."
- exit 3
- fi
- }
-
- Set_wallimag(){
- Get_wallimag
- if [ "$wallpg" = "" ]
- then
- echo "Get picture faild."
- exit 3
- fi
- $bgcommand $wallpg >/dev/null 2>&1
- if [ $? ]
- then
- :
- else
- echo "This job is faild."
- exit 2
- fi
- }
-
- # read arguments
- Read_arg(){
- while [[ $# -gt 0 ]]
- do
- parm="$1"
- shift
-
- case "$parm" in
- -h | --help)
- echo "-t time, --time time:
- Set the delay time(minute). e.g 3
- The time default is 3 minutes.
- -l , --list:
- List exist process.
- -r , --remove:
- Remove the current job.
- -v , --version:
- Show the current version.
-
- Notice:
- You must move some picture to $cache.
- "
- exit 0
- ;;
- -v | --version)
- echo "The current version:
- random 0.2"
- exit 0
- ;;
- -t | --time)
- TIME_CACHE="$1"
- if [ -z $TIME_CACHE ]
- then
- echo "Need a argument.
- randombg -t TIME"
- exit 0
- elif echo "$TIME_CACHE" | grep -q '[a-zA-Z]'
- then
- echo "Need a number.
- randombg -t NUMBER"
- exit 0
- fi
- TIME=$((TIME_CACHE*60))
- shift
- ;;
- -l | --list)
- local num
- num=$(ps -ef | grep randombg | grep -v grep|wc -l)
- if [ "$num" -gt 2 ]
- then
- echo "There is a process exist."
- ps -ef | grep "randombg" | grep -v 'grep' |head -n1| awk '{print $5" "$9" "$10" "$11}'
- exit 0
- else
- echo "There is no process exist."
- exit 1
- fi
- ;;
- -r | --remove)
- AIM=`ps -ef|grep randombg|grep -v grep| head -n1|awk '{print $2}'`
- kill $AIM
- exit 0
- ;;
- *)
- echo "Wrong input.
- randombg -h"
- exit 0
- ;;
- esac
- done
- }
-
- # loop forever
- Random_bg(){
- : ${TIME:=300}
- while :
- do
- Set_wallimag
- sleep $TIME
- done
- }
-
- # judge is there a randombg problem.
- Judge(){
- if [ `ps -ef | grep randombg | grep -v grep | wc -l` -gt 2 ]
- then
- ./randombg -l
- echo -n "There is randombg problem exist.Reset it now?(y/n)y "
- read ANSWER
- AIM=`ps -ef|grep randombg|grep -v grep| head -n1|awk '{print $2}'`
- : ${ANSWER:=y}
- case "$ANSWER" in
- y|Y)
- kill $AIM>/dev/null 2>&1
- Random_bg &
- exit 0
- ;;
- n|N)
- exit 0
- ;;
- *)
- exit 0
- ;;
- esac
- else
- Random_bg &
- exit 0
- fi
- }
-
- # main function
- Read_arg "$@"
- Judge
- exit 0