系统:ubuntu-16.04
右键桌面->更改桌面背景,如下图所示,在右侧缩略图中带有小钟表图标的就表示为动态切换的壁纸:
系统是通过读取这个文件来进行动态壁纸切换的:
/usr/share/backgrounds/contest/xenial.xml
文件主要内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33<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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
2781 #!/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 "tt<year>$(date +%Y)</year>" >> $animate_back 53 echo -e "tt<month>$(date +%m)</month>" >> $animate_back 54 echo -e "tt<day>$(date +%d)</day>" >> $animate_back 55 echo -e "tt<hour>00</hour>" >> $animate_back 56 echo -e "tt<minute>00</minute>" >> $animate_back 57 echo -e "tt<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 "tt<duration>${STAY:=300}</duration>" >> $animate_back 74 echo -e "tt<file>$f</file>" >> $animate_back 75 echo -e "t</static>" >> $animate_back 76 echo -e "t<transition>" >> $animate_back 77 echo -e "tt<duration>${DURATION:=3}</duration>" >> $animate_back 78 echo -e "tt<from>$f</from>" >> $animate_back 79 echo -e "tt<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
在ubuntu窗户里,要到那里才可以产生一个 shell程序/脚本?
在ubuntu桌面左上角 ,应用程序/附件/终端,打开终端后敲
gedit test.sh (自己取个名字,这里我用test)
会弹出一个类似windows里记事本一样的窗口,将代码复制进去,
#!/usr/bin/sh
echo "Hello "`whoami`
。。。。。。
echo 'no this num!'
fi
done
然后直接关掉,会提示你保存。
然后继续在终端下
bash test.sh 回车,执行后,不要关闭终端。按键盘上的print screen截屏按键,会将当前屏幕截成一个图片,保存位置就放在桌面上好了,注意多次截图文件名不要覆盖了。
然后将图片或通过u盘,或者直接复制到windows系统所在分区,转移走。
然后进入windows系统,打开那些图片,粘贴到word里,或者用windows的画图工具放大用矩形再截一下。
(基本操作应该就是那样了,只是一点没体现出linux的优越性。ubuntu10.04版是吧,不存在10.03版。楼主如果像学linux可以从bash脚本,vim编辑器开始学起。)
ubuntu 下执行shell脚本的问题
诶!你还要去好好去玩一下Linux(不要用图形系统),你问的这些问题,真不好解释
1,chmod +x test.sh:将test.sh变成可执行权限。
2,test.sh 第一行有"#!/bin/sh” 告诉解释器在什么位置。
3,第一步test.sh变成可执行了,./test.sh(运行当前目录下一个可执行文件,这是一个shell脚本,需要解释器,如果有"#!/bin/sh”通过sh解释,如果没有会报错没这个命令)。
4,./test.sh(第三步我以解释什么意思);运行test.sh(将同过path路径去找这个命令,显然这个tesh.sh这个文件不在你path路径下,你怎么能运行呢)。
5,sh test.sh(sh在/bin目录下也就是已经假如path路径,用sh命令解释你这个脚本)
转自:http://www.bkjia.com/Linuxjc/881784.html
最后
以上就是健壮抽屉最近收集整理的关于Ubuntu 设定壁纸自动切换的shell脚本的全部内容,更多相关Ubuntu内容请搜索靠谱客的其他文章。
发表评论 取消回复