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

来源:互联网 发布:淘宝首页轮播图多大 编辑:程序博客网 时间:2024/05/29 11:15

系统:ubuntu-16.04

右键桌面->更改桌面背景,如下图所示,在右侧缩略图中带有小钟表图标的就表示为动态切换的壁纸:

系统是通过读取这个文件来进行动态壁纸切换的:

/usr/share/backgrounds/contest/xenial.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     echo101     echo -e "-f[Filepath]\t 图像文件目录"102     echo -e "-d[Duration]\t 图像切换时长,默认3s"103     echo -e "-s[StayTime]\t 图像停留时长,默认300s"104     echo105     exit 1106 }107 108 109 #处理参数110 while getopts f:s:d: OPTION111 do112     case "$OPTION" in113     f)114         FILE_PATH="$OPTARG"115         ;;116     s)117         STAY="$OPTARG"118         ;;119     d)120         DURATION="$OPTARG"121         ;;122     *)123         help124         ;;125     esac126 done127 128 if [ -z "$FILE_PATH" ]129 then  130     help    131 fi132 133 134  135 #判断目录是是否存在136 if [ -d $FILE_PATH ]137 then     138     #获取到文件列表139     get_image_files $FILE_PATH140     141     #获取文件数目142     file_count=${#file_list[@]}143     144     if [ $file_count -gt 0 ]        145     then146         #替换原有动态背景文件147         echo "共获取到$file_count个图像文件"148         replae_file     149     else150         echo "目录$FILE_PATH下不存在符合要求的图像文件:${prefixs[*]}"151     fi152     153 154 else155     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

0 0
原创粉丝点击