让美女时钟做你的桌面

前两天写了一篇文章:Linux命令行下更改桌面背景(GNOME环境),这几天刚好看到了搜道网站的美女时钟,上面的美女图片是每分钟换一次,想了想要是能把上面的报时的美女图片当做桌面,为你报时,岂不是很好玩?

于是研究了下那个网站,写了个Shell脚本,实现了上面的想法,脚本如下:

[bash]

#!/bin/bash
#Author: LinuxSong
#Blog: https://www.topbyte.cn

gconftool=`which gconftool-2`

if [ ! -x "$gconftool" ]; then
echo 没有找到[gconftool-2]
exit 1
fi

imagePath=/tmp/sodao

if [ ! -d "$imagePath" ]; then
mkdir -p $imagePath
if [ ! -d "$imagePath" ]; then
echo 创建[$imagePath]目录失败
exit 1
fi
fi

url="http://www.sodao.com/app/ShowTime/gt?pcs_id=1&size=3"
#下面这个地址是青岛版,上面那个是杭州的
#url="http://www.sodao.com/app/ShowTime/gt1?pcs_id=9&size=3"

while [ true ]
do
imgUrl=`curl --connect-timeout 20 --retry 2 -s $url |awk -F, '{print $5}' | awk -F\" '{print $4}'`
if [ -n "$imgUrl" ]; then
fileName=`echo $imgUrl | awk -F\/ '{print $NF}'`
if [ -f "$imagePath/$fileName" -a "$fileName" = "$lastName" ];then
continue
fi

curl --connect-timeout 20 --retry 2 -s $imgUrl -o $imagePath/$fileName

if [ ! -f "$imagePath/$fileName" ]; then
echo 获取[$imgUrl]失败
continue
fi

$gconftool -s /desktop/gnome/background/picture_filename -t string "$imagePath/$fileName" -s /desktop/gnome/background/picture_options zoom
lastName=$fileName
fi
sleep 1
done

[/bash]

把上面的脚本复制到你的机器上,执行一下,你的桌面就会每分钟更换一个美女为你报时。不过要注意只能在GNOME环境下用,因为我的系统中只装了GNOME环境,KDE的用户只要把设置桌面的命令改成KDE下设置桌面的命令即可。

有了上面的脚本,我们就可以加入开机自动启动。加在rc.local和 crontab是不行的,因为gconftool-2依赖于很多GNOME桌面环境变量,而有些变量是每次启动图形都不一样。不过GNOME是支持自动启动配置程序的,方法如下:

在$HOME/.config/autostart目录中新建一个文件:

[Desktop Entry]

Version=1.0

Encoding=UTF-8

Name=wallpaper

Type=Application

Exec=/home/linuxsong/wallpaper.sh

Terminal=false

Comment=Girl wallpaper

Categories=Utility;

X-Desktop-File-Install-Version=0.15

Hidden=false

把Exec=/home/linuxsong/wallpaper.sh这句中的路径改成你自己的路径即可。

重新注销一下看看你的桌面,是不是有美女为你报时?