ubuntu桌面图片自动切换工具DIY
来源:互联网 发布:仿商城html5源码 编辑:程序博客网 时间:2024/05/17 03:29
前言
我还是比较ubuntu的桌面壁纸自动换功能的.之前会网上下载一些壁纸(性感美女了,Tesla跑车了,美景了),然后修改系统文件,让它自动切换我想要的桌面.
随着shell脚本越来越熟悉,我已经不甘心每次手动修改文件去完成这些事了,毕竟太费时间,还不能保证修改的文件完全正确.于是,写了一个脚本工具,实现这个功能.现分享一下.
我也是shell脚本的新手,希望各位吐槽时留情,也请多多指教.
知识背景
ubuntu(我用的是14.04LTS)的壁纸图片是存放在/usr/share/backgrounds/下,桌面壁纸自动切换功能是靠一个配置文件实现的,它就是/usr/share/backgrounds/contest/下的trusty.xml文件.修改该文件,就可以让系统显示自己想要的图片,也可以修改自动切换间隔等.
工具简介
基于上述知识背景,我写的这个脚本工具,可以将指定目录下的壁纸图片复制到/usr/share/backgrounds/目录下新创建的指定目录下,然后自动生成trusty.xml文件,并替换到/usr/share/backgrounds/contest/目录下.这样,脚本执行完,系统就会自动切换指定的图片了.用法及介绍如下:
- 在脚本所在目录下执行./ubuntutrusty.sh (也可以sudo ./ubuntutrusty.sh,如果不用sudo,则在执行过程中需要输入sudo密码,因为需要往系统路径(/usr/share/backgrounds/)下创建目录,复制图片,备份trusty.xml文件,替换文件等).
- 根据提示,输入存放图片的路径,也可以输入quit退出程序;
- 根据提示,输入一个关键字,也可以输入quit退出程序.该关键字用于重命名图片,创建目录,生成trusty.xml文件.例如输入sexygirl,则:
- 指定的图片会被重命名为sexgirl_1.jpg,sexgirl_2.jpg等,当然.jpg后缀是根据原文件后缀生成的,即会保持原文件后缀;
- 在/usr/share/backgrounds/目录下创建sexgirl目录,并把上述重命名后的图片复制到该目录下;
- 生成trusty_sexgirl.xml配置文件,并复制到/usr/share/backgrounds/contest/目录下,然后将原trusty.xml文件备份,并用trusty_sexgirl.xml配置文件复制得到新的trusty.xml文件.
- OK,看一下桌面,已经开始显示指定图片了.
工具详情
# 大家不要直接copy,因为我添加了一些中文注释,添加后没有测试.大家可以从下面的svn地址上下载.#!/bin/bash# this script is a tool to generate a ubuntu trusty.xml for desktop backgroud,with a specified path and a keyword# editted by Fick Song# at 2015.08.12# 提示信息声明inputMsg="input the specified path please or input quit to quit the script:"inputErrorMsg=" is not a directory,input the specified path please or input quit to quit the script:"inputKeywordMsg="input a keyword please or input quit to quit the script:"inputKeywordErrorMsg="keyword can not be null,input a keyword please or input quit to quit the script:"IFS=$(echo -en "\n\b")# 这个方法用来获取关键字,因为关键字可能输入错误,也可以在该过程中直接退出,所以写得有点肿# function for getting specified keywordfunction getKeyword(){keywordConfirm=Nkeyword=""while [ -z "$keyword" ] || [ "$keywordConfirm" = N ] || [ "$keywordConfirm" = n ] dokeyword=""if [ -z "$keyword" ] thenecho $inputKeywordMsgelse echo $inputKeywordErrorMsgfi read keyword if [ "$keyword" = "quit" ] then echo "bye" exit 0 fi if [ -s $path"trusty_"$keyword".xml" ] thenecho $path"trusty_"$keyword".xml already exits,please change a password"continuefiif [ -n "$keyword" ]then echo "the keyword is '$keyword',confirm?(Y/N)" read keywordConfirmfi donereturn 0}# 如果输入的路径未添加/结尾,则该方法会自动于末尾添加# function for appendding a end to pathfunction appendEnd(){if [ -n "$path" ] then endwith=${path:0-1:1} if [ "$endwith" != "/" ] then path=$path"/" fifi}# 输入指定路径# function for inputting pathfunction inputPath(){echo $inputMsgread pathappendEnd# estimate if the path input is directorywhile [ ! -d "$path" ] do# if quit if [ "$path" = "quit" ] || [ "$path" = "quit/" ] then echo "bye" exit 0fi# if again if [ "$path" = "again" ] || [ -z "$path" ] then echo $inputMsgelseecho "'"$path"'"$inputErrorMsgfi read pathappendEnd donereturn 0}# 这个方法用来接收输入的存放图片的路径的# function for getting specified pathfunction getPath(){inputPathconfirmMsg="the path is '$path',confirm?(Y/N)"echo $confirmMsgread confirmwhile [ "$confirm" = "N" ] || [ "$confirm" = "n" ]do path="again" inputPath echo $confirmMsg read confirmdone}# 将文件重命名,根据上面输入的关键字命名# function for renamming filesfunction renameFiles(){filenames=`ls $path`filecount=0for filename in $filenames do if [ ! -d $path$filename ] then filecount=$[filecount+1] suffix=${filename##*.} mv $path$filename $path"tempxxxxxxxxxxxxxxxxxxxxx_"$filecount"."$suffix fi donefilenames=`ls $path`filecount=0for filename in $filenames do if [ ! -d $path$filename ] then filecount=$[filecount+1] suffix=${filename##*.} mv $path$filename $path$keyword"_"$filecount"."$suffix fi doneecho "$filecount files renamed."}# 生成trusty_关键字.xml文件# function for generate trusty.xml filefunction generateFile(){trustyFileName=$path"trusty_"$keyword".xml"# write headerecho -e "<background>\n\t<starttime>\n\t\t<year>2009</year>\n\t\t<month>08</month>\n\t\t<day>04</day>\n\t\t<hour>00</hour>\n\t\t<minute>00</minute>\n\t\t<second>00</second>\n\t</starttime>" > $trustyFileNamefileIndex=0firstName=""preName=""nowName=""filenames=`ls $path`for filename in $filenames dofileIndex=$[fileIndex+1]if [ $fileIndex -eq 1 ] then firstName=$filenamepreName=$filenamecontinue finowName=$filenameecho -e "\t<static>\n\t\t<duration>1795.0</duration>\n\t\t<file>/usr/share/backgrounds/$keyword/$preName</file>\n\t</static>\n\t<transition>\n\t\t<duration>5.0</duration>\n\t\t<from>/usr/share/backgrounds/$keyword/$preName</from>\n\t\t<to>/usr/share/backgrounds/$keyword/$nowName</to>\n\t</transition>" >> $trustyFileNameif [ $fileIndex -eq $filecount ] thenecho -e "\t<static>\n\t<duration>1795.0</duration>\n\t\t<file>/usr/share/backgrounds/$keyword/$nowName</file>\n\t</static>\n\t<transition>\n\t\t<duration>5.0</duration>\n\t\t<from>/usr/share/backgrounds/$keyword/$nowName</from>\n\t\t<to>/usr/share/backgrounds/$keyword/$firstName</to>\n\t</transition>" >> $trustyFileNameecho -e "</background>" >> $trustyFileNamebreakfipreName=$filename done}# 在/usr/share/backgrouds/ 目录下生成新的以关键字为名的目录,并将指定图片复制进去# function for mkdir in /usr/share/backgrouds/ and copy pics into it.function copyPicsUsingSu (){descPath='/usr/share/backgrounds/'$keywordsudo mkdir $descPathsudo mv $trustyFileName /usr/share/backgrounds/contestsudo cp $path*.* $descPath'/'sudo chmod a+r $descPath'/'*sudo mv /usr/share/backgrounds/contest/trusty.xml /usr/share/backgrounds/contest/trusty_bak_`date '+%Y%m%d'`.xmlsudo cp /usr/share/backgrounds/contest/'trusty_'$keyword'.xml' /usr/share/backgrounds/contest/trusty.xml}# 下面次序执行上述各方法,就可以了.getPath# echo $pathgetKeyword# echo $keywordrenameFilesgenerateFilecopyPicsUsingSu
脚本获取
我将脚本放在了公网的服务器上(阿里云服务器,我自己用来搞着玩的)的svn上,大家可以随便下载.
https://121.40.78.227/svn/workspace/sh/tools/ubuntytrusty/ubuntutrusty.sh
邀请扩展
目前市面上也应该存在类似的工具,并且应该也有图形化的工具.而我不喜欢瞎装软件,于是自已动手实现.但目前,该工具还有很多可以优化扩展的地方,例如指定自动切换间隔,实现图形化等.
由于我最近项目较紧,上述优化计划一直搁置,如果有同志愿意一同去改造它,可以联系我,我分配一个svn用户,可以随时更改并提交.
QQ:449270684
微信:fick707
邮箱:fick707@qq.com
0 0
- ubuntu桌面图片自动切换工具DIY
- ubuntu壁纸自动切换
- ubuntu 9.10 桌面壁纸自动切换
- Ubuntu 12.04 自动切换桌面背景
- 解决Ubuntu启动后只显示一桌面图片的问题
- Visual SourceSafe 多项目自动切换工具
- 【原创】Ubuntu快捷方式DIY
- 获取桌面图片
- DIY:magento 自动用户信息管理
- imageswitch的自动切换的一个工具类
- 代码统计工具(DIY)
- 自己动手写Python实现Ubuntu自动切换壁纸
- ubuntu sudo自动切换root 无需输入密码
- ubuntu sudo自动切换root 无需输入密码
- Ubuntu 设定壁纸自动切换的shell脚本
- DIY计算机自动开关机自己做
- DIY的JS自动完成的插件
- DIY一张“时尚”的多媒体工具光盘
- 自定义HTML导航页
- Runnable接口与Thread类的区别
- activity进入出去动画
- BCD详细测试说明
- iOS---Url编解码的问题
- ubuntu桌面图片自动切换工具DIY
- Memcached java 简单实例
- Java多线程[3]:线程同步(互斥)
- Redhat下mysql的安装与使用
- 学习笔记 - InputStreamReader封装System.in,最后用BufferedReader封装
- Myeclipse run as 只有Run Configurations
- Kafka简单测试demo
- POJ 3237 - Tree(树链剖分)
- DOM对象和jQuery对象之间的相互转换