這個改變壁紙的腳本有什麼問題嗎?

這個改變壁紙的腳本有什麼問題嗎?

我對 Linux 世界還很陌生,所以我會請求原諒我現在提出的每一個愚蠢或過於簡單的問題。

我昨天從他們的網站安裝了 Elementary OS。我想用Bing 的壁紙自動更改我的壁紙,所以我在Google 上找到了一個腳本,可以從Bing 的伺服器下載圖像......這很好,它有效(我看到資料夾內下載的圖像)。

問題是它並不總是更換壁紙。有時是,有時不是。我真的不明白什麼時候以及為什麼。當下載的圖像不是新的時,似乎會發生這種情況。抱歉我的英文不好,我的意思是我用 Elementary OS 提供的工具啟動這個腳本,如果我每天打開我的電腦 >1 次,(並且 Bing 的圖像每天更改一次)其他時候我只是把一切都變成黑色當壁紙。這是更改壁紙的程式碼部分,也許存在問題:

#!/bin/bash

# $bing is needed to form the fully qualified URL for
# the Bing pic of the day
bing="www.bing.com"

# $xmlURL is needed to get the xml data from which
# the relative URL for the Bing pic of the day is extracted
#
# The mkt parameter determines which Bing market you would like to
# obtain your images from.
# Valid values are: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ, en-CA.
#
# The idx parameter determines where to start from. 0 is the current day,
# 1 the previous day, etc.
xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=1&n=1&mkt=en-US"

# $saveDir is used to set the location where Bing pics of the day
# are stored.  $HOME holds the path of the current user's home directory
saveDir=$HOME'/Pictures/BingDesktopImages/'

# Create saveDir if it does not already exist
mkdir -p $saveDir

# Set picture options
# Valid options are: none,wallpaper,centered,scaled,stretched,zoom,spanned
picOpts="zoom"

# The desired Bing picture resolution to download
# Valid options: "_1024x768" "_1280x720" "_1366x768" "_1920x1200"
desiredPicRes="_1366x768"

# The file extension for the Bing pic
picExt=".jpg"

# Extract the relative URL of the Bing pic of the day from
# the XML data retrieved from xmlURL, form the fully qualified
# URL for the pic of the day, and store it in $picURL

# Form the URL for the desired pic resolution
desiredPicURL=$bing$(echo $(curl -s $xmlURL) | grep -oP "<urlBase>(.*)</urlBase>" | cut -d ">" -f 2 | cut -d "<" -f 1)$desiredPicRes$picExt

# Form the URL for the default pic resolution
defaultPicURL=$bing$(echo $(curl -s $xmlURL) | grep -oP "<url>(.*)</url>" | cut -d ">" -f 2 | cut -d "<" -f 1)

# $picName contains the filename of the Bing pic of the day

# Attempt to download the desired image resolution. If it doesn't
# exist then download the default image resolution
if wget --quiet --spider "$desiredPicURL"
then

    # Set picName to the desired picName
    picName=${desiredPicURL##*/}
    # Download the Bing pic of the day at desired resolution
    curl -s -o $saveDir$picName $desiredPicURL
else
    # Set picName to the default picName
    picName=${defaultPicURL##*/}
    # Download the Bing pic of the day at default resolution
    curl -s -o $saveDir$picName $defaultPicURL
fi

# Set the GNOME3 wallpaper
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$saveDir$picName'"'

# Set the GNOME 3 wallpaper picture options
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-options $picOpts

 # Remove pictures older than 30 days
find $saveDir -atime 30 -delete

# Exit the script
exit

答案1

dconf您可以feh --bg-max相當輕鬆地設定背景圖像,而不是搞亂。

$ feh --bg-max <image>

我使用這種類型的腳本每 15 分鐘旋轉一次背景圖像,例如:

while true; do
  find ~/.wallpaper -type f \( -name '*.jpg' -o -name '*.png' \) -print0 |
    shuf -n1 -z | xargs -0 feh --bg-max
    sleep 15m
done

上面還使用了很少使用但方便的shuf命令來隨機化文件列表。

相關內容