Ubuntu 设定壁纸自动切换的shell脚本
升级到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脚本的更多相关文章
- top 自动执行的shell脚本中,使用top -n 1 > log.txt, 上电自动执行,文件无输出
. 自动执行的shell脚本中,使用top -n > log.txt, 上电自动执行,文件无输出,使用一下命令解决: //usr/bin/top -d -n -b > log.txt 如果 ...
- 一个在linxu自动切换ip的脚本
最近的爬虫是在linux下运行的,使用的是云立方的代理服务器,需要自动切换一下ip. #!/bin/bash# coding:utf8 aa="sources.list" #主流程 ...
- ubuntu更新删除旧内核的shell脚本
ubuntu经常提示要更新内核,更新几次后 /boot目录就满了,再更新就提示目录没空间了,这时候就需要删除不用的老旧内核,之前都是uname, grep, dpkg之类的命令一条条敲,然后用眼睛看需 ...
- mysql5.7 在Centeros 6 下自动安装的shell脚本
概述: 此脚本实现了在Centeros 6版本下自动安装mysql5.7到目录 /opt/mysql-5.7*并且做软连接映射到 /usr/local/mysql,自动修改root密码为:123456 ...
- window自动切换ip的脚本
因为总要切换ip,所以百度了一下脚本 如下http://jingyan.baidu.com/article/d2b1d1029d21b95c7e37d4fa.html 动态ip netsh inter ...
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
如何保证服务一直运行?如何保证即使服务挂掉了也能自动重启?在写服务程序时经常会碰到这样的问题.在Linux系统中,强大的shell就可以很灵活的处理这样的事务. 下面的shell通过一个while-d ...
- 创意编程,Python开发多功能壁纸自动切换工具!
import ctypes import time import requests import os from threading import Thread from tkinter import ...
- mysql自动备份维护shell脚本 (copy)
#!/bin/bash #Mysql 自动备份 压缩并上传到 指定ftp #设想每天凌晨3点备份mysql #编辑crontab配置文件 # * * * backupmysql.sh #压缩并以&qu ...
- 实用脚本----Linux下Jdk和Tomcat自动安装shell脚本总结
系统环境为:ubuntu 14.04 一.JDK 自动安装脚本 jdk自动安装bash shell脚本,截止今天(2014/10/15)亲测可用: sudo su #切换到root权限 mkdir / ...
随机推荐
- [AngularJS] Using $anchorScroll
If you're in a scenario where you want to disable the auto scrolling, but you want to control the sc ...
- JUnit中测试异常抛出的方法
最近在做TWU关于TDD的作业,对JUnit中测试异常抛出的方法进行了一些学习和思考. 在进行单元测试的时候有的时候需要测试某一方法是否抛出了正确的异常.例如,我有一个方法,里面对一个List进行读取 ...
- Blueprint编译过程
Blueprint 编译概述 一.术语 Blueprint,像C++语言一下的,在游戏中使用前须要编译.当你在BP编辑器中,点击编译button时候.BP资源開始把属性和图例过程转换为一个类对象处理. ...
- SVN是什么,svn的目录结构
Svn是一个离线的代码管理,可以多个人一起修改,然后再将修改的内容提交到Svn中.每一个svn服务器中的数据存储单位叫做存储,但是你不仅仅可以把整个存储当作你维护的内容,也可以将其中的某个分支目录像根 ...
- 1083. List Grades (25)
the problem is from PAT,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1083 and the ...
- Helpers\Pagination
Helpers\Pagination Break recordset into a series of pages. First create a new instance of the class ...
- 完全自定义 TabBar
// // CustomTabBarController.h // Dream // // Created by mac on 14-10-17. // Copyright (c) 2014年 HM. ...
- iOS之Storyboard References
如果你曾经使用 interface builder 创建过一个复杂.界面非常多的应用,你就会明白最后那些Storyboards 文件变的有多大.他会迅速变的无法管理,阻碍你的进度.自从引入 Story ...
- android开发之路08(ListView&Adapter)
ListView控件介绍:用于将数据库中的数据或者网络中的数据通过列表的形式显示出来:ListView采用MVC模式将前端显示和后端数据进行分离. 也就是说,ListView控件在装载数据时并不是直接 ...
- LeetCode 268
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...