升级到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. 改进uboot,添加自定义快捷菜单

    .在common目录下新增cmd_menu.c文件,内容为: #include<common.h> #include<command.h> #ifdef CONFIG_MENU ...

  2. [AngularJS] Directive with Transcluded Elements

    Create a wrapWith directive using advanced transclusion techniques. transclude - compile the content ...

  3. 【模式识别】Learning To Rank之RankBoost

    RankBoost的思想比較简单,是二元Learning to rank的常规思路:通过构造目标分类器,使得pair之间的对象存在相对大小关系.通俗点说,把对象组成一对对的pair,比方一组排序r1& ...

  4. oc-10-对象做参数

    // // main.m // 6-[掌握]对象和方法之间的关系 #import <Foundation/Foundation.h> //声明人类 @interface Person : ...

  5. java后端模拟表单提交

    代码可实现文本域及非文本域的处理 请求代码: /** * 上传 * * @param urlStr * @param textMap * @param fileMap * @return */ pub ...

  6. printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf - 输出格式转换函数

    参考:http://blog.sina.com.cn/s/blog_674b5aae0100prv3.html 总览 (SYNOPSIS) #include <stdio.h> int p ...

  7. javascript 实现htmlEncode htmlDecode

    屌屌的写法..function htmlEncode(value){ //create a in-memory div, set it's inner text(which jQuery automa ...

  8. 1083. List Grades (25)

    the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1083 and the ...

  9. Linux编程之《守护进程》

    Intro ----- 守护进程,也就是通常说的Daemon进程,是Linux中的后台服务进程.它是一个生存期较长的进程,通常独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程常 ...

  10. JavaScript 正则表达式相关理解

    1.使用正则的test.exec方法而且带有g属性进行连续匹配的时候, 如果每次匹配之前 lastIndex属性没有清零. <script> var str='123qweQWE125|2 ...