您的位置:首页 > 运维架构 > Shell

Ubuntu 设定壁纸自动切换的shell脚本

2020-04-03 07:26 1146 查看

升级到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脚本实现

 
1 #!/bin/bash
2
3 #可用文件后缀名列表
4 readonly prefixs=("jpg" "jpeg" "png" "bmp")
5
6 #动态背景文件地址
7 #/usr/share/backgrounds/contest/trusty.xml
8 readonly animate_background_file_path="/usr/share/backgrounds/contest/trusty.xml"
9
10 #文件列表索引
11 index=0
12
13 #获取图像文件列表
14 get_image_files(){
15
16     #获取文件所在目录名称
17     base_dir="`dirname $1`/`basename $1`/"
18
19     for f in `ls $1`
20     do
21         #检查文件后缀
22         for p in "${prefixs[@]}"
23         do
24             len_before=${#f}
25             f_after=${f%"$p"}
26             len_after=${#f_after}
27
28             #名称发生改变,说明后缀名称符合条件
29             if [ $len_before -ne $len_after ]
30             then
31                 file_list[$index]="$base_dir$f"
32                 echo "获取图像:$base_dir$f"
33                 let index=$index+1
34                 break
35             fi
36         done
37     done
38
39 }
40
41
42 #写入文件
43 replae_file(){
44
45     #创建临时文件
46     animate_back="animate_back.xml"
47     #清空文本内容
48     cat /dev/null > $animate_back
49
50     echo -e  "<background>" >> $animate_back
51     echo -e  "\t<starttime>" >> $animate_back
52     echo -e  "\t\t<year>$(date +%Y)</year>" >> $animate_back
53     echo -e  "\t\t<month>$(date +%m)</month>" >> $animate_back
54     echo -e  "\t\t<day>$(date +%d)</day>" >> $animate_back
55     echo -e  "\t\t<hour>00</hour>" >> $animate_back
56     echo -e  "\t\t<minute>00</minute>" >> $animate_back
57     echo -e  "\t\t<second>00</second>" >> $animate_back
58     echo -e  "\t</starttime>" >> $animate_back
59
60     #写入文件名称
61     index_=0
62     len=${#file_list[@]}
63     for f in "${file_list[@]}"
64     do
65         if [ $index_ -eq $((len-1)) ]
66         then
67             fn=${file_list[0]}
68         else
69             fn=${file_list[$index_+1]}
70         fi
71
72         echo -e  "\t<static>" >> $animate_back
73         echo -e  "\t\t<duration>${STAY:=300}</duration>" >> $animate_back
74         echo -e  "\t\t<file>$f</file>" >> $animate_back
75         echo -e  "\t</static>" >> $animate_back
76         echo -e  "\t<transition>" >> $animate_back
77         echo -e  "\t\t<duration>${DURATION:=3}</duration>" >> $animate_back
78         echo -e  "\t\t<from>$f</from>" >> $animate_back
79         echo -e  "\t\t<to>$fn</to>" >> $animate_back
80         echo -e  "\t</transition>" >> $animate_back
81
82         let index_=$index_+1
83     done
84
85     echo -e  "</background>" >> $animate_back
86
87     #移动文件
88     mv $animate_back $animate_background_file_path
89     if [ $? -eq 0 ]
90     then
91         echo -e  "已经设定好文件"
92     fi
93
94 }
95
96 help(){
97     echo
98     echo "命令格式:`basename $0` [OPTION] -f Filepath"
99     echo "指定图片目录,目录下的图片将作为动态更换的壁纸"
100     echo
101     echo -e "-f[Filepath]\t 图像文件目录"
102     echo -e "-d[Duration]\t 图像切换时长,默认3s"
103     echo -e "-s[StayTime]\t 图像停留时长,默认300s"
104     echo
105     exit 1
106 }
107
108
109 #处理参数
110 while getopts f:s:d: OPTION
111 do
112     case "$OPTION" in
113     f)
114         FILE_PATH="$OPTARG"
115         ;;
116     s)
117         STAY="$OPTARG"
118         ;;
119     d)
120         DURATION="$OPTARG"
121         ;;
122     *)
123         help
124         ;;
125     esac
126 done
127
128 if [ -z "$FILE_PATH" ]
129 then
130     help
131 fi
132
133
134
135 #判断目录是是否存在
136 if [ -d $FILE_PATH ]
137 then
138     #获取到文件列表
139     get_image_files $FILE_PATH
140
141     #获取文件数目
142     file_count=${#file_list[@]}
143
144     if [ $file_count -gt 0 ]
145     then
146         #替换原有动态背景文件
147         echo "共获取到$file_count个图像文件"
148         replae_file
149     else
150         echo "目录$FILE_PATH下不存在符合要求的图像文件:${prefixs[*]}"
151     fi
152
153
154 else
155     echo "不存在目录:$FILE_PATH"
156 fi
157
158
159 exit 0

 

转载于:https://www.cnblogs.com/kyyblabla/p/3983520.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
dbd557957 发布了0 篇原创文章 · 获赞 0 · 访问量 65 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: