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

原理

 

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

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

/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

Ubuntu 设定壁纸自动切换的shell脚本的更多相关文章

  1. top 自动执行的shell脚本中,使用top -n 1 > log.txt, 上电自动执行,文件无输出

    . 自动执行的shell脚本中,使用top -n > log.txt, 上电自动执行,文件无输出,使用一下命令解决: //usr/bin/top -d -n -b > log.txt 如果 ...

  2. 一个在linxu自动切换ip的脚本

    最近的爬虫是在linux下运行的,使用的是云立方的代理服务器,需要自动切换一下ip. #!/bin/bash# coding:utf8 aa="sources.list" #主流程 ...

  3. ubuntu更新删除旧内核的shell脚本

    ubuntu经常提示要更新内核,更新几次后 /boot目录就满了,再更新就提示目录没空间了,这时候就需要删除不用的老旧内核,之前都是uname, grep, dpkg之类的命令一条条敲,然后用眼睛看需 ...

  4. mysql5.7 在Centeros 6 下自动安装的shell脚本

    概述: 此脚本实现了在Centeros 6版本下自动安装mysql5.7到目录 /opt/mysql-5.7*并且做软连接映射到 /usr/local/mysql,自动修改root密码为:123456 ...

  5. window自动切换ip的脚本

    因为总要切换ip,所以百度了一下脚本 如下http://jingyan.baidu.com/article/d2b1d1029d21b95c7e37d4fa.html 动态ip netsh inter ...

  6. linux下监视进程 崩溃挂掉后自动重启的shell脚本

    如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题.在Linux系统中,强大的shell就可以很灵活的处理这样的事务. 下面的shell通过一个while-d ...

  7. 创意编程,Python开发多功能壁纸自动切换工具!

    import ctypes import time import requests import os from threading import Thread from tkinter import ...

  8. mysql自动备份维护shell脚本 (copy)

    #!/bin/bash #Mysql 自动备份 压缩并上传到 指定ftp #设想每天凌晨3点备份mysql #编辑crontab配置文件 # * * * backupmysql.sh #压缩并以&qu ...

  9. 实用脚本----Linux下Jdk和Tomcat自动安装shell脚本总结

    系统环境为:ubuntu 14.04 一.JDK 自动安装脚本 jdk自动安装bash shell脚本,截止今天(2014/10/15)亲测可用: sudo su #切换到root权限 mkdir / ...

随机推荐

  1. 线程池QueueUserWorkItem

    // Test1.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...

  2. 现在的SEO最须要会点啥

    如今的SEO最须要会点啥,会飞天,会遁地,NONONO,不须要你这么流弊,咳咳,不瞎扯.在以往SEO的就是从搜索引擎中获取免费流量.是啊,曾经多好弄啊.而如今在我们不但须要流量还须要把流量进行转换,毕 ...

  3. Jquery-Ajax常用总结

    1.方式一:访问.aspx 客户端: function Del(Id) { if (confirm("确认要删除?")) { $.ajax({ type: "Post&q ...

  4. VBA Excel 单元格操作

    1. 设置单元格边框: .Range("A3:M" & l + 1).SelectWith Selection.Borders() .LineStyle = xlConti ...

  5. SVN是什么,svn的目录结构

    Svn是一个离线的代码管理,可以多个人一起修改,然后再将修改的内容提交到Svn中.每一个svn服务器中的数据存储单位叫做存储,但是你不仅仅可以把整个存储当作你维护的内容,也可以将其中的某个分支目录像根 ...

  6. debian之source.list详解

    之前安装的是debian sarge(内核是2.4.7),不太想更新,但是发现原来的源/ect/apt/source.lists如下,但是用apt-get update,发现大都已经不可用了.怎么办, ...

  7. python之装饰器详解

    这几天翻看python语法,看到装饰器这里着实卡了一阵,最初认为也就是个函数指针的用法,但仔细研究后发现,不止这么简单. 首先很多资料将装饰器定义为AOP的范畴,也就是Aspect Oriented ...

  8. 使用ReactiveCocoa开发RSS阅读器

    目前已经完成的功能有对RSS的解析和Atom解析,RSS内容本地数据库存储和读取,抓取中状态进度展示,标记阅读状态,标记全部已读等.这些功能里我对一些异步操作产生的数据采用了ReactiveCocoa ...

  9. isa-swizzling 是什么鬼?

    刚看到这个名字估计很多人有点熟悉,Method Swizzling对不对,不熟悉也没关系,去看看之前的一篇文章黑魔法之Method Swizzling吧.不过也可以根据名称猜测出来所谓的isa-swi ...

  10. InvocationTargetException

    在使用反射的适合,调用的方法无论抛出什么异常,都会变成InvocationTargetException,要想得到原始的异常,必须使用getTargetException()