ubuntu桌面图片自动切换工具DIY

前言

我还是比较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/目录下.这样,脚本执行完,系统就会自动切换指定的图片了.用法及介绍如下:
  1. 在脚本所在目录下执行./ubuntutrusty.sh     (也可以sudo ./ubuntutrusty.sh,如果不用sudo,则在执行过程中需要输入sudo密码,因为需要往系统路径(/usr/share/backgrounds/)下创建目录,复制图片,备份trusty.xml文件,替换文件等).
  2. 根据提示,输入存放图片的路径,也可以输入quit退出程序;
  3. 根据提示,输入一个关键字,也可以输入quit退出程序.该关键字用于重命名图片,创建目录,生成trusty.xml文件.例如输入sexygirl,则:
    1. 指定的图片会被重命名为sexgirl_1.jpg,sexgirl_2.jpg等,当然.jpg后缀是根据原文件后缀生成的,即会保持原文件后缀;
    2. /usr/share/backgrounds/目录下创建sexgirl目录,并把上述重命名后的图片复制到该目录下;
    3. 生成trusty_sexgirl.xml配置文件,并复制到/usr/share/backgrounds/contest/目录下,然后将原trusty.xml文件备份,并用trusty_sexgirl.xml配置文件复制得到新的trusty.xml文件.
  4. 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 keyword
function getKeyword()
{
keywordConfirm=N
keyword=""
while [ -z "$keyword" ] || [ "$keywordConfirm" = N ] || [ "$keywordConfirm" = n ]
    do
	keyword=""
	if [ -z "$keyword" ]
	    then
		echo $inputKeywordMsg
	else
	    echo $inputKeywordErrorMsg
	fi
        read keyword
        if [ "$keyword" = "quit" ]
            then
                echo "bye"
                exit 0
        fi
        if [ -s $path"trusty_"$keyword".xml" ]
	    then
		echo $path"trusty_"$keyword".xml already exits,please change a password"
		continue
	fi
	if [ -n "$keyword" ]
	then
            echo "the keyword is '$keyword',confirm?(Y/N)"   
	    read keywordConfirm
	fi
    done
return 0
}

# 如果输入的路径未添加/结尾,则该方法会自动于末尾添加
# function for appendding a end to path
function appendEnd()
{
if [ -n "$path" ]
    then
        endwith=${path:0-1:1}
        if [ "$endwith" != "/" ]
            then
                 path=$path"/"
        fi
fi
}

# 输入指定路径
# function for inputting path
function inputPath()
{
echo $inputMsg
read path
appendEnd
# estimate if the path input is directory
while [ ! -d "$path" ]
    do
# if quit
        if [ "$path" = "quit" ] || [ "$path" = "quit/" ]
            then
                echo "bye"
                exit 0
	fi
# if again
        if [ "$path" = "again" ] || [ -z "$path" ]
            then
                echo $inputMsg
	else
		echo "'"$path"'"$inputErrorMsg
	fi
        read path
	appendEnd
    done
return 0
}

# 这个方法用来接收输入的存放图片的路径的
# function for getting specified path
function getPath()
{
inputPath
confirmMsg="the path is '$path',confirm?(Y/N)"
echo $confirmMsg
read confirm
while [ "$confirm" = "N" ] || [ "$confirm" = "n" ]
do
    path="again"
    inputPath
    echo $confirmMsg
    read confirm
done
}

# 将文件重命名,根据上面输入的关键字命名
# function for renamming files
function renameFiles()
{
filenames=`ls $path`
filecount=0
for filename in $filenames
    do
        if [ ! -d $path$filename ]
            then
                filecount=$[filecount+1]
                suffix=${filename##*.}
                mv $path$filename $path"tempxxxxxxxxxxxxxxxxxxxxx_"$filecount"."$suffix
        fi
    done
filenames=`ls $path`
filecount=0
for filename in $filenames
    do
        if [ ! -d $path$filename ]
            then
                filecount=$[filecount+1]
                suffix=${filename##*.}
                mv $path$filename $path$keyword"_"$filecount"."$suffix
        fi
    done
echo "$filecount files renamed."
}

# 生成trusty_关键字.xml文件
# function for generate trusty.xml file
function generateFile(){
trustyFileName=$path"trusty_"$keyword".xml"
# write header
echo -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>" > $trustyFileName


fileIndex=0
firstName=""
preName=""
nowName=""
filenames=`ls $path`
for filename in $filenames
    do
	fileIndex=$[fileIndex+1]
	if [ $fileIndex -eq 1 ]
             then
                firstName=$filename
		preName=$filename
		continue
        fi
	nowName=$filename
	echo -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>" >> $trustyFileName
	if [ $fileIndex -eq $filecount ]
	    then
		echo -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>" >> $trustyFileName
		echo -e "</background>" >> $trustyFileName
		break
	fi
	preName=$filename
    done
}

# 在/usr/share/backgrouds/ 目录下生成新的以关键字为名的目录,并将指定图片复制进去
# function for mkdir in /usr/share/backgrouds/ and copy pics into it.
function copyPicsUsingSu (){
descPath='/usr/share/backgrounds/'$keyword
sudo mkdir $descPath
sudo mv $trustyFileName /usr/share/backgrounds/contest
sudo 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'`.xml
sudo cp /usr/share/backgrounds/contest/'trusty_'$keyword'.xml' /usr/share/backgrounds/contest/trusty.xml
}

# 下面次序执行上述各方法,就可以了.
getPath
# echo $path
getKeyword
# echo $keyword
renameFiles
generateFile
copyPicsUsingSu

脚本获取

我将脚本放在了公网的服务器上(阿里云服务器,我自己用来搞着玩的)的svn上,大家可以随便下载.
https://121.40.78.227/svn/workspace/sh/tools/ubuntytrusty/ubuntutrusty.sh

邀请扩展

目前市面上也应该存在类似的工具,并且应该也有图形化的工具.而我不喜欢瞎装软件,于是自已动手实现.但目前,该工具还有很多可以优化扩展的地方,例如指定自动切换间隔,实现图形化等.
由于我最近项目较紧,上述优化计划一直搁置,如果有同志愿意一同去改造它,可以联系我,我分配一个svn用户,可以随时更改并提交.
QQ:449270684
微信:fick707
邮箱:fick707@qq.com
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值