我正在嘗試下載每日圖片並將其設定為我的背景圖片:
#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"
rm -f ${PICS}/wall.jpg
rm -f ${PICS}/photo-of-the-day
# download photo-of-the-day page
wget http://photography.nationalgeographic.com/photography/photo-of-the-day -O ${PICS}/photo-of-the-day
# parse the url out from the file
url=`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '"' -f 2`
# download the photo
wget http:$url -O ${PICS}/wall.jpg
# set the desktop background
URI=file:///${PICS}/wall.jpg
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri ${URI}
圖像按預期下載,但背景實際上並未設定。奇怪的是,如果我修改 URI 以包含更多或更少的正斜杠,它就會起作用,但只會執行一次。為了使本節正常工作,我最終不得不每次都以一種毫無意義的方式修改腳本。
可能是什麼原因造成的?
答案1
試試這樣:
#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"
rm -f "${PICS}/wall.jpg"
rm -f "${PICS}/photo-of-the-day"
# download photo-of-the-day page
wget "http://photography.nationalgeographic.com/photography/photo-of-the-day" -O "${PICS}/photo-of-the-day"
# parse the url out from the file
url="`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '\"' -f 2`"
# download the photo
wget "http:$url" -O "${PICS}/wall.jpg"
# set the desktop background
# only two slashes here, because the PICS var already has a leading slash
URI="file://${PICS}/wall.jpg"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"
答案2
您可以引用URI
cmks 顯示的完整變量,或確保引用file://
和wall.jpg
,如下所示:
URI="file:///"${PICS}"/wall.jpg"
這是您的腳本的一個小改進。變數用於縮短命令行。文件保存到/tmp
,每次系統重新啟動時都會刪除該文件,因此您不必手動清除快取。 AWK 用於改進解析並減少管道。wget
直接寫入AWK以避免保存額外的文件
#!/bin/bash
# set variables to shorten lines
FILE="/tmp/photo_of_the_day"
PAGE="http://photography.nationalgeographic.com/photography/photo-of-the-day"
SEARCH="images.nationalgeographic.com.*cache.*990x742.jpg"
# get image URI directly
IMAGE=$(wget "$PAGE" -O - -o /dev/null | awk -F'"' -v regex="$SEARCH" '$0~ regex {print $2}')
# download the photo
wget http:$IMAGE -O "$FILE"
# set the desktop background
URI="file:///$FILE"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"
答案3
我想我發現了問題:即使我轉到文件,右鍵單擊並說“設置為壁紙...”,也沒有任何反應。所以我假設文件名每次都不會改變; Linux 有某種節省成本的「我實際上不會刷新,因為那是相同的圖像」功能。若要強制系統每次都識別它是新圖片,請更改檔案名,如下所示:
#!/bin/bash
# clear cache
PICS="/home/pvlkmrv/Pictures"
RAND=$RANDOM
rm -f ${PICS}/*.jpg
rm -f ${PICS}/photo-of-the-day
# download photo-of-the-day page
wget http://photography.nationalgeographic.com/photography/photo-of-the-day -O ${PICS}/photo-of-the-day
# parse the url out from the file
url=`cat ${PICS}/photo-of-the-day | grep 'images.nationalgeographic.com.*cache.*990x742.jpg' | cut -d '"' -f 2`
# download the photo
wget http:$url -O ${PICS}/wall${RAND}.jpg
# set the desktop background
URI="file://${PICS}/wall${RAND}.jpg"
echo ${URI}
gsettings set org.gnome.desktop.background picture-options 'centered'
gsettings set org.gnome.desktop.background picture-uri "${URI}"