一步添加动态壁纸
以前学习shell脚本语言时写的一个小脚本,可以一步添加一个文件夹里的特定格式的图片为系统的动态壁纸,也可以根据指定的标签删除创建的动态壁纸适合gnome环境
命令帮助如下:
xml.sh [-dsth] [time] [Directory] [标签]
-d Dir 包含有壁纸的目录
-s time 壁纸静态时间(s),默认1790.0 s
-t time 壁纸切换时间(s),默认2.0 s
-r [itm1 itm2 ...] 根据指定的动态壁纸的标签,删除动态壁纸的信息,默认标签为目录的名字
-h 打印帮助
例如有一文件夹/home/zhang/picture下有若干图片,想把它们添加为动态壁纸,标签设为gnome,可如下执行:
xml.sh -d /home/zhang/picture gnome
指定壁纸静态保持的时间和壁纸间切换的时间
xml.sh -s 100 -t 5 -d /home/zhang/picture gnome
若执行 xml.sh,默认搜索当前目录下的图片并以目录名(非绝对路径)为标签建立动态壁纸信息
删除动态壁纸信息,这需要知道壁纸标签名,可同时指定多个标签
xml.sh -r gnome ...
此脚本只是练习,如有错误,恳请批评指教
程序代码:
#!/bin/bash #Progam : # 1、将一个文件夹下的特定格式的图片组成一组动态壁纸,然后应用到系统 # 2、根据指定的文件夹从系统删除对应的动态壁纸信息(并不删除图片) #Data :2013-09-08 #Author :Juson #Release :3rd Release #Operating System :Fedora/Ubuntu Gnome环境 (其余未测试) directory=`pwd` filename="" deldir="" osdir="/usr/share/gnome-background-properties" STATICTIME=1790.0 #壁纸静态时间(s) TRANSTIME=2.0 #壁纸间切换时间(s) ####处理参数#### while [ -n "$1" ] do if [ "$1" == "-h" ]; then echo "Usage: $0 [-dsth] [time] [Directory] [标签]" echo " -d Dir 包含有壁纸的目录" echo " -s time 壁纸静态时间(s),默认$STATICTIME s" echo " -t time 壁纸切换时间(s),默认$TRANSTIME s" echo " -r [itm1 itm2 ...] 根据指定的动态壁纸的标签,删除动态壁纸的信息,默认标签为目录的名字" echo " -h 打印帮助" exit 1 elif [ "$1" == "-s" ]; then if [[ "$2" =~ ^[0-9]+\.?[0-9]*$ ]]; then STATICTIME=$2 shift else echo "Error :壁纸静态时间必须是有效数字" exit 2 fi elif [ "$1" == "-t" ]; then if [[ "$2" =~ ^[0-9]+\.?[0-9]*$ ]]; then TRANSTIME=$2 shift else echo "Error :壁纸切换时间必须是有效数字" exit 3 fi elif [ "$1" == "-d" ]; then directory=$2 shift elif [ "$1" == "-r" ]; then shift deldir=$@ if [ -z "$deldir" ]; then echo "Error :未指定要删除的标签" exit else break fi else filename=$1.xml fi shift done ####删除动态壁纸信息#### if [ -n "$deldir" ]; then for dir in $deldir do if [[ "$dir" =~ \/ ]]; then echo "$dir 不是有效标签" else echo "正在删除动态壁纸$dir的信息..." sudo rm -rf $osdir/*$dir-wallpapers.xml if [ "$?" == "0" ]; then echo "已经成功删除动态壁纸$dir的信息" else echo "删除失败!!!" fi fi done exit fi ####建立动态壁纸信息#### directory=${directory%/} if [ ! -d "$directory" ]; then echo "Error :$directory 不是目录." exit 4 fi if [ -z "$filename" ]; then filename="${RANDOM}_${directory##*/}.xml" fi rm $filename 2> /dev/null touch $filename if [ "$?" != "0" ]; then echo "Error :无法创建文件$filename" exit 5 fi #下面仅仅搜索jpg、jpeg、png三种常用格式的图片,如有其他格式的图片可以按照下面的形式自己添加 firstfile=`ls -l $directory | egrep -i *.'(jpg|jpeg|png)$' | awk 'NR==1 { print $9 }'` notfirst=`ls -l $directory | egrep -i *.'(jpg|jpeg|png)$' | awk 'NR!=1 { print $9 }'` ##判断是否有图片文件 if [ -z "$firstfile" ]; then echo "Error :Don't find any pictures in $directory" rm $filename 2> /dev/null exit 4 fi wallpaperfilename="${filename%.*}-wallpapers.xml" cat > $filename << EOF <background> <starttime> <year>2013</year> <month>03</month> <day>29</day> <hour>00</hour> <minute>00</minute> <second>00</second> </starttime> <!-- This animation will start at anytime. --> <static> <duration>$STATICTIME</duration> <file>$directory/$firstfile</file> </static> <transition> <duration>$TRANSTIME</duration> <from>$directory/$firstfile</from> EOF cat > $wallpaperfilename << EOF <?xml version="1.0" encoding="UTF-8"?> <!-- DOCTYPE wallpapers SYSTEM "gnome-wp-list.dtd" --> <wallpapers> <wallpaper deleted="false"> <name>${filename%.*}</name> <filename>$directory/$filename</filename> <options>zoom</options> </wallpaper> EOF for i in $notfirst do cat >> $filename << EOF <to>$directory/$i</to> </transition> <static> <duration>$STATICTIME</duration> <file>$directory/$i</file> </static> <transition> <duration>$TRANSTIME</duration> <from>$directory/$i</from> EOF cat >> $wallpaperfilename << EOF <wallpaper> <name>${i%.*}</name> <filename>$directory/$i</filename> <options>zoom</options> <pcolor>#000000</pcolor> <scolor>#000000</scolor> <shade_type>solid</shade_type> </wallpaper> EOF done cat >> $filename << EOF <to>$directory/$firstfile</to> </transition> </background> EOF cat >> $wallpaperfilename << EOF </wallpapers> EOF ####将壁纸信息应用到系统#### echo 正在使壁纸信息生效... mv $filename $directory sudo mv $wallpaperfilename $osdir if [ "$?" == "0" ]; then echo "操作成功。" else echo "未添加壁纸信息,操作失败。" fi exit 0