zoukankan      html  css  js  c++  java
  • ubuntu自动更换桌面背景

      Environment: Ubuntu OS

      作为一名同时喜欢windows 8.1和 ubuntu的用户,转入ubuntu系统后自然会觉得ubuntu的界面太过简陋,所有就在网上找到了一些自动更换壁纸的脚本命令来美化桌面,在此记录与分享。

    以下是所用代码:

      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  "	<starttime>" >> $animate_back
     52     echo -e  "		<year>$(date +%Y)</year>" >> $animate_back
     53     echo -e  "		<month>$(date +%m)</month>" >> $animate_back
     54     echo -e  "		<day>$(date +%d)</day>" >> $animate_back
     55     echo -e  "		<hour>00</hour>" >> $animate_back
     56     echo -e  "		<minute>00</minute>" >> $animate_back
     57     echo -e  "		<second>00</second>" >> $animate_back
     58     echo -e  "	</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  "	<static>" >> $animate_back
     73         echo -e  "		<duration>${STAY:=300}</duration>" >> $animate_back
     74         echo -e  "		<file>$f</file>" >> $animate_back
     75         echo -e  "	</static>" >> $animate_back        
     76         echo -e  "	<transition>" >> $animate_back
     77         echo -e  "		<duration>${DURATION:=3}</duration>" >> $animate_back
     78         echo -e  "		<from>$f</from>" >> $animate_back
     79         echo -e  "		<to>$fn</to>" >> $animate_back
     80         echo -e  "	</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]	 图像文件目录"
    102     echo -e "-d[Duration]	 图像切换时长,默认3s"
    103     echo -e "-s[StayTime]	 图像停留时长,默认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
    View Code

    新建一个shell脚本文件,假设命为setbackground.sh

    touch setbackground.sh

    或gedit setbackground.sh 粘贴后保存

    使用命令 

    1 sudo bash setbackground.sh -d 3 -s 300 -f yourfilepath

    这里Option 有三个选项,其中

    f(filepath) 为你提供的图像文件目录  

    d (duration)为图像切换时长,默认为3s,太短可能无幻灯片效果,

    s(staytime)图像停留时长,默认为300s,即5分钟。

    注意修改对应的访问文件权限(如果有必要),我第一次使用时忽略这个问题,尝试了很久才发现了原因。

    成功后的截图

    代码来源:http://www.cnblogs.com/iwtwiioi/p/3857510.html

    http://blog.csdn.net/leomon_1993/article/details/30291283

    分享请注明来源:http://www.cnblogs.com/7explore-share 请尊重别人的创作,谢谢!
  • 相关阅读:
    C#随笔
    C# 程序间通信的各种途径及解析
    C#控件根据窗体改变大小
    c# SqlHelper Class
    初始Java
    函数基础
    基本数据类型的高级特性:
    python基础
    C#double转化成字符串 保留小数位数, 不以科学计数法的形式出现。
    Oracle使用dblink导入数据
  • 原文地址:https://www.cnblogs.com/7explore-share/p/4197406.html
Copyright © 2011-2022 走看看