1. 程式人生 > >Ubuntu 設定桌布自動切換的shell指令碼

Ubuntu 設定桌布自動切換的shell指令碼

系統: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     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