Ubuntu 设定壁纸自动切换的shell脚本

升级到Ubuntu14.04后,感觉bug的确比12.04少多了。顶部任务栏支持半透明效果,所以整个桌面也看上去漂亮了很多。这样的桌面也是值得瞎捣鼓一下的,想到换壁纸,但是没找到设定动态更换壁纸的选项,但手动修改配置文件的方法总是有的,本文的目的也在于此。(以下过程在Ubuntu14上进行,未测试其他版本!)。

原理

 

右键桌面->更改桌面背景,如下图所示,在右侧缩略图中带有小钟表图标的就表示为动态切换的壁纸:

Ubuntu 设定壁纸自动切换的shell脚本

系统是通过读取这个文件来进行动态壁纸切换的:

/usr/share/backgrounds/contest/trusty.xml

文件主要内容如下:

<background>
<starttime>
<year>2014</year>
<month>09</month>
<day>21</day>
<hour>00</hour>
<minute>00</minute>
<second>00</second>
</starttime>
<static>
<duration>300</duration>
<file>/home/kyy/Wallpaper/1019236,106.jpg</file>
</static>
<transition>
<duration>3</duration>
<from>/home/kyy/Wallpaper/1019236,106.jpg</from>
<to>/home/kyy/Wallpaper/1019306,106.jpg</to>
</transition>
<static>
<duration>300</duration>
<file>/home/kyy/Wallpaper/1019306,106.jpg</file>
</static>
<transition>
<duration>3</duration>
<from>/home/kyy/Wallpaper/1019306,106.jpg</from>
<to>/home/kyy/Wallpaper/1082502,106.jpg</to>
</transition>
<static>......
</static>
<transition>......
</transition>
......
</background>

其中static标签内file表示当前图像,duration表示当前图像显示的持续时间

transition标签内from和to分别表示不下一步在那两个图片之间切换,duration表示过渡时间

so,系统就是根据这个来进行桌面壁纸动态切换的。不过没切换一次图像就需要写大量代码,我们肯定不会脑残到自己手动去写的,那么的,既然实在Linux下,用shell脚本代替人工自然是最合适不过了

shell脚本实现

 
 #!/bin/bash

 #可用文件后缀名列表
readonly prefixs=("jpg" "jpeg" "png" "bmp") #动态背景文件地址
#/usr/share/backgrounds/contest/trusty.xml
readonly animate_background_file_path="/usr/share/backgrounds/contest/trusty.xml" #文件列表索引
index= #获取图像文件列表
get_image_files(){ #获取文件所在目录名称
base_dir="`dirname $1`/`basename $1`/" for f in `ls $`
do
#检查文件后缀
for p in "${prefixs[@]}"
do
len_before=${#f}
f_after=${f%"$p"}
len_after=${#f_after} #名称发生改变,说明后缀名称符合条件
if [ $len_before -ne $len_after ]
then
file_list[$index]="$base_dir$f"
echo "获取图像:$base_dir$f"
let index=$index+
break
fi
done
done } #写入文件
replae_file(){ #创建临时文件
animate_back="animate_back.xml"
#清空文本内容
cat /dev/null > $animate_back echo -e "<background>" >> $animate_back
echo -e "\t<starttime>" >> $animate_back
echo -e "\t\t<year>$(date +%Y)</year>" >> $animate_back
echo -e "\t\t<month>$(date +%m)</month>" >> $animate_back
echo -e "\t\t<day>$(date +%d)</day>" >> $animate_back
echo -e "\t\t<hour>00</hour>" >> $animate_back
echo -e "\t\t<minute>00</minute>" >> $animate_back
echo -e "\t\t<second>00</second>" >> $animate_back
echo -e "\t</starttime>" >> $animate_back #写入文件名称
index_=
len=${#file_list[@]}
for f in "${file_list[@]}"
do
if [ $index_ -eq $((len-)) ]
then
fn=${file_list[]}
else
fn=${file_list[$index_+]}
fi echo -e "\t<static>" >> $animate_back
echo -e "\t\t<duration>${STAY:=300}</duration>" >> $animate_back
echo -e "\t\t<file>$f</file>" >> $animate_back
echo -e "\t</static>" >> $animate_back
echo -e "\t<transition>" >> $animate_back
echo -e "\t\t<duration>${DURATION:=3}</duration>" >> $animate_back
echo -e "\t\t<from>$f</from>" >> $animate_back
echo -e "\t\t<to>$fn</to>" >> $animate_back
echo -e "\t</transition>" >> $animate_back let index_=$index_+
done echo -e "</background>" >> $animate_back #移动文件
mv $animate_back $animate_background_file_path
if [ $? -eq ]
then
echo -e "已经设定好文件"
fi } help(){
echo
echo "命令格式:`basename $0` [OPTION] -f Filepath"
echo "指定图片目录,目录下的图片将作为动态更换的壁纸"
echo
echo -e "-f[Filepath]\t 图像文件目录"
echo -e "-d[Duration]\t 图像切换时长,默认3s"
echo -e "-s[StayTime]\t 图像停留时长,默认300s"
echo
exit
} #处理参数
while getopts f:s:d: OPTION
do
case "$OPTION" in
f)
FILE_PATH="$OPTARG"
;;
s)
STAY="$OPTARG"
;;
d)
DURATION="$OPTARG"
;;
*)
help
;;
esac
done if [ -z "$FILE_PATH" ]
then
help
fi #判断目录是是否存在
if [ -d $FILE_PATH ]
then
#获取到文件列表
get_image_files $FILE_PATH #获取文件数目
file_count=${#file_list[@]} if [ $file_count -gt ]
then
#替换原有动态背景文件
echo "共获取到$file_count个图像文件"
replae_file
else
echo "目录$FILE_PATH下不存在符合要求的图像文件:${prefixs[*]}"
fi else
echo "不存在目录:$FILE_PATH"
fi exit
上一篇:Linux lamp环境编译安装


下一篇:Redis 持久化和配置文件