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 / ...
随机推荐
- BZOJ 1024: [SCOI2009]生日快乐 dfs
1024: [SCOI2009]生日快乐 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- Spark Core源代码分析: Spark任务运行模型
DAGScheduler 面向stage的调度层,为job生成以stage组成的DAG,提交TaskSet给TaskScheduler运行. 每个Stage内,都是独立的tasks,他们共同运行同一个 ...
- iOS 2D绘图详解(Quartz 2D)之Transform(CTM,Translate,Rotate,Scale)
前言:Quartz默认采用设备无关的user space来进行绘图,当context(画板)建立之后,默认的坐标系原点以及方向也就确认了,可以通过CTM(current transformation ...
- iOS开发——面试总结(一)
面试总结(一) 通过网络搜寻和自己总结经历找了一些IOS面试经常被问道的问题: 1.搞清楚touch事件的传递(事件的响应链) 事件的响应(responder chain) 只有继承了UIRespon ...
- sublime php语法检查
安装sublimelinter 安装sublimelinter-php 设置sublimelinter 进入SublimeLinter文件夹改动 SublimeLinter.sublime-setti ...
- 假设用一个名为text的字符串向量存放文本文件的数据,其中的元素或者是一句话或者是一个用于表示段分隔的空字符串。将text中第一段全改为大写形式
#include<iostream> #include<string> #include<vector> using namespace std; int main ...
- Jetty学习(一)
最近做一个项目,需要动态添加与移除servlet容器的http端口,并且启动都是嵌入式的.因此,果断选择了Jetty. 在模块化方面,Jetty是做的相当给力的一个容器,对 ...
- CentOS中查看系统资源占用情况的命令
用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...
- 如何在linux下解压缩rar和zip格式的文件压缩包
转载:http://oldboy.blog.51cto.com/2561410/597515 使用apt-get安装: sudo apt-get install rar zip rar使用: 将 ...
- 小白日记26:kali渗透测试之提权(六)--收集敏感信息,隐藏痕迹
提权后操作 提权之后,要收集目标系统的重要信息LINUX /etc/resolv.conf #查看DNS配置,是否可以进行DNS篡改或劫持 /etc/passwd #存放账 ...