代码: 全选
#!/bin/bash
WD="${HOME}/picture/Wallpaper/random 0" # 壁纸目录(目前不能包含中文)
INTERVAL=1 # 更换时间间隔(秒)
DELAY=0 # 第一次更换壁纸前的暂停时间(秒)
TYPES=":image/jpeg:image/jpg:image/png:image/x-ms-bmp:image/gif:" # 文件类型
# -----------------------------------------------------------------------------
HISTORY=$HOME/.swp_history
# 此处是个死循环 @@
(export IFS=$'\n'
while sleep $DELAY; do
# 获取文件列表
files="$(for f in "$WD"/*; do
if [[ $TYPES =~ :$(file -bi "$f"): ]]; then
echo "$f" && let "i++"
fi
done)"
# 将文件列表和已经更换过的壁纸历史对照,得到剩余壁纸序列
que=(`echo "$files" | cat - "$HISTORY" 2>/dev/null | sort | uniq -u`)
ub=$((${#que[@]} - 1))
# 如果已经没有壁纸可显示,清空历史重来
if [ $ub -lt 0 ]; then
: > "$HISTORY"
continue;
fi
# 随机取出一张壁纸,更换成功后写入历史
if [ $ub -eq 0 ]; then x=0; else let "x = $RANDOM % ub + 1"; fi
path="${que[$x]}"
gconftool-2 --set "/desktop/gnome/background/picture_filename" \
--type "string" "$path"
[[ $? -eq 0 ]] && echo "$path" >> "$HISTORY"
sleep $INTERVAL
done)
exit 0