bash Shell 脚本更改桌面壁纸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5550895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell script changing desktop wallpaper
提问by xralf
Could you write the easiest possible shell script that will change the desktop wallpaper (in Ubuntu) in regular intervals (e.g. 1 minute).
你能写出最简单的 shell 脚本来定期(例如 1 分钟)更改桌面壁纸(在 Ubuntu 中)。
Wallpapers will be saved in particular directory (e.g. $HOME/wallpapers). I need only basic functionality.
壁纸将保存在特定目录中(例如 $HOME/wallpapers)。我只需要基本功能。
1) select random wallpaper from $HOME/wallpapers
2) set it as wallpaper on desktop
3) set cron to run the script every minute (not part of the question).
1) 从$HOME/wallpapers
2 ) 中选择随机墙纸3) 将其设置为桌面墙纸
3) 将 cron 设置为每分钟运行一次脚本(不是问题的一部分)。
采纳答案by tamasgal
#!/bin/bash
wallpaperdir='$HOME/wallpaper'
files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`
gconftool-2 -t str --set /desktop/gnome/background/picture_filename "$randompic"
Save this script and edit your with the command "crontab -e" (it launches an editor where you put this line at the end of the file):
保存此脚本并使用命令“crontab -e”进行编辑(它会启动一个编辑器,您将这一行放在文件末尾):
*/1 * * * * /bin/bash /path/to/script.sh
edit: I assumed you're using gnome. If not you need to edit the last line, because my example uses the Gnome Conftool. ;)
编辑:我假设您正在使用 gnome。如果不是,您需要编辑最后一行,因为我的示例使用 Gnome Conftool。;)
To change the background in XFCE, you should change the line with gconftool-2 to:
要更改 XFCE 中的背景,您应该将 gconftool-2 的行更改为:
echo -e “# xfce backdrop list\n$randompic”>$HOME/.config/xfce4/desktop/backdrops.list
killall -USR1 xfdesktop
回答by Migwel
I know this answer is kind of late but since it could help some people, I'm posting it.
我知道这个答案有点晚了,但由于它可以帮助某些人,所以我将其发布。
From septi's code plus some modifications, here is my solution :
从 septi 的代码加上一些修改,这是我的解决方案:
#!/bin/bash
wallpaperdir="$HOME/wallpaper"
files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`
echo -e "# xfce backdrop list\n$randompic">$HOME/.config/xfce4/desktop/backdrop.list
xfdesktop --reload
The single quotes must be replaced by double quotes in order for the computer to interpret the $HOME part correctly. Also, the file you want to edit is backdrop.list, not backdrops.list. And finally, I find that using killall is kind of excessive in this case, since you can simply reload xfdesktop.
单引号必须替换为双引号,以便计算机正确解释 $HOME 部分。另外,您要编辑的文件是background.list,而不是backgrounds.list。最后,我发现在这种情况下使用 killall 有点过分,因为您可以简单地重新加载 xfdesktop。
I've tested it on my computer (Linux Mint Debian Edition) and it seems to work perfectly.
我已经在我的电脑(Linux Mint Debian 版)上测试过它,它似乎工作得很好。
Hope it helps. =)
希望能帮助到你。=)
EDIT : I forgot to mention that you have to add DISPLAY=:0.0 before your command, in crontab. That gives
编辑:我忘了提到您必须在 crontab 中的命令之前添加 DISPLAY=:0.0。那给
*/1 * * * * DISPLAY=:0.0 wallpaper.sh
回答by mackatozis
This is just my approach on this matter. I don't claim that it's the ideal one.
这只是我在这个问题上的做法。我并没有声称这是理想的。
WALLS_PATH=/path/to/images
cd $WALLS_PATH
while [ 1 ]; do
for NEW_WALL in "$WALLS_PATH"/*; do
gsettings set org.gnome.desktop.background picture-uri "file://${NEW_WALL}"
sleep 1800
done
done
回答by tomeu_quely
For gnome3 you need to use gsettings instead of gconftool.
对于 gnome3,您需要使用 gsettings 而不是 gconftool。
But if you're going to execute the script throught cron it will not work.
但是,如果您要通过 cron 执行脚本,它将不起作用。
I've tried a lot of .sh scripts but no one works for me.
我尝试了很多 .sh 脚本,但没有一个适合我。
At the end, i fixed it using this python script that loads a random wallpaper from a folder:
最后,我使用这个从文件夹加载随机壁纸的 python 脚本修复了它:
#!/usr/bin/env python
#coding: utf8
import os,random
setup = "/path_to_folder/" + random.choice(os.listdir("/path_to_folder/"))
os.system("DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri 'file://%s'" %(setup))
Hope it helps for someone with my same problem!
希望它对有我同样问题的人有所帮助!
回答by Indra
This worked for me in Gnome:
这在 Gnome 中对我有用:
#!/bin/bash
DIR="/home/user/Pictures/wallpapers"
PIC=$(find $DIR -type f -maxdepth 1 | shuf -n1)
gsettings set org.gnome.desktop.background picture-uri "file://$PIC"